Skip to content

Commit

Permalink
register opis key so it is not tied to a deferred service provider
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell authored and driesvints committed Apr 24, 2020
1 parent f2ea1a2 commit a4574ea
Showing 1 changed file with 44 additions and 8 deletions.
52 changes: 44 additions & 8 deletions src/Illuminate/Encryption/EncryptionServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str;
use Opis\Closure\SerializableClosure;
use RuntimeException;

class EncryptionServiceProvider extends ServiceProvider
Expand All @@ -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.
*
Expand Down

0 comments on commit a4574ea

Please sign in to comment.