-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSecretKeyGeneratorCommand.php
57 lines (49 loc) · 1.8 KB
/
SecretKeyGeneratorCommand.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
<?php
declare(strict_types=1);
namespace Jose\Component\Console;
use InvalidArgumentException;
use function is_bool;
use function is_string;
use Jose\Component\KeyManagement\JWKFactory;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
final class SecretKeyGeneratorCommand extends GeneratorCommand
{
protected static $defaultName = 'key:generate:from_secret';
protected function configure(): void
{
parent::configure();
$this->setDescription('Generate an octet key (JWK format) using an existing secret')
->addArgument('secret', InputArgument::REQUIRED, 'The secret')
->addOption(
'is_b64',
'b',
InputOption::VALUE_NONE,
'Indicates if the secret is Base64 encoded (useful for binary secrets)'
)
;
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$secret = $input->getArgument('secret');
if (! is_string($secret)) {
throw new InvalidArgumentException('Invalid secret');
}
$isBsae64Encoded = $input->getOption('is_b64');
if (! is_bool($isBsae64Encoded)) {
throw new InvalidArgumentException('Invalid option value for "is_b64"');
}
if ($isBsae64Encoded) {
$secret = base64_decode($secret, true);
}
if (! is_string($secret)) {
throw new InvalidArgumentException('Invalid secret');
}
$args = $this->getOptions($input);
$jwk = JWKFactory::createFromSecret($secret, $args);
$this->prepareJsonOutput($input, $output, $jwk);
return 0;
}
}