Debian Codegraph @

lib/3rdParty/composer/web-token/jwt-library/Console/PublicKeysetCommand.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 use function is_string;
16
17 #[AsCommand(
18 name: 'keyset:convert:public',
19 description: 'Convert private keys in a key set into public keys. Symmetric keys (shared keys) are not changed.',
20 )]
21 final class PublicKeysetCommand extends ObjectOutputCommand
22 {
23 protected static $defaultName = 'keyset:convert:public';
24
25 protected static $defaultDescription = 'Convert private keys in a key set into public keys. Symmetric keys (shared keys) are not changed.';
26
27 protected function configure(): void
28 {
29 parent::configure();
30 $this->setHelp('This command converts private keys in a key set into public keys.')
31 ->addArgument('jwkset', InputArgument::REQUIRED, 'The JWKSet object');
32 }
33
34 protected function execute(InputInterface $input, OutputInterface $output): int
35 {
36 $jwkset = $this->getKeyset($input);
37 $newJwkset = new JWKSet([]);
38
39 foreach ($jwkset->all() as $jwk) {
40 $newJwkset = $newJwkset->with($jwk->toPublic());
41 }
42 $this->prepareJsonOutput($input, $output, $newJwkset);
43
44 return self::SUCCESS;
45 }
46
47 private function getKeyset(InputInterface $input): JWKSet
48 {
49 $jwkset = $input->getArgument('jwkset');
50 if (! is_string($jwkset)) {
51 throw new InvalidArgumentException('Invalid JWKSet');
52 }
53 $json = JsonConverter::decode($jwkset);
54 if (! is_array($json)) {
55 throw new InvalidArgumentException('Invalid JWKSet');
56 }
57
58 return JWKSet::createFromKeyData($json);
59 }
60 }