-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerateKey.php
More file actions
150 lines (119 loc) · 4.23 KB
/
GenerateKey.php
File metadata and controls
150 lines (119 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
/**
* This file is part of Blitz PHP framework.
*
* (c) 2022 Dimitri Sitchet Tomkeu <devcode.dst@gmail.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace BlitzPHP\Cli\Commands\Encryption;
use BlitzPHP\Cli\Console\Command;
use BlitzPHP\Loader\DotEnv;
use BlitzPHP\Security\Encryption\Encryption;
/**
* Genere une nouvelle cle d'encryption.
*/
class GenerateKey extends Command
{
/**
* @var string Groupe
*/
protected $group = 'Encryption';
/**
* @var string Nom
*/
protected $name = 'key:generate';
/**
* @var string Description
*/
protected $description = 'Génère une nouvelle clé de chiffrememt et la met dans le fichier `.env`.';
/**
* @var string
*/
protected $service = 'Service de chiffrememt';
/**
* @var array Options
*/
protected $options = [
'--force' => 'Force l\'écrasement de clé existante dans le fichier `.env`.',
'--length' => ['La longueur de la chaîne aléatoire qui doit être retournée en bytes.', 32],
'--prefix' => ['Prefix à ajouter à la clé encodée (doit être hex2bin ou base64).', 'hex2bin'],
'--show' => 'Indique qu\'on souhaite afficher la clé générée dans le terminal après l\'avoir mis dans le fichier `.env`.',
];
/**
* {@inheritDoc}
*/
public function execute(array $params)
{
$prefix = $params['prefix'] ?? null;
if (in_array($prefix, [null, true], true)) {
$prefix = 'hex2bin';
} elseif (! in_array($prefix, ['hex2bin', 'base64'], true)) {
$prefix = $this->choice('Veuillez utiliser un prefixe validee.', ['hex2bin', 'base64']); // @codeCoverageIgnore
}
$length = $params['length'] ?? null;
if (in_array($length, [null, true], true)) {
$length = 32;
}
$this->task('Génération d\'une nouvelle clé de chiffrememt');
$encodedKey = $this->generateRandomKey($prefix, $length);
if ($this->option('show')) {
$this->writer->warn($encodedKey, true);
return;
}
if (! $this->setNewEncryptionKey($encodedKey)) {
$this->writer->error('Erreur dans la configuration d\'une nouvelle clé de chiffrememt dans le fichier `.env`.', true);
return;
}
$this->success('Une nouvelle clé de chiffrement de l\'application a été définie avec succès.');
}
/**
* Genere une cle et l'encode.
*/
protected function generateRandomKey(string $prefix, int $length): string
{
$key = Encryption::createKey($length);
if ($prefix === 'hex2bin') {
return 'hex2bin:' . bin2hex($key);
}
return 'base64:' . base64_encode($key);
}
/**
* Definit la nouvelle cle d'encryption dans le fichier .env
*/
protected function setNewEncryptionKey(string $key): bool
{
$currentKey = env('encryption.key', '');
if ($currentKey !== '' && ! $this->confirmOverwrite()) {
// Pas testable car require une entree au clavier
return false; // @codeCoverageIgnore
}
return $this->writeNewEncryptionKeyToFile($key);
}
/**
* Verifie si on doit ecraser la cle d'encryption existante.
*/
protected function confirmOverwrite(): bool
{
return $this->option('force') || $this->confirm('Voulez-vous modifier la clé existante ?');
}
/**
* Writes the new encryption key to .env file.
*/
protected function writeNewEncryptionKeyToFile(string $key): bool
{
$baseEnv = ROOTPATH . '.env.example';
$envFile = ROOTPATH . '.env';
if (! is_file($envFile)) {
if (! is_file($baseEnv)) {
$this->writer->warn('Le fichier `.env.example` livré par défaut et le fichier `.env` personnalisé sont manquants.');
$this->eol()->write('Voici votre nouvelle clé à la place: ');
$this->writer->warn($key, true);
return false;
}
copy($baseEnv, $envFile);
}
return DotEnv::instance()->replace(['encryption.key' => $key]);
}
}