1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* The MIT License (MIT) |
7
|
|
|
* |
8
|
|
|
* Copyright (c) 2014-2019 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 InvalidArgumentException; |
17
|
|
|
use Jose\Component\Core\JWK; |
18
|
|
|
use Jose\Component\Core\Util\JsonConverter; |
19
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
20
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
21
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
22
|
|
|
|
23
|
|
|
final class PublicKeyCommand extends ObjectOutputCommand |
24
|
|
|
{ |
25
|
|
|
protected function configure(): void |
26
|
|
|
{ |
27
|
|
|
parent::configure(); |
28
|
|
|
$this |
29
|
|
|
->setName('key:convert:public') |
30
|
|
|
->setDescription('Convert a private key into public key. Symmetric keys (shared keys) are not changed.') |
31
|
|
|
->setHelp('This command converts a private key into a public key.') |
32
|
|
|
->addArgument('jwk', InputArgument::REQUIRED, 'The JWK object') |
33
|
|
|
; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): ?int |
37
|
|
|
{ |
38
|
|
|
$jwk = $this->getKey($input); |
39
|
|
|
$jwk = $jwk->toPublic(); |
40
|
|
|
|
41
|
|
|
$this->prepareJsonOutput($input, $output, $jwk); |
42
|
|
|
|
43
|
|
|
return 0; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @throws InvalidArgumentException if the key is invalid |
48
|
|
|
*/ |
49
|
|
|
private function getKey(InputInterface $input): JWK |
50
|
|
|
{ |
51
|
|
|
$jwk = $input->getArgument('jwk'); |
52
|
|
|
if (!\is_string($jwk)) { |
53
|
|
|
throw new InvalidArgumentException('Invalid JWK'); |
54
|
|
|
} |
55
|
|
|
$json = JsonConverter::decode($jwk); |
56
|
|
|
if (!\is_array($json)) { |
57
|
|
|
throw new InvalidArgumentException('Invalid JWK'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return new JWK($json); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|