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 RuntimeException; |
14
|
|
|
use Symfony\Component\Console\Command\Command; |
15
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
16
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
17
|
|
|
use Symfony\Component\Console\Question\Question; |
18
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class KeysGeneratorCommand. |
22
|
|
|
* |
23
|
|
|
* @author Jafar Jabr <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class KeysGeneratorCommand extends Command |
26
|
|
|
{ |
27
|
|
|
protected static $defaultName = 'jafar:generate-keys'; |
28
|
|
|
|
29
|
|
|
private $keysDir; |
30
|
|
|
|
31
|
|
|
public function __construct(string $keys_dir = '') |
32
|
|
|
{ |
33
|
|
|
$this->keysDir = $keys_dir; |
34
|
|
|
parent::__construct(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
|
|
protected function configure() |
41
|
|
|
{ |
42
|
|
|
$this |
43
|
|
|
->setName('jafar:generate-keys') |
44
|
|
|
->setDescription('Generate private and public key for JWT encryption') |
45
|
|
|
->setHelp('Generate password protected private and public key for JWT encryption'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
52
|
|
|
{ |
53
|
|
|
$io = new SymfonyStyle($input, $output); |
54
|
|
|
$io->title('Thank you for using Jafar:GuardedAuthenticationBundle'); |
55
|
|
|
$question = new Question('Please enter passPhrase OpenSSL keys pair? '); |
56
|
|
|
$question->setHidden(true); |
57
|
|
|
$question->setHiddenFallback(false); |
58
|
|
|
$question->setValidator(function ($answer) { |
59
|
|
|
if (strlen($answer) < 6) { |
60
|
|
|
throw new RuntimeException('The passPhrase can not be less than 6 characters'); |
61
|
|
|
} elseif (strlen($answer) > 50) { |
62
|
|
|
throw new RuntimeException('The passPhrase can not be more than 50 characters'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $answer; |
66
|
|
|
}); |
67
|
|
|
$helper = $this->getHelper('question'); |
68
|
|
|
$passPhrase = $helper->ask($input, $output, $question); |
69
|
|
|
$key_directory = $this->prepareTheRoute(); |
70
|
|
|
$privateKey = openssl_pkey_new([ |
71
|
|
|
'private_key_bits' => 4096, |
72
|
|
|
'private_key_type' => OPENSSL_KEYTYPE_RSA, |
73
|
|
|
]); |
74
|
|
|
openssl_pkey_export($privateKey, $privKey, $passPhrase); |
75
|
|
|
$pubKey = openssl_pkey_get_details($privateKey); |
76
|
|
|
$pubKey = $pubKey['key']; |
77
|
|
|
file_put_contents($key_directory.'private.pem', $privKey); |
78
|
|
|
file_put_contents($key_directory.'public.pem', $pubKey); |
79
|
|
|
$output->writeln('<info>private and public keys generated successfully.</info>'); |
80
|
|
|
|
81
|
|
|
return 1; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
|
|
private function prepareTheRoute() |
88
|
|
|
{ |
89
|
|
|
if (!is_dir($this->keysDir) || !is_readable($this->keysDir)) { |
90
|
|
|
mkdir($this->keysDir, 0777); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $this->keysDir; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|