Passed
Push — master ( 90db0f...396226 )
by Jafar
03:16
created

KeysGeneratorCommand::execute()   B

Complexity

Conditions 3
Paths 1

Size

Total Lines 35
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 26
nc 1
nop 2
dl 0
loc 35
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/*
3
 * This file is part of the Guarded Authentication package.
4
 *
5
 * (c) Jafar Jabr <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Jafar\Bundle\GuardedAuthenticationBundle\Command;
12
13
use Symfony\Component\Console\Command\Command;
14
use Symfony\Component\Console\Input\InputInterface;
15
use Symfony\Component\Console\Output\OutputInterface;
16
use Symfony\Component\Console\Question\Question;
17
use Symfony\Component\Console\Style\SymfonyStyle;
18
19
/**
20
 * Class KeysGeneratorCommand.
21
 *
22
 * @author Jafar Jabr <[email protected]>
23
 */
24
class KeysGeneratorCommand extends Command
25
{
26
    protected static $defaultName = 'jafar:generate-keys';
27
28
    private $keysDir;
29
30
    public function __construct(string $keys_dir = '')
31
    {
32
        $this->keysDir = $keys_dir;
33
        parent::__construct();
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    protected function configure()
40
    {
41
        $this
42
            ->setName('jafar:generate-keys')
43
            ->setDescription('Generate private and public key for JWT encryption')
44
            ->setHelp('Generate password protected private and public key for JWT encryption');
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    protected function execute(InputInterface $input, OutputInterface $output)
51
    {
52
        $io = new SymfonyStyle($input, $output);
53
        $io->title('Thank you for using Jafar:GuardedAuthenticationBundle');
54
        $question = new Question('Please enter passPhrase OpenSSL keys pair? ');
55
        $question->setHidden(true);
56
        $question->setHiddenFallback(false);
57
        $question->setValidator(function ($answer) {
58
            if (strlen($answer) < 6) {
59
                throw new \RuntimeException(
60
                    'The passPhrase can not be less than 6 characters'
61
                );
62
            } elseif (strlen($answer) > 50) {
63
                throw new \RuntimeException(
64
                    'The passPhrase can not be more than 50 characters'
65
                );
66
            }
67
68
            return $answer;
69
        });
70
        $helper        = $this->getHelper('question');
71
        $passPhrase    = $helper->ask($input, $output, $question);
72
        $key_directory = $this->prepareTheRoute();
73
        $privateKey    = openssl_pkey_new([
74
            'private_key_bits' => 4096,
75
            'private_key_type' => OPENSSL_KEYTYPE_RSA,
76
        ]);
77
        openssl_pkey_export($privateKey, $privKey, $passPhrase);
78
        $pubKey = openssl_pkey_get_details($privateKey);
79
        $pubKey = $pubKey['key'];
80
        file_put_contents($key_directory.'private.pem', $privKey);
81
        file_put_contents($key_directory.'public.pem', $pubKey);
82
        $output->writeln('<info>private and public keys generated successfully.</info>');
83
84
        return 0;
85
    }
86
87
    /**
88
     * @return string
89
     */
90
    private function prepareTheRoute()
91
    {
92
        if (!is_dir($this->keysDir) || !is_readable($this->keysDir)) {
93
            mkdir($this->keysDir, 0777);
94
        }
95
96
        return $this->keysDir;
97
    }
98
}
99