|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace AbterPhp\Framework\Console\Commands\Oauth2; |
|
6
|
|
|
|
|
7
|
|
|
use Opulence\Console\Commands\Command; |
|
8
|
|
|
use Opulence\Console\Responses\IResponse; |
|
9
|
|
|
|
|
10
|
|
|
class GenerateKeys extends Command |
|
11
|
|
|
{ |
|
12
|
|
|
public const NAME = 'oauth2:generatekeys'; |
|
13
|
|
|
|
|
14
|
|
|
protected string $privateKeyPassword; |
|
15
|
|
|
protected string $privateKeyPath; |
|
16
|
|
|
protected string $publicKeyPath; |
|
17
|
|
|
|
|
18
|
|
|
protected bool $isOpenSslAvailable = true; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* GenerateKeys constructor. |
|
22
|
|
|
* |
|
23
|
|
|
* @param string $privateKeyPassword |
|
24
|
|
|
* @param string $privateKeyPath |
|
25
|
|
|
* @param string $publicKeyPath |
|
26
|
|
|
*/ |
|
27
|
|
|
public function __construct(string $privateKeyPassword, string $privateKeyPath, string $publicKeyPath) |
|
28
|
|
|
{ |
|
29
|
|
|
$this->privateKeyPassword = $privateKeyPassword; |
|
30
|
|
|
$this->privateKeyPath = $privateKeyPath; |
|
31
|
|
|
$this->publicKeyPath = $publicKeyPath; |
|
32
|
|
|
|
|
33
|
|
|
if (!defined('OPENSSL_VERSION_NUMBER')) { |
|
34
|
|
|
$this->isOpenSslAvailable = false; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
parent::__construct(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param bool $isOpenSslAvailable |
|
42
|
|
|
* |
|
43
|
|
|
* @return $this |
|
44
|
|
|
*/ |
|
45
|
|
|
public function setIsOpenSslAvailable(bool $isOpenSslAvailable): self |
|
46
|
|
|
{ |
|
47
|
|
|
$this->isOpenSslAvailable = $isOpenSslAvailable; |
|
48
|
|
|
|
|
49
|
|
|
return $this; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @inheritdoc |
|
54
|
|
|
*/ |
|
55
|
|
|
protected function define(): void |
|
56
|
|
|
{ |
|
57
|
|
|
$this->setName(static::NAME) |
|
58
|
|
|
->setDescription('Generates openssl keys'); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @inheritdoc |
|
63
|
|
|
*/ |
|
64
|
|
|
protected function doExecute(IResponse $response): void |
|
65
|
|
|
{ |
|
66
|
|
|
if (!$this->isOpenSslAvailable) { |
|
67
|
|
|
$response->writeln('<fatal>OpenSSL is not installed.</fatal>'); |
|
68
|
|
|
|
|
69
|
|
|
return; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
$genPrivateKeyCmd = sprintf( |
|
73
|
|
|
'openssl genrsa -passout pass:%s -out %s 2048 2> /dev/null', |
|
74
|
|
|
$this->privateKeyPassword, |
|
75
|
|
|
$this->privateKeyPath |
|
76
|
|
|
); |
|
77
|
|
|
$response->writeln(sprintf('<comment>%s</comment>', $genPrivateKeyCmd)); |
|
78
|
|
|
exec($genPrivateKeyCmd); |
|
79
|
|
|
|
|
80
|
|
|
$genPublicKeyCmd = sprintf( |
|
81
|
|
|
'openssl rsa -in %s -passin pass:%s -pubout -out %s 2> /dev/null', |
|
82
|
|
|
$this->privateKeyPath, |
|
83
|
|
|
$this->privateKeyPassword, |
|
84
|
|
|
$this->publicKeyPath |
|
85
|
|
|
); |
|
86
|
|
|
$response->writeln(sprintf('<comment>%s</comment>', $genPublicKeyCmd)); |
|
87
|
|
|
exec($genPublicKeyCmd); |
|
88
|
|
|
|
|
89
|
|
|
chmod($this->privateKeyPath, 0600); |
|
90
|
|
|
chmod($this->publicKeyPath, 0600); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|