8
8
use Symfony \Component \Console \Command \Command ;
9
9
use Symfony \Component \Console \Input \InputInterface ;
10
10
use Symfony \Component \Console \Input \InputOption ;
11
+ use Symfony \Component \Console \Input \InputArgument ;
11
12
use Symfony \Component \Console \Output \OutputInterface ;
12
13
use Symfony \Component \Console \Style \SymfonyStyle ;
13
14
use Symfony \Component \Filesystem \Filesystem ;
@@ -43,16 +44,13 @@ final class GenerateKeyPairCommand extends Command
43
44
44
45
private ?string $ passphrase ;
45
46
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 )
49
48
{
50
49
parent ::__construct ();
51
50
$ this ->filesystem = $ filesystem ;
52
51
$ this ->secretKey = $ secretKey ;
53
52
$ this ->publicKey = $ publicKey ;
54
53
$ this ->passphrase = $ passphrase ;
55
- $ this ->algorithm = $ algorithm ;
56
54
}
57
55
58
56
protected function configure (): void
@@ -61,19 +59,20 @@ protected function configure(): void
61
59
$ this ->addOption ('dry-run ' , null , InputOption::VALUE_NONE , 'Do not update key files. ' );
62
60
$ this ->addOption ('skip-if-exists ' , null , InputOption::VALUE_NONE , 'Do not update key files if they already exist. ' );
63
61
$ 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 ' );
64
63
}
65
64
66
65
protected function execute (InputInterface $ input , OutputInterface $ output ): int
67
66
{
68
67
$ 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 ));
72
71
73
72
return Command::FAILURE ;
74
73
}
75
74
76
- [$ secretKey , $ publicKey ] = $ this ->generateKeyPair ($ this ->passphrase );
75
+ [$ secretKey , $ publicKey ] = $ this ->generateKeyPair ($ this ->passphrase , $ algorithm );
77
76
78
77
if ($ input ->getOption ('dry-run ' )) {
79
78
$ io ->success ('Your keys have been generated! ' );
@@ -137,9 +136,9 @@ private function handleExistingKeys(InputInterface $input): void
137
136
/**
138
137
* @return array{0: string, 1: string}
139
138
*/
140
- private function generateKeyPair (?string $ passphrase ): array
139
+ private function generateKeyPair (?string $ passphrase, string $ algorithm ): array
141
140
{
142
- $ config = $ this ->buildOpenSSLConfiguration ();
141
+ $ config = $ this ->buildOpenSSLConfiguration ($ algorithm );
143
142
144
143
$ resource = openssl_pkey_new ($ config );
145
144
if (false === $ resource ) {
@@ -165,7 +164,7 @@ private function generateKeyPair(?string $passphrase): array
165
164
return [$ privateKey , $ publicKeyData ['key ' ]];
166
165
}
167
166
168
- private function buildOpenSSLConfiguration (): array
167
+ private function buildOpenSSLConfiguration (string $ algorithm ): array
169
168
{
170
169
$ digestAlgorithms = [
171
170
'RS256 ' => 'sha256 ' ,
@@ -208,13 +207,13 @@ private function buildOpenSSLConfiguration(): array
208
207
];
209
208
210
209
$ 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 ],
214
213
];
215
214
216
- if (isset ($ curves [$ this -> algorithm ])) {
217
- $ config ['curve_name ' ] = $ curves [$ this -> algorithm ];
215
+ if (isset ($ curves [$ algorithm ])) {
216
+ $ config ['curve_name ' ] = $ curves [$ algorithm ];
218
217
}
219
218
220
219
return $ config ;
0 commit comments