1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* The MIT License (MIT) |
7
|
|
|
* |
8
|
|
|
* Copyright (c) 2014-2017 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 Base64Url\Base64Url; |
17
|
|
|
use Jose\Component\KeyManagement\JWKFactory; |
18
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
19
|
|
|
use Symfony\Component\Console\Input\InputOption; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class GeneratorCommand. |
23
|
|
|
*/ |
24
|
|
|
abstract class GeneratorCommand extends ObjectOutputCommand |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* {@inheritdoc} |
28
|
|
|
*/ |
29
|
|
|
public function isEnabled() |
30
|
|
|
{ |
31
|
|
|
return class_exists(JWKFactory::class); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Configures the current command. |
36
|
|
|
*/ |
37
|
|
|
protected function configure() |
38
|
|
|
{ |
39
|
|
|
parent::configure(); |
40
|
|
|
$this |
41
|
|
|
->addOption('use', 'u', InputOption::VALUE_OPTIONAL, 'Usage of the key. Must be either "sig" or "enc".') |
42
|
|
|
->addOption('alg', 'a', InputOption::VALUE_OPTIONAL, 'Algorithm for the key.') |
43
|
|
|
->addOption('random_id', null, InputOption::VALUE_NONE, 'If this option is set, a random key ID (kid) will be generated.') |
44
|
|
|
; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param InputInterface $input |
49
|
|
|
* |
50
|
|
|
* @return array |
51
|
|
|
*/ |
52
|
|
|
protected function getOptions(InputInterface $input): array |
53
|
|
|
{ |
54
|
|
|
$args = []; |
55
|
|
|
if ($input->getOption('random_id')) { |
56
|
|
|
$args['kid'] = $this->generateKeyID(); |
57
|
|
|
} |
58
|
|
|
foreach (['use', 'alg'] as $key) { |
59
|
|
|
$value = $input->getOption($key); |
60
|
|
|
if (null !== $value) { |
61
|
|
|
$args[$key] = $value; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $args; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
|
|
private function generateKeyID(): string |
72
|
|
|
{ |
73
|
|
|
return Base64Url::encode(random_bytes(64)); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|