Repository navigation
Expand file tree
/
Copy pathAddKeyIntoKeysetCommand.php
More file actions
71 lines (61 loc) · 2.25 KB
/
Copy pathAddKeyIntoKeysetCommand.php
File metadata and controls
71 lines (61 loc) · 2.25 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
declare(strict_types=1);
namespace Jose\Component\Console;
use Jose\Component\Core\Exception\InvalidKeyException;
use Jose\Component\Core\Exception\InvalidKeySetException;
use Jose\Component\Core\JWK;
use Jose\Component\Core\JWKSet;
use Jose\Component\Core\Util\JsonConverter;
use Override;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use function is_array;
use function is_string;
#[AsCommand(name: 'keyset:add:key', description: 'Add a key into a key set.')]
final class AddKeyIntoKeysetCommand extends ObjectOutputCommand
{
#[Override]
protected function configure(): void
{
parent::configure();
$this
->setHelp('This command adds a key at the end of a key set.')
->addArgument('jwkset', InputArgument::REQUIRED, 'The JWKSet object')
->addArgument('jwk', InputArgument::REQUIRED, 'The new JWK object');
}
#[Override]
protected function execute(InputInterface $input, OutputInterface $output): int
{
$jwkset = $this->getKeyset($input);
$jwk = $this->getKey($input);
$jwkset = $jwkset->with($jwk);
$this->prepareJsonOutput($input, $output, $jwkset);
return self::SUCCESS;
}
private function getKeyset(InputInterface $input): JWKSet
{
$jwkset = $input->getArgument('jwkset');
if (! is_string($jwkset)) {
throw new InvalidKeySetException('The argument must be a valid JWKSet.');
}
$json = JsonConverter::decode($jwkset, true);
if (! is_array($json)) {
throw new InvalidKeySetException('The argument must be a valid JWKSet.');
}
return JWKSet::createFromKeyData($json);
}
private function getKey(InputInterface $input): JWK
{
$jwk = $input->getArgument('jwk');
if (! is_string($jwk)) {
throw new InvalidKeyException('The argument must be a valid JWK.');
}
$json = JsonConverter::decode($jwk, true);
if (! is_array($json)) {
throw new InvalidKeyException('The argument must be a valid JWK.');
}
return new JWK($json);
}
}