-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGeneratorCommand.php
60 lines (52 loc) · 1.66 KB
/
GeneratorCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
declare(strict_types=1);
namespace Jose\Component\Console;
use InvalidArgumentException;
use function is_bool;
use Jose\Component\KeyManagement\JWKFactory;
use ParagonIE\ConstantTime\Base64UrlSafe;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
abstract class GeneratorCommand extends ObjectOutputCommand
{
public function isEnabled(): bool
{
return class_exists(JWKFactory::class);
}
protected function configure(): void
{
parent::configure();
$this
->addOption('use', 'u', InputOption::VALUE_OPTIONAL, 'Usage of the key. Must be either "sig" or "enc".')
->addOption('alg', 'a', InputOption::VALUE_OPTIONAL, 'Algorithm for the key.')
->addOption(
'random_id',
null,
InputOption::VALUE_NONE,
'If this option is set, a random key ID (kid) will be generated.'
)
;
}
protected function getOptions(InputInterface $input): array
{
$args = [];
$useRandomId = $input->getOption('random_id');
if (! is_bool($useRandomId)) {
throw new InvalidArgumentException('Invalid value for option "random_id"');
}
if ($useRandomId) {
$args['kid'] = $this->generateKeyID();
}
foreach (['use', 'alg'] as $key) {
$value = $input->getOption($key);
if ($value !== null) {
$args[$key] = $value;
}
}
return $args;
}
private function generateKeyID(): string
{
return Base64UrlSafe::encode(random_bytes(32));
}
}