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\JWKSet; |
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 PublicKeysetCommand extends ObjectOutputCommand |
24
|
|
|
{ |
25
|
|
|
protected function configure(): void |
26
|
|
|
{ |
27
|
|
|
parent::configure(); |
28
|
|
|
$this |
29
|
|
|
->setName('keyset:convert:public') |
30
|
|
|
->setDescription('Convert private keys in a key set into public keys. Symmetric keys (shared keys) are not changed.') |
31
|
|
|
->setHelp('This command converts private keys in a key set into public keys.') |
32
|
|
|
->addArgument('jwkset', InputArgument::REQUIRED, 'The JWKSet object') |
33
|
|
|
; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): ?int |
37
|
|
|
{ |
38
|
|
|
$jwkset = $this->getKeyset($input); |
39
|
|
|
$newJwkset = new JWKSet([]); |
40
|
|
|
|
41
|
|
|
foreach ($jwkset->all() as $jwk) { |
42
|
|
|
$newJwkset = $newJwkset->with($jwk->toPublic()); |
43
|
|
|
} |
44
|
|
|
$this->prepareJsonOutput($input, $output, $newJwkset); |
45
|
|
|
|
46
|
|
|
return 0; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @throws InvalidArgumentException if the keyset is invalid |
51
|
|
|
*/ |
52
|
|
|
private function getKeyset(InputInterface $input): JWKSet |
53
|
|
|
{ |
54
|
|
|
$jwkset = $input->getArgument('jwkset'); |
55
|
|
|
if (!\is_string($jwkset)) { |
56
|
|
|
throw new InvalidArgumentException('Invalid JWKSet'); |
57
|
|
|
} |
58
|
|
|
$json = JsonConverter::decode($jwkset); |
59
|
|
|
if (!\is_array($json)) { |
60
|
|
|
throw new InvalidArgumentException('Invalid JWKSet'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return JWKSet::createFromKeyData($json); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|