From a4574ea973bab9bd6a2ba34d36dfb8f9b55d5a4a Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 23 Apr 2020 10:04:11 -0500 Subject: [PATCH] register opis key so it is not tied to a deferred service provider --- .../Encryption/EncryptionServiceProvider.php | 52 ++++++++++++++++--- 1 file changed, 44 insertions(+), 8 deletions(-) diff --git a/src/Illuminate/Encryption/EncryptionServiceProvider.php b/src/Illuminate/Encryption/EncryptionServiceProvider.php index 31a5972d3839..cd590f12dbb3 100755 --- a/src/Illuminate/Encryption/EncryptionServiceProvider.php +++ b/src/Illuminate/Encryption/EncryptionServiceProvider.php @@ -4,6 +4,7 @@ use Illuminate\Support\ServiceProvider; use Illuminate\Support\Str; +use Opis\Closure\SerializableClosure; use RuntimeException; class EncryptionServiceProvider extends ServiceProvider @@ -14,21 +15,56 @@ class EncryptionServiceProvider extends ServiceProvider * @return void */ public function register() + { + $this->registerEncrypter(); + $this->registerOpisSecurityKey(); + } + + /** + * Register the encrypter. + * + * @return void + */ + protected function registerEncrypter() { $this->app->singleton('encrypter', function ($app) { $config = $app->make('config')->get('app'); - // If the key starts with "base64:", we will need to decode the key before handing - // it off to the encrypter. Keys may be base-64 encoded for presentation and we - // want to make sure to convert them back to the raw bytes before encrypting. - if (Str::startsWith($key = $this->key($config), 'base64:')) { - $key = base64_decode(substr($key, 7)); - } - - return new Encrypter($key, $config['cipher']); + return new Encrypter($this->parseKey($config), $config['cipher']); }); } + /** + * Configure Opis Closure signing for security. + * + * @return void + */ + protected function registerOpisSecurityKey() + { + $config = $this->app->make('config')->get('app'); + + if (! class_exists(SerializableClosure::class) || empty($config['key'])) { + return; + } + + SerializableClosure::setSecretKey($this->parseKey($config)); + } + + /** + * Parse the encryption key. + * + * @param array $config + * @return string + */ + protected function parseKey(array $config) + { + if (Str::startsWith($key = $this->key($config), $prefix = 'base64:')) { + $key = base64_decode(Str::after($key, $prefix)); + } + + return $key; + } + /** * Extract the encryption key from the given configuration. *