2022-10-29

Laravel 9.x proper organization of .mobileconfig file generation through a zsh script for further download

I am refactoring my project using Laravel 9, which was originally written in native PHP.

In the past, I have implemented simple method of generating some unsigned.mobileconfig file, then saving it to the download folder, signing it with my certs and creating a link for the user to download it. Then i deleted old configurations via cron once a week.

I have following questions:

  1. Is it possible to create and simultaneously sign a similar configuration file "on the fly" in Laravel, without saving it on the backend? If not, what is the best way to organize this process so that the link will be available via api routing?

  2. Where is the best place to store script files in the standard Laravel project structure? Or is it easier to store them on the server side and run them using, for example, this Symfony package?

Some words about my new project - i'm using default Laravel folder structure with Spatie DTO's and business-logic organized in service layer ('tiny controllers'). Main routings are written in routes/api.php.

My project structure:

app/Console
   /Exceptions
   /Helpers
     Helper.php <-- Class for methods like generatePassword(), generateRandomString() etc
   /Http
     /Controllers <-- Controllers for api
       UserController.php
     /Middleware
   /Providers
   /MyApp <-- Main folder for my application
     /Builders <-- Builders for SpatieDTO data-classes
       UserDataBuilder.php
     /DTO
       UserData.php <-- Spatie-extended data-classes
     /User
       /Services <-- Services for implementing main business-logic
         UserService.php
       /Model
         User.php
     /Tariff
       /Services
         TariffService.php
       /Model
         Tariff.php

I found only a few articles on similar cases, for example, this and this, but I could not determine the logic of my further actions.

Thanks!

Here is some review of this process:

GenerateConfig.php

// Variables for script     
putenv("USERID=$userId");
putenv("PASSWORD=$password");
// Some salt for configuration file name
putenv("SALT=$random");

$unsignedConfig = "zsh -c /path/to/mobileconfig.sh";
shell_exec($unsignedConfig);
$linkForSignedCfgDownloading = 'example.com/path/to/config/'cfg' . $random . '.mobileconfig';

<a href="' . linkForSignedCfgDownloading .'">Download your configuration</a>

mobileconfig.sh

#!/bin/zsh

# Putting some needed variables
USERID=$USERID
PASSWORD=$PASSWORD

cat << EOF >/path/to/config/download/unsigned.mobileconfig
<?xml version="1.0" encoding="UTF-8"?>
<plist version="1.0">
<dict>
    # Here some XML Data needed to be generated
</dict>
</plist>
EOF

# Next step after generating unsigned config file
source /path/to/next/script/signconfig.sh

signconfig.sh

#!/bin/zsh

USERID=$USERID
SALT=$SALT

# Signing new unsigned file

openssl smime -sign -in /path/to/config/download/unsigned.mobileconfig 
-out /path/to/config/download/cfg${SALT}.mobileconfig 
-signer /path/to/server/cert.pem -inkey /path/to/server/privkey.pem 
-certfile /path/to/server/chain.pem 
-outform der -nodetach

# Deleting unnecessary unsigned config file 

rm -f /path/to/config/download/unsigned.mobileconfig



No comments:

Post a Comment