1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace RichardStyles\EloquentAES; |
4
|
|
|
|
5
|
|
|
use Illuminate\Encryption\Encrypter; |
6
|
|
|
use Illuminate\Encryption\EncryptionServiceProvider; |
7
|
|
|
use Illuminate\Encryption\MissingAppKeyException; |
8
|
|
|
use Opis\Closure\SerializableClosure; |
9
|
|
|
use RichardStyles\EloquentAES\Command\KeyGenerateCommand; |
10
|
|
|
|
11
|
|
|
class EloquentAESServiceProvider extends EncryptionServiceProvider |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Bootstrap the application services. |
15
|
|
|
*/ |
16
|
|
|
public function boot() |
17
|
|
|
{ |
18
|
|
|
if ($this->app->runningInConsole()) { |
19
|
|
|
$this->publishes([ |
20
|
|
|
__DIR__ . '/../config/eloquentaes.php' => config_path('eloquentaes.php'), |
21
|
|
|
], 'config'); |
22
|
|
|
|
23
|
|
|
// Registering package commands. |
24
|
|
|
$this->commands([ |
25
|
|
|
KeyGenerateCommand::class, |
26
|
|
|
]); |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Register the application services. |
32
|
|
|
*/ |
33
|
|
|
public function register() |
34
|
|
|
{ |
35
|
|
|
// Automatically apply the package configuration |
36
|
|
|
$this->mergeConfigFrom(__DIR__ . '/../config/eloquentaes.php', 'eloquentaes'); |
37
|
|
|
|
38
|
|
|
$this->registerEncryptor(); |
39
|
|
|
$this->registerOpisSecurityKey(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Register the encrypter. |
44
|
|
|
* |
45
|
|
|
* @return void |
46
|
|
|
*/ |
47
|
|
|
protected function registerEncryptor() |
48
|
|
|
{ |
49
|
|
|
$this->app->singleton('eloquentaes', function ($app) { |
50
|
|
|
$config = $app->make('config')->get('eloquentaes'); |
51
|
|
|
|
52
|
|
|
return new Encrypter($this->parseKey($config), $config['cipher']); |
53
|
|
|
}); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Configure Opis Closure signing for security. |
58
|
|
|
* |
59
|
|
|
* @return void |
60
|
|
|
*/ |
61
|
|
|
protected function registerOpisSecurityKey() |
62
|
|
|
{ |
63
|
|
|
$config = $this->app->make('config')->get('eloquentaes'); |
64
|
|
|
|
65
|
|
|
if (! class_exists(SerializableClosure::class) || empty($config['key'])) { |
66
|
|
|
return; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
SerializableClosure::setSecretKey($this->parseKey($config)); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Extract the encryption key from the given configuration. |
74
|
|
|
* |
75
|
|
|
* @param array $config |
76
|
|
|
* @return string |
77
|
|
|
* |
78
|
|
|
* @throws \RuntimeException |
79
|
|
|
*/ |
80
|
|
|
protected function key(array $config) |
81
|
|
|
{ |
82
|
|
|
return tap($config['key'], function ($key) { |
83
|
|
|
if (empty($key)) { |
84
|
|
|
throw new MissingAppKeyException( |
85
|
|
|
"No eloquent encryption key has been specified." |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
}); |
89
|
|
|
} |
90
|
|
|
} |