Failed Conditions
Push — v7 ( 137ba9...eb2dfc )
by Florent
03:53
created

RsaKeysetGeneratorCommand::execute()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 25
rs 8.439
c 1
b 0
f 1
cc 5
eloc 17
nc 12
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2017 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\Command;
15
16
use Jose\Component\Core\JWKSet;
17
use Jose\Component\KeyManagement\JWKFactory;
18
use Symfony\Component\Console\Command\Command;
19
use Symfony\Component\Console\Input\InputDefinition;
20
use Symfony\Component\Console\Input\InputInterface;
21
use Symfony\Component\Console\Input\InputOption;
22
use Symfony\Component\Console\Output\OutputInterface;
23
24
final class RsaKeysetGeneratorCommand extends Command
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29
    protected function configure()
30
    {
31
        $this
32
            ->setName('keyset:generate:rsa')
33
            ->setDescription('Generate a key set with RSA keys (JWK format)')
34
            ->setDefinition(
35
                new InputDefinition([
36
                    new InputOption('quantity', null, InputOption::VALUE_REQUIRED, 'Quantity of keys in the key set.'),
37
                    new InputOption('size', 's', InputOption::VALUE_OPTIONAL, 'Key size.', 2048),
38
                    new InputOption('use', 'u', InputOption::VALUE_OPTIONAL, 'Usage of the key. Must be either "sig" or "enc".'),
39
                    new InputOption('alg', 'a', InputOption::VALUE_OPTIONAL, 'Algorithm for the key.'),
40
                    new InputOption('out', 'o', InputOption::VALUE_OPTIONAL, 'File where to save the key. Must be a valid and writable file name.'),
41
                ])
42
            )
43
        ;
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    protected function execute(InputInterface $input, OutputInterface $output)
50
    {
51
        $quantity = (int) $input->getOption('quantity');
52
        $size = (int) $input->getOption('size');
53
        $args = [];
54
        foreach (['use', 'alg'] as $key) {
55
            $value = $input->getOption($key);
56
            if (null !== $value) {
57
                $args[$key] = $value;
58
            }
59
        }
60
61
        $keyset = JWKSet::createFromKeys([]);
62
        for ($i = 0 ; $i < $quantity ; ++$i) {
63
            $keyset = $keyset->withKey(JWKFactory::createRSAKey($size, $args));
64
        }
65
        $json = json_encode($keyset);
66
67
        $file = $input->getOption('out');
68
        if (null !== $file) {
69
            file_put_contents($file, $json, LOCK_EX);
70
        } else {
71
            $output->write($json);
72
        }
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function isEnabled()
79
    {
80
        return class_exists('\Jose\Component\KeyManagement\JWKFactory');
81
    }
82
}
83