simeonshopov

Untitled

Nov 6th, 2024
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.65 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Console\Commands;
  4.  
  5. use Illuminate\Console\Command;
  6.  
  7. class GenerateTokenKeys extends Command
  8. {
  9.     /**
  10.      * The name and signature of the console command.
  11.      *
  12.      * @var string
  13.      */
  14.     protected $signature = 'app:generate-token-keys';
  15.  
  16.     /**
  17.      * The console command description.
  18.      *
  19.      * @var string
  20.      */
  21.     protected $description = 'Command to generate public and private keys for JWT.';
  22.  
  23.     /**
  24.      * Execute the console command.
  25.      */
  26.     final public function handle(): void
  27.     {
  28.         $this->info('Generating public and private keys for JWT...');
  29.  
  30.         $keys = [
  31.             'public' => 'file://'.storage_path('app/private/jwt/public.pem'),
  32.             'private' => 'file://'.storage_path('app/private/jwt/private.pem'),
  33.         ];
  34.  
  35.         $passphrase = $this->secret('Enter the passphrase for the private key: ');
  36.         $passphraseConfirm = $this->secret('Confirm the passphrase for the private key: ');
  37.  
  38.         if ($passphrase !== $passphraseConfirm) {
  39.             $this->error('The passphrases do not match.');
  40.             return;
  41.         }
  42.  
  43.         $config = [
  44.             'curve_name' => 'secp521r1',
  45.             'private_key_bits' => 4096,
  46.             'private_key_type' => OPENSSL_KEYTYPE_EC,
  47.         ];
  48.  
  49.         $res = openssl_pkey_new($config);
  50.  
  51.         openssl_pkey_export($res, $privateKey, $passphrase);
  52.         file_put_contents($keys['private'], $privateKey);
  53.  
  54.         $publicKey = openssl_pkey_get_details($res)['key'];
  55.         file_put_contents($keys['public'], $publicKey);
  56.  
  57.         $this->info('Public and private keys generated successfully.');
  58.     }
  59. }
  60.  
Advertisement
Add Comment
Please, Sign In to add comment