Skip to content

Commit b13771a

Browse files
committed
bug #197 fix: Fix the command GenerateKeyPair (oharzallah-greenbull)
This PR was squashed before being merged into the 0.9-dev branch. Discussion ---------- fix: Fix the command GenerateKeyPair - Fix a typo in service decalaration - Make algorithm as an argument because it was missing in the service definition and cannot be declared Commits ------- 9dfe896 fix: Fix the command GenerateKeyPair
2 parents 746a5f5 + 9dfe896 commit b13771a

File tree

3 files changed

+23
-30
lines changed

3 files changed

+23
-30
lines changed

src/Command/GenerateKeyPairCommand.php

+15-16
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Symfony\Component\Console\Command\Command;
99
use Symfony\Component\Console\Input\InputInterface;
1010
use Symfony\Component\Console\Input\InputOption;
11+
use Symfony\Component\Console\Input\InputArgument;
1112
use Symfony\Component\Console\Output\OutputInterface;
1213
use Symfony\Component\Console\Style\SymfonyStyle;
1314
use Symfony\Component\Filesystem\Filesystem;
@@ -43,16 +44,13 @@ final class GenerateKeyPairCommand extends Command
4344

4445
private ?string $passphrase;
4546

46-
private string $algorithm;
47-
48-
public function __construct(Filesystem $filesystem, string $secretKey, string $publicKey, ?string $passphrase, string $algorithm)
47+
public function __construct(Filesystem $filesystem, string $secretKey, string $publicKey, ?string $passphrase)
4948
{
5049
parent::__construct();
5150
$this->filesystem = $filesystem;
5251
$this->secretKey = $secretKey;
5352
$this->publicKey = $publicKey;
5453
$this->passphrase = $passphrase;
55-
$this->algorithm = $algorithm;
5654
}
5755

5856
protected function configure(): void
@@ -61,19 +59,20 @@ protected function configure(): void
6159
$this->addOption('dry-run', null, InputOption::VALUE_NONE, 'Do not update key files.');
6260
$this->addOption('skip-if-exists', null, InputOption::VALUE_NONE, 'Do not update key files if they already exist.');
6361
$this->addOption('overwrite', null, InputOption::VALUE_NONE, 'Overwrite key files if they already exist.');
62+
$this->addArgument('algorithm', InputArgument::OPTIONAL, sprintf('The algorithm code, possible values : %s', implode(self::ACCEPTED_ALGORITHMS)), 'RS256');
6463
}
6564

