diff --git a/src/Command/GenerateKeyPairCommand.php b/src/Command/GenerateKeyPairCommand.php index 4b33d4b..58bdeaa 100644 --- a/src/Command/GenerateKeyPairCommand.php +++ b/src/Command/GenerateKeyPairCommand.php @@ -8,6 +8,7 @@ use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; use Symfony\Component\Filesystem\Filesystem; @@ -43,16 +44,13 @@ final class GenerateKeyPairCommand extends Command private ?string $passphrase; - private string $algorithm; - - public function __construct(Filesystem $filesystem, string $secretKey, string $publicKey, ?string $passphrase, string $algorithm) + public function __construct(Filesystem $filesystem, string $secretKey, string $publicKey, ?string $passphrase) { parent::__construct(); $this->filesystem = $filesystem; $this->secretKey = $secretKey; $this->publicKey = $publicKey; $this->passphrase = $passphrase; - $this->algorithm = $algorithm; } protected function configure(): void @@ -61,19 +59,20 @@ protected function configure(): void $this->addOption('dry-run', null, InputOption::VALUE_NONE, 'Do not update key files.'); $this->addOption('skip-if-exists', null, InputOption::VALUE_NONE, 'Do not update key files if they already exist.'); $this->addOption('overwrite', null, InputOption::VALUE_NONE, 'Overwrite key files if they already exist.'); + $this->addArgument('algorithm', InputArgument::OPTIONAL, sprintf('The algorithm code, possible values : %s', implode(self::ACCEPTED_ALGORITHMS)), 'RS256'); } protected function execute(InputInterface $input, OutputInterface $output): int { $io = new SymfonyStyle($input, $output); - - if (!\in_array($this->algorithm, self::ACCEPTED_ALGORITHMS, true)) { - $io->error(\sprintf('Cannot generate key pair with the provided algorithm `%s`.', $this->algorithm)); + $algorithm = $input->getArgument('algorithm'); + if (!\in_array($algorithm, self::ACCEPTED_ALGORITHMS, true)) { + $io->error(\sprintf('Cannot generate key pair with the provided algorithm `%s`.', $algorithm)); return Command::FAILURE; } - [$secretKey, $publicKey] = $this->generateKeyPair($this->passphrase); + [$secretKey, $publicKey] = $this->generateKeyPair($this->passphrase, $algorithm); if ($input->getOption('dry-run')) { $io->success('Your keys have been generated!'); @@ -137,9 +136,9 @@ private function handleExistingKeys(InputInterface $input): void /** * @return array{0: string, 1: string} */ - private function generateKeyPair(?string $passphrase): array + private function generateKeyPair(?string $passphrase, string $algorithm): array { - $config = $this->buildOpenSSLConfiguration(); + $config = $this->buildOpenSSLConfiguration($algorithm); $resource = openssl_pkey_new($config); if (false === $resource) { @@ -165,7 +164,7 @@ private function generateKeyPair(?string $passphrase): array return [$privateKey, $publicKeyData['key']]; } - private function buildOpenSSLConfiguration(): array + private function buildOpenSSLConfiguration(string $algorithm): array { $digestAlgorithms = [ 'RS256' => 'sha256', @@ -208,13 +207,13 @@ private function buildOpenSSLConfiguration(): array ]; $config = [ - 'digest_alg' => $digestAlgorithms[$this->algorithm], - 'private_key_type' => $privateKeyTypes[$this->algorithm], - 'private_key_bits' => $privateKeyBits[$this->algorithm], + 'digest_alg' => $digestAlgorithms[$algorithm], + 'private_key_type' => $privateKeyTypes[$algorithm], + 'private_key_bits' => $privateKeyBits[$algorithm], ]; - if (isset($curves[$this->algorithm])) { - $config['curve_name'] = $curves[$this->algorithm]; + if (isset($curves[$algorithm])) { + $config['curve_name'] = $curves[$algorithm]; } return $config; diff --git a/src/Resources/config/services.php b/src/Resources/config/services.php index 582d97c..19919f0 100644 --- a/src/Resources/config/services.php +++ b/src/Resources/config/services.php @@ -265,7 +265,7 @@ abstract_arg('Public key'), abstract_arg('Private key passphrase'), ]) - ->tag('consome.command', ['command' => 'league:oauth2-server:generate-keypair']) + ->tag('console.command', ['command' => 'league:oauth2-server:generate-keypair']) ->alias(GenerateKeyPairCommand::class, 'league.oauth2_server.command.generate_keypair') // Utility services diff --git a/tests/Functional/Command/GenerateKeyPairCommandTest.php b/tests/Functional/Command/GenerateKeyPairCommandTest.php index 22a8dc5..b5f9995 100644 --- a/tests/Functional/Command/GenerateKeyPairCommandTest.php +++ b/tests/Functional/Command/GenerateKeyPairCommandTest.php @@ -14,7 +14,7 @@ class GenerateKeyPairCommandTest extends TestCase /** * @dataProvider providePassphrase */ - public function testItGeneratesKeyPair($algorithm, $passphrase) + public function testItGeneratesKeyPair($passphrase) { $privateKeyFile = tempnam(sys_get_temp_dir(), 'private_'); $publicKeyFile = tempnam(sys_get_temp_dir(), 'public_'); @@ -28,8 +28,7 @@ public function testItGeneratesKeyPair($algorithm, $passphrase) new Filesystem(), $privateKeyFile, $publicKeyFile, - $passphrase, - $algorithm + $passphrase ) ); @@ -86,8 +85,7 @@ public function testOverwriteAndSkipCannotBeCombined() new Filesystem(), $privateKeyFile, $publicKeyFile, - null, - 'RS256' + null ) ); $input = ['--overwrite' => true, '--skip-if-exists' => true]; @@ -117,8 +115,7 @@ public function testNoOverwriteDoesNotOverwrite() new Filesystem(), $privateKeyFile, $publicKeyFile, - null, - 'RS256' + null ) ); @@ -148,8 +145,7 @@ public function testOverwriteActuallyOverwrites() new Filesystem(), $privateKeyFile, $publicKeyFile, - null, - 'RS256' + null ) ); @@ -176,8 +172,7 @@ public function testSkipIfExistsWritesIfNotExists() new Filesystem(), $privateKeyFile, $publicKeyFile, - null, - 'RS256' + null ) ); @@ -202,8 +197,7 @@ public function testSkipIfExistsDoesNothingIfExists() new Filesystem(), $privateKeyFile, $publicKeyFile, - null, - 'RS256' + null ) );