44
55use Illuminate \Support \ServiceProvider ;
66use Illuminate \Support \Str ;
7+ use Opis \Closure \SerializableClosure ;
78use RuntimeException ;
89
910class EncryptionServiceProvider extends ServiceProvider
@@ -14,21 +15,56 @@ class EncryptionServiceProvider extends ServiceProvider
1415 * @return void
1516 */
1617 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 ()
1729 {
1830 $ this ->app ->singleton ('encrypter ' , function ($ app ) {
1931 $ config = $ app ->make ('config ' )->get ('app ' );
2032
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 ' ]);
2934 });
3035 }
3136
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+
3268 /**
3369 * Extract the encryption key from the given configuration.
3470 *
0 commit comments