This repository has been archived by the owner on Dec 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 95
/
MergeKeysetCommand.php
71 lines (64 loc) · 2.13 KB
/
MergeKeysetCommand.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
declare(strict_types=1);
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2017 Spomky-Labs
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Jose\Component\Console;
use Jose\Component\Core\Converter\JsonConverterInterface;
use Jose\Component\Core\JWKSet;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Class MergeKeysetCommand.
*/
final class MergeKeysetCommand extends AbstractObjectOutputCommand
{
/**
* KeyAnalyzerCommand constructor.
*
* @param JsonConverterInterface $jsonConverter
* @param string|null $name
*/
public function __construct(JsonConverterInterface $jsonConverter, string $name = null)
{
parent::__construct($jsonConverter, $name);
}
/**
* {@inheritdoc}
*/
protected function configure()
{
parent::configure();
$this
->setName('keyset:merge')
->setDescription('Merge several key sets into one.')
->setHelp('This command merges several key sets into one. It is very useful when you generate e.g. RSA, EC and OKP keys and you want only one key set to rule them all.')
->addArgument('jwksets', InputArgument::REQUIRED | InputArgument::IS_ARRAY, 'The JWKSet objects')
;
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$keySets = $input->getArgument('jwksets');
$newJwkset = JWKSet::createFromKeys([]);
foreach ($keySets as $keySet) {
$json = $this->jsonConverter->decode($keySet);
if (!is_array($json)) {
throw new \InvalidArgumentException('The argument must be a valid JWKSet.');
}
$jwkset = JWKSet::createFromKeyData($json);
foreach ($jwkset->all() as $jwk) {
$newJwkset = $newJwkset->with($jwk);
}
}
$this->prepareJsonOutput($input, $output, $newJwkset);
}
}