1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* The MIT License (MIT) |
7
|
|
|
* |
8
|
|
|
* Copyright (c) 2014-2019 Spomky-Labs |
9
|
|
|
* |
10
|
|
|
* This software may be modified and distributed under the terms |
11
|
|
|
* of the MIT license. See the LICENSE file for details. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Jose\Component\Console; |
15
|
|
|
|
16
|
|
|
use InvalidArgumentException; |
17
|
|
|
use Jose\Component\Core\JWK; |
18
|
|
|
use Jose\Component\Core\Util\JsonConverter; |
19
|
|
|
use Jose\Component\KeyManagement\Analyzer\KeyAnalyzerManager; |
20
|
|
|
use Symfony\Component\Console\Command\Command; |
21
|
|
|
use Symfony\Component\Console\Formatter\OutputFormatterStyle; |
22
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
23
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
24
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
25
|
|
|
|
26
|
|
|
final class KeyAnalyzerCommand extends Command |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var KeyAnalyzerManager |
30
|
|
|
*/ |
31
|
|
|
private $analyzerManager; |
32
|
|
|
|
33
|
|
|
public function __construct(KeyAnalyzerManager $keysetAnalyzerManager, string $name = null) |
34
|
|
|
{ |
35
|
|
|
parent::__construct($name); |
36
|
|
|
$this->analyzerManager = $keysetAnalyzerManager; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
protected function configure(): void |
40
|
|
|
{ |
41
|
|
|
parent::configure(); |
42
|
|
|
$this |
43
|
|
|
->setName('key:analyze') |
44
|
|
|
->setDescription('JWK quality analyzer.') |
45
|
|
|
->setHelp('This command will analyze a JWK object and find security issues.') |
46
|
|
|
->addArgument('jwk', InputArgument::REQUIRED, 'The JWK object') |
47
|
|
|
; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): ?int |
51
|
|
|
{ |
52
|
|
|
$output->getFormatter()->setStyle('success', new OutputFormatterStyle('white', 'green')); |
53
|
|
|
$output->getFormatter()->setStyle('high', new OutputFormatterStyle('white', 'red', ['bold'])); |
54
|
|
|
$output->getFormatter()->setStyle('medium', new OutputFormatterStyle('yellow')); |
55
|
|
|
$output->getFormatter()->setStyle('low', new OutputFormatterStyle('blue')); |
56
|
|
|
$jwk = $this->getKey($input); |
57
|
|
|
|
58
|
|
|
$result = $this->analyzerManager->analyze($jwk); |
59
|
|
|
if (0 === $result->count()) { |
60
|
|
|
$output->writeln('<success>All good! No issue found.</success>'); |
61
|
|
|
} else { |
62
|
|
|
foreach ($result->all() as $message) { |
63
|
|
|
$output->writeln('<'.$message->getSeverity().'>* '.$message->getMessage().'</'.$message->getSeverity().'>'); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return 0; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @throws InvalidArgumentException if the key is invalid |
72
|
|
|
*/ |
73
|
|
|
private function getKey(InputInterface $input): JWK |
74
|
|
|
{ |
75
|
|
|
$jwk = $input->getArgument('jwk'); |
76
|
|
|
if (!\is_string($jwk)) { |
77
|
|
|
throw new InvalidArgumentException('Invalid JWK'); |
78
|
|
|
} |
79
|
|
|
$json = JsonConverter::decode($jwk); |
80
|
|
|
if (!\is_array($json)) { |
81
|
|
|
throw new InvalidArgumentException('Invalid JWK.'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return new JWK($json); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|