Debian Codegraph @

lib/3rdParty/composer/web-token/jwt-library/Console/MergeKeysetCommand.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Jose\Component\Console;
6
7 use InvalidArgumentException;
8 use Jose\Component\Core\JWKSet;
9 use Jose\Component\Core\Util\JsonConverter;
10 use Symfony\Component\Console\Attribute\AsCommand;
11 use Symfony\Component\Console\Input\InputArgument;
12 use Symfony\Component\Console\Input\InputInterface;
13 use Symfony\Component\Console\Output\OutputInterface;
14 use function is_array;
15
16 #[AsCommand(name: 'keyset:merge', description: 'Merge several key sets into one.',)]
17 final class MergeKeysetCommand extends ObjectOutputCommand
18 {
19 protected static $defaultName = 'keyset:merge';
20
21 protected static $defaultDescription = 'Merge several key sets into one.';
22
23 protected function configure(): void
24 {
25 parent::configure();
26 $this->setHelp(
27 '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.'
28 )
29 ->addArgument('jwksets', InputArgument::REQUIRED | InputArgument::IS_ARRAY, 'The JWKSet objects');
30 }
31
32 protected function execute(InputInterface $input, OutputInterface $output): int
33 {
34 /** @var string[] $keySets */
35 $keySets = $input->getArgument('jwksets');
36 $newJwkset = new JWKSet([]);
37 foreach ($keySets as $keySet) {
38 $json = JsonConverter::decode($keySet);
39 if (! is_array($json)) {
40 throw new InvalidArgumentException('The argument must be a valid JWKSet.');
41 }
42 $jwkset = JWKSet::createFromKeyData($json);
43 foreach ($jwkset->all() as $jwk) {
44 $newJwkset = $newJwkset->with($jwk);
45 }
46 }
47 $this->prepareJsonOutput($input, $output, $newJwkset);
48
49 return self::SUCCESS;
50 }
51 }