4
4
5
5
use Illuminate \Support \ServiceProvider ;
6
6
use Illuminate \Support \Str ;
7
+ use Opis \Closure \SerializableClosure ;
7
8
use RuntimeException ;
8
9
9
10
class EncryptionServiceProvider extends ServiceProvider
@@ -14,21 +15,56 @@ class EncryptionServiceProvider extends ServiceProvider
14
15
* @return void
15
16
*/
16
17
public function register ()
18
+ {
19
+ $ this ->registerEncrypter ();
20
+ $ this ->registerOpisSecurityKey ();
21
+ }
22
+
23
+ /**
24
+ * Register the encrypter.
25
+ *
26
+ * @return void
27
+ */
28
+ protected function registerEncrypter ()
17
29
{
18
30
$ this ->app ->singleton ('encrypter ' , function ($ app ) {
19
31
$ config = $ app ->make ('config ' )->get ('app ' );
20
32
21
- // If the key starts with "base64:", we will need to decode the key before handing
22
- // it off to the encrypter. Keys may be base-64 encoded for presentation and we
23
- // want to make sure to convert them back to the raw bytes before encrypting.
24
- if (Str::startsWith ($ key = $ this ->key ($ config ), $ prefix = 'base64: ' )) {
25
- $ key = base64_decode (Str::after ($ key , $ prefix ));
26
- }
27
-
28
- return new Encrypter ($ key , $ config ['cipher ' ]);
33
+ return new Encrypter ($ this ->parseKey ($ config ), $ config ['cipher ' ]);
29
34
});
30
35
}
31
36
37
+ /**
38
+ * Configure Opis Closure signing for security.
39
+ *
40
+ * @return void
41
+ */
42
+ protected function registerOpisSecurityKey ()
43
+ {
44
+ $ config = $ this ->app ->make ('config ' )->get ('app ' );
45
+
46
+ if (! class_exists (SerializableClosure::class) || empty ($ config ['key ' ])) {
47
+ return ;
48
+ }
49
+
50
+ SerializableClosure::setSecretKey ($ this ->parseKey ($ config ));
51
+ }
52
+
53
+ /**
54
+ * Parse the encryption key.
55
+ *
56
+ * @param array $config
57
+ * @return string
58
+ */
59
+ protected function parseKey (array $ config )
60
+ {
61
+ if (Str::startsWith ($ key = $ this ->key ($ config ), $ prefix = 'base64: ' )) {
62
+ $ key = base64_decode (Str::after ($ key , $ prefix ));
63
+ }
64
+
65
+ return $ key ;
66
+ }
67
+
32
68
/**
33
69
* Extract the encryption key from the given configuration.
34
70
*
0 commit comments