Skip to content

Commit

Permalink
fix: Fix the command GenerateKeyPair
Browse files Browse the repository at this point in the history
  • Loading branch information
oharzallah-greenbull authored and chalasr committed Nov 29, 2024
1 parent 44272ff commit 9dfe896
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 30 deletions.
31 changes: 15 additions & 16 deletions src/Command/GenerateKeyPairCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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!');
Expand Down Expand Up @@ -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) {
Expand All @@ -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',
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 7 additions & 13 deletions tests/Functional/Command/GenerateKeyPairCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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_');
Expand All @@ -28,8 +28,7 @@ public function testItGeneratesKeyPair($algorithm, $passphrase)
new Filesystem(),
$privateKeyFile,
$publicKeyFile,
$passphrase,
$algorithm
$passphrase
)
);

Expand Down Expand Up @@ -86,8 +85,7 @@ public function testOverwriteAndSkipCannotBeCombined()
new Filesystem(),
$privateKeyFile,
$publicKeyFile,
null,
'RS256'
null
)
);
$input = ['--overwrite' => true, '--skip-if-exists' => true];
Expand Down Expand Up @@ -117,8 +115,7 @@ public function testNoOverwriteDoesNotOverwrite()
new Filesystem(),
$privateKeyFile,
$publicKeyFile,
null,
'RS256'
null
)
);

Expand Down Expand Up @@ -148,8 +145,7 @@ public function testOverwriteActuallyOverwrites()
new Filesystem(),
$privateKeyFile,
$publicKeyFile,
null,
'RS256'
null
)
);

Expand All @@ -176,8 +172,7 @@ public function testSkipIfExistsWritesIfNotExists()
new Filesystem(),
$privateKeyFile,
$publicKeyFile,
null,
'RS256'
null
)
);

Expand All @@ -202,8 +197,7 @@ public function testSkipIfExistsDoesNothingIfExists()
new Filesystem(),
$privateKeyFile,
$publicKeyFile,
null,
'RS256'
null
)
);

Expand Down

0 comments on commit 9dfe896

Please sign in to comment.