Debian Codegraph @

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