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\JWKSet; |
19
|
|
|
use Jose\Component\Core\Util\JsonConverter; |
20
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
21
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
22
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
23
|
|
|
|
24
|
|
|
final class RotateKeysetCommand extends ObjectOutputCommand |
25
|
|
|
{ |
26
|
|
|
protected function configure(): void |
27
|
|
|
{ |
28
|
|
|
parent::configure(); |
29
|
|
|
$this |
30
|
|
|
->setName('keyset:rotate') |
31
|
|
|
->setDescription('Rotate a key set.') |
32
|
|
|
->setHelp('This command removes the last key in a key set a place a new one at the beginning.') |
33
|
|
|
->addArgument('jwkset', InputArgument::REQUIRED, 'The JWKSet object') |
34
|
|
|
->addArgument('jwk', InputArgument::REQUIRED, 'The new JWK object') |
35
|
|
|
; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): ?int |
39
|
|
|
{ |
40
|
|
|
$jwkset = $this->getKeyset($input)->all(); |
41
|
|
|
$jwk = $this->getKey($input); |
42
|
|
|
|
43
|
|
|
if (0 !== \count($jwkset)) { |
44
|
|
|
array_pop($jwkset); |
45
|
|
|
} |
46
|
|
|
array_unshift($jwkset, $jwk); |
47
|
|
|
|
48
|
|
|
$this->prepareJsonOutput($input, $output, new JWKSet($jwkset)); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @throws InvalidArgumentException if the keyset is invalid |
53
|
|
|
*/ |
54
|
|
|
private function getKeyset(InputInterface $input): JWKSet |
55
|
|
|
{ |
56
|
|
|
$jwkset = $input->getArgument('jwkset'); |
57
|
|
|
if (!\is_string($jwkset)) { |
58
|
|
|
throw new InvalidArgumentException('Invalid JWKSet'); |
59
|
|
|
} |
60
|
|
|
$json = JsonConverter::decode($jwkset); |
61
|
|
|
if (!\is_array($json)) { |
62
|
|
|
throw new InvalidArgumentException('Invalid JWKSet'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return JWKSet::createFromKeyData($json); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @throws InvalidArgumentException if the key is invalid |
70
|
|
|
*/ |
71
|
|
|
private function getKey(InputInterface $input): JWK |
72
|
|
|
{ |
73
|
|
|
$jwk = $input->getArgument('jwk'); |
74
|
|
|
if (!\is_string($jwk)) { |
75
|
|
|
throw new InvalidArgumentException('Invalid JWK'); |
76
|
|
|
} |
77
|
|
|
$json = JsonConverter::decode($jwk); |
78
|
|
|
if (!\is_string($jwk)) { |
79
|
|
|
throw new InvalidArgumentException('Invalid JWK'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return new JWK($json); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|