|
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\Core\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
|
|
|
abstract class AbstractGeneratorCommand extends Command |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* {@inheritdoc} |
|
27
|
|
|
*/ |
|
28
|
|
|
public function isEnabled() |
|
29
|
|
|
{ |
|
30
|
|
|
return class_exists(JWKFactory::class); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Configures the current command. |
|
35
|
|
|
*/ |
|
36
|
|
|
protected function configure() |
|
37
|
|
|
{ |
|
38
|
|
|
$this |
|
39
|
|
|
->setName('key:generate:ec') |
|
40
|
|
|
->setDescription('Generate an EC key (JWK format)') |
|
41
|
|
|
->setDefinition( |
|
42
|
|
|
new InputDefinition([ |
|
43
|
|
|
new InputOption('use', 'u', InputOption::VALUE_OPTIONAL, 'Usage of the key. Must be either "sig" or "enc".'), |
|
44
|
|
|
new InputOption('alg', 'a', InputOption::VALUE_OPTIONAL, 'Algorithm for the key.'), |
|
45
|
|
|
new InputOption('out', 'o', InputOption::VALUE_OPTIONAL, 'File where to save the key. Must be a valid and writable file name.'), |
|
46
|
|
|
]) |
|
47
|
|
|
) |
|
48
|
|
|
; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param InputInterface $input |
|
53
|
|
|
* |
|
54
|
|
|
* @return array |
|
55
|
|
|
*/ |
|
56
|
|
|
protected function getOptions(InputInterface $input): array |
|
57
|
|
|
{ |
|
58
|
|
|
$args = []; |
|
59
|
|
|
foreach (['use', 'alg'] as $key) { |
|
60
|
|
|
$value = $input->getOption($key); |
|
61
|
|
|
if (null !== $value) { |
|
62
|
|
|
$args[$key] = $value; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return $args; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param InputInterface $input |
|
71
|
|
|
* @param OutputInterface $output |
|
72
|
|
|
* @param string $json |
|
73
|
|
|
*/ |
|
74
|
|
|
protected function prepareOutput(InputInterface $input, OutputInterface $output, string $json) |
|
75
|
|
|
{ |
|
76
|
|
|
$file = $input->getOption('out'); |
|
77
|
|
|
if (null !== $file) { |
|
78
|
|
|
file_put_contents($file, $json, LOCK_EX); |
|
79
|
|
|
} else { |
|
80
|
|
|
$output->write($json); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|