|
1
|
|
|
<?php |
|
2
|
|
|
namespace RichardStyles\EloquentAES\Command; |
|
3
|
|
|
|
|
4
|
|
|
use Illuminate\Console\Command; |
|
5
|
|
|
use Illuminate\Console\ConfirmableTrait; |
|
6
|
|
|
use Illuminate\Encryption\Encrypter; |
|
7
|
|
|
use Illuminate\Support\Str; |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
class KeyGenerateCommand extends Command |
|
11
|
|
|
{ |
|
12
|
|
|
use ConfirmableTrait; |
|
13
|
|
|
/** |
|
14
|
|
|
* The name and signature of the console command. |
|
15
|
|
|
* |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $signature = 'key:eloquent |
|
19
|
|
|
{--show : Display the key instead of modifying files} |
|
20
|
|
|
{--force : Force the operation to run when in production}'; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* The console command description. |
|
24
|
|
|
* |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $description = 'Set the eloquent key'; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Execute the console command. |
|
31
|
|
|
* |
|
32
|
|
|
* @return void |
|
33
|
|
|
*/ |
|
34
|
|
|
public function handle() |
|
35
|
|
|
{ |
|
36
|
|
|
$key = $this->generateRandomKey(); |
|
37
|
|
|
|
|
38
|
|
|
if ($this->option('show')) { |
|
39
|
|
|
return $this->line('<comment>'.$key.'</comment>'); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
// Next, we will replace the application key in the environment file so it is |
|
43
|
|
|
// automatically setup for this developer. This key gets generated using a |
|
44
|
|
|
// secure random byte generator and is later base64 encoded for storage. |
|
45
|
|
|
if (! $this->setKeyInEnvironmentFile($key)) { |
|
46
|
|
|
return; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
$this->laravel['config']['eloquentaes.key'] = $key; |
|
50
|
|
|
|
|
51
|
|
|
$this->info('Eloquent key set successfully.'); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Generate a random key for the application. |
|
56
|
|
|
* |
|
57
|
|
|
* @return string |
|
58
|
|
|
*/ |
|
59
|
|
|
protected function generateRandomKey() |
|
60
|
|
|
{ |
|
61
|
|
|
return 'base64:'.base64_encode( |
|
62
|
|
|
Encrypter::generateKey($this->laravel['config']['eloquentaes.cipher']) |
|
63
|
|
|
); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Set the application key in the environment file. |
|
68
|
|
|
* |
|
69
|
|
|
* @param string $key |
|
70
|
|
|
* @return bool |
|
71
|
|
|
*/ |
|
72
|
|
|
protected function setKeyInEnvironmentFile($key) |
|
73
|
|
|
{ |
|
74
|
|
|
$currentKey = $this->laravel['config']['eloquentaes.key']; |
|
75
|
|
|
|
|
76
|
|
|
if (strlen($currentKey) !== 0 && (! $this->confirmToProceed())) { |
|
77
|
|
|
return false; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$this->writeNewEnvironmentFileWith($key); |
|
81
|
|
|
|
|
82
|
|
|
return true; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
protected function environmentFileWithExists() |
|
86
|
|
|
{ |
|
87
|
|
|
return preg_match( |
|
88
|
|
|
$this->keyReplacementPattern(), |
|
89
|
|
|
file_get_contents($this->laravel->environmentFilePath()) |
|
|
|
|
|
|
90
|
|
|
); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Write a new environment file with the given key. |
|
95
|
|
|
* |
|
96
|
|
|
* @param string $key |
|
97
|
|
|
* @return void |
|
98
|
|
|
*/ |
|
99
|
|
|
protected function writeNewEnvironmentFileWith($key) |
|
100
|
|
|
{ |
|
101
|
|
|
if($this->environmentFileWithExists()) { |
|
102
|
|
|
file_put_contents($this->laravel->environmentFilePath(), preg_replace( |
|
|
|
|
|
|
103
|
|
|
$this->keyReplacementPattern(), |
|
104
|
|
|
'ELOQUENT_KEY=' . $key, |
|
105
|
|
|
file_get_contents($this->laravel->environmentFilePath()) |
|
|
|
|
|
|
106
|
|
|
)); |
|
107
|
|
|
|
|
108
|
|
|
return; |
|
109
|
|
|
} |
|
110
|
|
|
file_put_contents($this->laravel->environmentFilePath(), |
|
|
|
|
|
|
111
|
|
|
file_get_contents($this->laravel->environmentFilePath()) . PHP_EOL . |
|
|
|
|
|
|
112
|
|
|
'# You should backup this key in a safe secure place' . PHP_EOL . |
|
113
|
|
|
'ELOQUENT_KEY=' . $key . PHP_EOL |
|
114
|
|
|
); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Get a regex pattern that will match env APP_KEY with any random key. |
|
119
|
|
|
* |
|
120
|
|
|
* @return string |
|
121
|
|
|
*/ |
|
122
|
|
|
protected function keyReplacementPattern() |
|
123
|
|
|
{ |
|
124
|
|
|
$escaped = preg_quote('='.$this->laravel['config']['eloquentaes.key'], '/'); |
|
125
|
|
|
|
|
126
|
|
|
return "/^ELOQUENT_KEY{$escaped}/m"; |
|
127
|
|
|
} |
|
128
|
|
|
} |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.