56 lines
1.7 KiB
PHP
56 lines
1.7 KiB
PHP
<?php
|
|||
|
|
|
||
|
|
namespace App\Console\Commands;
|
||
|
|
|
||
|
|
use Illuminate\Console\Command;
|
||
|
|
use Illuminate\Support\Facades\Storage;
|
||
|
|
|
||
|
|
class GenerateLicenseKeysCommand extends Command
|
||
|
|
{
|
||
|
|
protected $signature = 'license:generate-keys {--force : Overwrite existing key pair}';
|
||
|
|
|
||
|
|
protected $description = 'Generate a new application-wide license signing key pair.';
|
||
|
|
|
||
|
|
public function handle(): int
|
||
|
|
{
|
||
|
|
$directory = 'license-keys';
|
||
|
|
$privateKeyPath = $directory . '/private.pem';
|
||
|
|
$publicKeyPath = $directory . '/public.pem';
|
||
|
|
$disk = Storage::disk('local');
|
||
|
|
|
||
|
|
$privateExists = $disk->exists($privateKeyPath);
|
||
|
|
$publicExists = $disk->exists($publicKeyPath);
|
||
|
|
|
||
|
|
if (($privateExists || $publicExists) && !$this->option('force')) {
|
||
|
|
$this->error('Key pair already exists. Use --force to overwrite.');
|
||
|
|
return 1;
|
||
|
|
}
|
||
|
|
|
||
|
|
$keyPair = $this->generateKeyPair();
|
||
|
|
$disk->put($privateKeyPath, $keyPair['privateKey']);
|
||
|
|
$disk->put($publicKeyPath, $keyPair['publicKey']);
|
||
|
|
|
||
|
|
$this->info('Generated application license key pair.');
|
||
|
|
$this->line('Private key: ' . $privateKeyPath);
|
||
|
|
$this->line('Public key: ' . $publicKeyPath);
|
||
|
|
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
protected function generateKeyPair(): array
|
||
|
|
{
|
||
|
|
$privateKey = openssl_pkey_new([
|
||
|
|
'private_key_bits' => 2048,
|
||
|
|
'private_key_type' => OPENSSL_KEYTYPE_RSA,
|
||
|
|
]);
|
||
|
|
|
||
|
|
openssl_pkey_export($privateKey, $privateKeyPem);
|
||
|
|
$publicKeyDetails = openssl_pkey_get_details($privateKey);
|
||
|
|
|
||
|
|
return [
|
||
|
|
'privateKey' => $privateKeyPem,
|
||
|
|
'publicKey' => $publicKeyDetails['key'],
|
||
|
|
];
|
||
|
|
}
|
||
|
|
}
|