1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* The MIT License (MIT) |
7
|
|
|
* |
8
|
|
|
* Copyright (c) 2014-2019 Spomky-Labs |
9
|
|
|
* |
10
|
|
|
* This software may be modified and distributed under the terms |
11
|
|
|
* of the MIT license. See the LICENSE file for details. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Jose\Component\Console; |
15
|
|
|
|
16
|
|
|
use InvalidArgumentException; |
17
|
|
|
use Jose\Component\Core\JWKSet; |
18
|
|
|
use Jose\Component\KeyManagement\JWKFactory; |
19
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
20
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
21
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
22
|
|
|
|
23
|
|
|
final class RsaKeysetGeneratorCommand extends GeneratorCommand |
24
|
|
|
{ |
25
|
|
|
protected function configure(): void |
26
|
|
|
{ |
27
|
|
|
parent::configure(); |
28
|
|
|
$this |
29
|
|
|
->setName('keyset:generate:rsa') |
30
|
|
|
->setDescription('Generate a key set with RSA keys (JWK format)') |
31
|
|
|
->addArgument('quantity', InputArgument::REQUIRED, 'Quantity of keys in the key set.') |
32
|
|
|
->addArgument('size', InputArgument::REQUIRED, 'Key size.') |
33
|
|
|
; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @throws InvalidArgumentException if the quantity is invalid |
38
|
|
|
* @throws InvalidArgumentException if the key size is invalid |
39
|
|
|
*/ |
40
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): ?int |
41
|
|
|
{ |
42
|
|
|
$quantity = (int) $input->getArgument('quantity'); |
43
|
|
|
$size = (int) $input->getArgument('size'); |
44
|
|
|
if ($quantity < 1) { |
45
|
|
|
throw new InvalidArgumentException('Invalid quantity'); |
46
|
|
|
} |
47
|
|
|
if ($size < 1) { |
48
|
|
|
throw new InvalidArgumentException('Invalid size'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$keyset = new JWKSet([]); |
52
|
|
|
for ($i = 0; $i < $quantity; ++$i) { |
53
|
|
|
$args = $this->getOptions($input); |
54
|
|
|
$keyset = $keyset->with(JWKFactory::createRSAKey($size, $args)); |
55
|
|
|
} |
56
|
|
|
$this->prepareJsonOutput($input, $output, $keyset); |
57
|
|
|
|
58
|
|
|
return 0; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|