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 EcKeysetGeneratorCommand extends GeneratorCommand |
24
|
|
|
{ |
25
|
|
|
protected function configure(): void |
26
|
|
|
{ |
27
|
|
|
parent::configure(); |
28
|
|
|
$this |
29
|
|
|
->setName('keyset:generate:ec') |
30
|
|
|
->setDescription('Generate an EC key set (JWKSet format)') |
31
|
|
|
->addArgument('quantity', InputArgument::REQUIRED, 'Quantity of keys in the key set.') |
32
|
|
|
->addArgument('curve', InputArgument::REQUIRED, 'Curve of the keys.') |
33
|
|
|
; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @throws InvalidArgumentException if the quantity of keys is invalid |
38
|
|
|
* @throws InvalidArgumentException if the curve is invalid |
39
|
|
|
*/ |
40
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): ?int |
41
|
|
|
{ |
42
|
|
|
$quantity = (int) $input->getArgument('quantity'); |
43
|
|
|
if ($quantity < 1) { |
44
|
|
|
throw new InvalidArgumentException('Invalid quantity'); |
45
|
|
|
} |
46
|
|
|
$curve = $input->getArgument('curve'); |
47
|
|
|
if (!\is_string($curve)) { |
48
|
|
|
throw new InvalidArgumentException('Invalid curve'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$keyset = new JWKSet([]); |
52
|
|
|
for ($i = 0; $i < $quantity; ++$i) { |
53
|
|
|
$args = $this->getOptions($input); |
54
|
|
|
$keyset = $keyset->with(JWKFactory::createECKey($curve, $args)); |
55
|
|
|
} |
56
|
|
|
$this->prepareJsonOutput($input, $output, $keyset); |
57
|
|
|
|
58
|
|
|
return 0; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|