Debian Codegraph @

lib/3rdParty/composer/web-token/jwt-library/Console/KeyAnalyzerCommand.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\JWK;
9 use Jose\Component\Core\Util\JsonConverter;
10 use Jose\Component\KeyManagement\Analyzer\KeyAnalyzerManager;
11 use Symfony\Component\Console\Attribute\AsCommand;
12 use Symfony\Component\Console\Command\Command;
13 use Symfony\Component\Console\Formatter\OutputFormatterStyle;
14 use Symfony\Component\Console\Input\InputArgument;
15 use Symfony\Component\Console\Input\InputInterface;
16 use Symfony\Component\Console\Output\OutputInterface;
17 use function is_array;
18 use function is_string;
19
20 #[AsCommand(name: 'key:analyze', description: 'JWK quality analyzer.',)]
21 final class KeyAnalyzerCommand extends Command
22 {
23 protected static $defaultName = 'key:analyze';
24
25 protected static $defaultDescription = 'JWK quality analyzer.';
26
27 public function __construct(
28 private readonly KeyAnalyzerManager $analyzerManager,
29 ?string $name = null
30 ) {
31 parent::__construct($name);
32 }
33
34 protected function configure(): void
35 {
36 parent::configure();
37 $this->setHelp('This command will analyze a JWK object and find security issues.')
38 ->addArgument('jwk', InputArgument::REQUIRED, 'The JWK object');
39 }
40
41 protected function execute(InputInterface $input, OutputInterface $output): int
42 {
43 $output->getFormatter()
44 ->setStyle('success', new OutputFormatterStyle('white', 'green'));
45 $output->getFormatter()
46 ->setStyle('high', new OutputFormatterStyle('white', 'red', ['bold']));
47 $output->getFormatter()
48 ->setStyle('medium', new OutputFormatterStyle('yellow'));
49 $output->getFormatter()
50 ->setStyle('low', new OutputFormatterStyle('blue'));
51 $jwk = $this->getKey($input);
52
53 $result = $this->analyzerManager->analyze($jwk);
54 if ($result->count() === 0) {
55 $output->writeln('<success>All good! No issue found.</success>');
56 } else {
57 foreach ($result->all() as $message) {
58 $output->writeln(
59 '<' . $message->getSeverity() . '>* ' . $message->getMessage() . '</' . $message->getSeverity() . '>'
60 );
61 }
62 }
63
64 return self::SUCCESS;
65 }
66
67 private function getKey(InputInterface $input): JWK
68 {
69 $jwk = $input->getArgument('jwk');
70 if (! is_string($jwk)) {
71 throw new InvalidArgumentException('Invalid JWK');
72 }
73 $json = JsonConverter::decode($jwk);
74 if (! is_array($json)) {
75 throw new InvalidArgumentException('Invalid JWK.');
76 }
77
78 return new JWK($json);
79 }
80 }