Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Console\Commands;
- use Illuminate\Console\Command;
- class GenerateTokenKeys extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'app:generate-token-keys';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = 'Command to generate public and private keys for JWT.';
- /**
- * Execute the console command.
- */
- final public function handle(): void
- {
- $this->info('Generating public and private keys for JWT...');
- $keys = [
- 'public' => 'file://'.storage_path('app/private/jwt/public.pem'),
- 'private' => 'file://'.storage_path('app/private/jwt/private.pem'),
- ];
- $passphrase = $this->secret('Enter the passphrase for the private key: ');
- $passphraseConfirm = $this->secret('Confirm the passphrase for the private key: ');
- if ($passphrase !== $passphraseConfirm) {
- $this->error('The passphrases do not match.');
- return;
- }
- $config = [
- 'curve_name' => 'secp521r1',
- 'private_key_bits' => 4096,
- 'private_key_type' => OPENSSL_KEYTYPE_EC,
- ];
- $res = openssl_pkey_new($config);
- openssl_pkey_export($res, $privateKey, $passphrase);
- file_put_contents($keys['private'], $privateKey);
- $publicKey = openssl_pkey_get_details($res)['key'];
- file_put_contents($keys['public'], $publicKey);
- $this->info('Public and private keys generated successfully.');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment