|
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\Command; |
|
15
|
|
|
|
|
16
|
|
|
use Jose\Component\KeyManagement\JWKFactory; |
|
17
|
|
|
use Symfony\Component\Console\Command\Command; |
|
18
|
|
|
use Symfony\Component\Console\Input\InputDefinition; |
|
19
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
20
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
21
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
22
|
|
|
|
|
23
|
|
|
final class OkpKeyGeneratorCommand extends Command |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* {@inheritdoc} |
|
27
|
|
|
*/ |
|
28
|
|
|
protected function configure() |
|
29
|
|
|
{ |
|
30
|
|
|
$this |
|
31
|
|
|
->setName('key:generate:okp') |
|
32
|
|
|
->setDescription('Generate an Octet Key Pair key (JWK format)') |
|
33
|
|
|
->setDefinition( |
|
34
|
|
|
new InputDefinition([ |
|
35
|
|
|
new InputOption('curve', 'c', InputOption::VALUE_REQUIRED, 'Curve of the key.'), |
|
36
|
|
|
new InputOption('use', 'u', InputOption::VALUE_OPTIONAL, 'Usage of the key. Must be either "sig" or "enc".'), |
|
37
|
|
|
new InputOption('alg', 'a', InputOption::VALUE_OPTIONAL, 'Algorithm for the key.'), |
|
38
|
|
|
new InputOption('out', 'o', InputOption::VALUE_OPTIONAL, 'File where to save the key. Must be a valid and writable file name.'), |
|
39
|
|
|
]) |
|
40
|
|
|
) |
|
41
|
|
|
; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* {@inheritdoc} |
|
46
|
|
|
*/ |
|
47
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
48
|
|
|
{ |
|
49
|
|
|
$curve = $input->getOption('curve'); |
|
50
|
|
|
$args = []; |
|
51
|
|
|
foreach (['use', 'alg'] as $key) { |
|
52
|
|
|
$value = $input->getOption($key); |
|
53
|
|
|
if (null !== $value) { |
|
54
|
|
|
$args[$key] = $value; |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$jwk = JWKFactory::createOKPKey($curve, $args); |
|
59
|
|
|
$json = json_encode($jwk); |
|
60
|
|
|
|
|
61
|
|
|
$file = $input->getOption('out'); |
|
62
|
|
|
if (null !== $file) { |
|
63
|
|
|
file_put_contents($file, $json, LOCK_EX); |
|
64
|
|
|
} else { |
|
65
|
|
|
$output->write($json); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* {@inheritdoc} |
|
71
|
|
|
*/ |
|
72
|
|
|
public function isEnabled() |
|
73
|
|
|
{ |
|
74
|
|
|
return class_exists('\Jose\Component\KeyManagement\JWKFactory'); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|