Debian Codegraph @

lib/3rdParty/composer/web-token/jwt-library/Console/SecretKeyGeneratorCommand.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\KeyManagement\JWKFactory;
9 use Symfony\Component\Console\Attribute\AsCommand;
10 use Symfony\Component\Console\Input\InputArgument;
11 use Symfony\Component\Console\Input\InputInterface;
12 use Symfony\Component\Console\Input\InputOption;
13 use Symfony\Component\Console\Output\OutputInterface;
14 use function is_bool;
15 use function is_string;
16
17 #[AsCommand(
18 name: 'key:generate:from_secret',
19 description: 'Generate an octet key (JWK format) using an existing secret',
20 )]
21 final class SecretKeyGeneratorCommand extends GeneratorCommand
22 {
23 protected static $defaultName = 'key:generate:from_secret';
24
25 protected static $defaultDescription = 'Generate an octet key (JWK format) using an existing secret';
26
27 protected function configure(): void
28 {
29 parent::configure();
30 $this->addArgument('secret', InputArgument::REQUIRED, 'The secret')
31 ->addOption(
32 'is_b64',
33 'b',
34 InputOption::VALUE_NONE,
35 'Indicates if the secret is Base64 encoded (useful for binary secrets)'
36 );
37 }
38
39 protected function execute(InputInterface $input, OutputInterface $output): int
40 {
41 $secret = $input->getArgument('secret');
42 if (! is_string($secret)) {
43 throw new InvalidArgumentException('Invalid secret');
44 }
45 $isBsae64Encoded = $input->getOption('is_b64');
46 if (! is_bool($isBsae64Encoded)) {
47 throw new InvalidArgumentException('Invalid option value for "is_b64"');
48 }
49 if ($isBsae64Encoded) {
50 $secret = base64_decode($secret, true);
51 }
52 if (! is_string($secret)) {
53 throw new InvalidArgumentException('Invalid secret');
54 }
55 $args = $this->getOptions($input);
56
57 $jwk = JWKFactory::createFromSecret($secret, $args);
58 $this->prepareJsonOutput($input, $output, $jwk);
59
60 return self::SUCCESS;
61 }
62 }