6665
protected function execute(InputInterface $input, OutputInterface $output): int
6766
{
6867
$io = new SymfonyStyle($input, $output);
69-
70-
if (!\in_array($this->algorithm, self::ACCEPTED_ALGORITHMS, true)) {
71-
$io->error(\sprintf('Cannot generate key pair with the provided algorithm `%s`.', $this->algorithm));
68+
$algorithm = $input->getArgument('algorithm');
69+
if (!\in_array($algorithm, self::ACCEPTED_ALGORITHMS, true)) {
70+
$io->error(\sprintf('Cannot generate key pair with the provided algorithm `%s`.', $algorithm));
7271

7372
return Command::FAILURE;
7473
}
7574

76-
[$secretKey, $publicKey] = $this->generateKeyPair($this->passphrase);
75+
[$secretKey, $publicKey] = $this->generateKeyPair($this->passphrase, $algorithm);
7776

7877
if ($input->getOption('dry-run')) {
7978
$io->success('Your keys have been generated!');
@@ -137,9 +136,9 @@ private function handleExistingKeys(InputInterface $input): void
137136
/**
138137
* @return array{0: string, 1: string}
139138
*/
140-
private function generateKeyPair(?string $passphrase): array
139+
private function generateKeyPair(?string $passphrase, string $algorithm): array
141140
{
142-
$config = $this->buildOpenSSLConfiguration();
141+
$config = $this->buildOpenSSLConfiguration($algorithm);
143142

144143
$resource = openssl_pkey_new($config);
145144
if (false === $resource) {
@@ -165,7 +164,7 @@ private function generateKeyPair(?string $passphrase): array
165164
return [$privateKey, $publicKeyData['key']];
166165
}
167166

168-
private function buildOpenSSLConfiguration(): array
167+
private function buildOpenSSLConfiguration(string $algorithm): array
169168
{
170169
$digestAlgorithms = [
171170
'RS256' => 'sha256',
@@ -208,13 +207,13 @@ private function buildOpenSSLConfiguration(): array
208207
];
209208

210209
$config = [
211-
'digest_alg' => $digestAlgorithms[$this->algorithm],
212-
'private_key_type' => $privateKeyTypes[$this->algorithm],
213-
'private_key_bits' => $privateKeyBits[$this->algorithm],
210+
'digest_alg' => $digestAlgorithms[$algorithm],
211+
'private_key_type' => $privateKeyTypes[$algorithm],
212+
'private_key_bits' => $privateKeyBits[$algorithm],
214213
];
215214

216-
if (isset($curves[$this->algorithm])) {
217-
$config['curve_name'] = $curves[$this->algorithm];
215+
if (isset($curves[$algorithm])) {
216+
$config['curve_name'] = $curves[$algorithm];
218217
}
219218

220219
return $config;

src/Resources/config/services.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@
265265
abstract_arg('Public key'),
266266
abstract_arg('Private key passphrase'),
267267
])
268-
->tag('consome.command', ['command' => 'league:oauth2-server:generate-keypair'])
268+
->tag('console.command', ['command' => 'league:oauth2-server:generate-keypair'])
269269
->alias(GenerateKeyPairCommand::class, 'league.oauth2_server.command.generate_keypair')
270270

271271
// Utility services

tests/Functional/Command/GenerateKeyPairCommandTest.php

+7-13
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class GenerateKeyPairCommandTest extends TestCase
1414
/**
1515
* @dataProvider providePassphrase
1616
*/
17-
public function testItGeneratesKeyPair($algorithm, $passphrase)
17+
public function testItGeneratesKeyPair($passphrase)
1818
{
1919
$privateKeyFile = tempnam(sys_get_temp_dir(), 'private_');
2020
$publicKeyFile = tempnam(sys_get_temp_dir(), 'public_');
@@ -28,8 +28,7 @@ public function testItGeneratesKeyPair($algorithm, $passphrase)
2828
new Filesystem(),
2929
$privateKeyFile,
3030
$publicKeyFile,
31-
$passphrase,
32-
$algorithm
31+
$passphrase
3332
)
3433
);
3534

@@ -86,8 +85,7 @@ public function testOverwriteAndSkipCannotBeCombined()
8685
new Filesystem(),
8786
$privateKeyFile,
8887
$publicKeyFile,
89-
null,
90-
'RS256'
88+
null
9189
)
9290
);
9391
$input = ['--overwrite' => true, '--skip-if-exists' => true];
@@ -117,8 +115,7 @@ public function testNoOverwriteDoesNotOverwrite()
117115
new Filesystem(),
118116
$privateKeyFile,
119117
$publicKeyFile,
120-
null,
121-
'RS256'
118+
null
122119
)
123120
);
124121

@@ -148,8 +145,7 @@ public function testOverwriteActuallyOverwrites()
148145
new Filesystem(),
149146
$privateKeyFile,
150147
$publicKeyFile,
151-
null,
152-
'RS256'
148+
null
153149
)
154150
);
155151

@@ -176,8 +172,7 @@ public function testSkipIfExistsWritesIfNotExists()
176172
new Filesystem(),
177173
$privateKeyFile,
178174
$publicKeyFile,
179-
null,
180-
'RS256'
175+
null
181176
)
182177
);
183178

@@ -202,8 +197,7 @@ public function testSkipIfExistsDoesNothingIfExists()
202197
new Filesystem(),
203198
$privateKeyFile,
204199
$publicKeyFile,
205-
null,
206-
'RS256'
200+
null
207201
)
208202
);
209203

0 commit comments

Comments
 (0)