master

laravel/framework

Last updated at: 29/12/2023 09:22

EncryptionServiceProvider.php

TLDR

The EncryptionServiceProvider.php file in the Illuminate/Encryption namespace is a service provider that registers an encrypter and configures the Serializable Closure signing for security.

Methods

register

This method is responsible for registering the service provider. It calls the registerEncrypter and registerSerializableClosureSecurityKey methods.

registerEncrypter

This method registers the encrypter in the application container ($this->app). It retrieves the encryption configuration from the application's config and creates a new Encrypter instance using the encryption key and cipher specified in the configuration.

registerSerializableClosureSecurityKey

This method configures the Serializable Closure signing for security. It retrieves the key from the application's config and sets it as the secret key for the SerializableClosure class if the class exists and the key is not empty.

parseKey

This method parses the encryption key from the given configuration. If the key starts with the prefix 'base64:', it decodes the base64-encoded part of the key and returns the result. Otherwise, it returns the original key.

key

This method extracts the encryption key from the given configuration. It returns the key from the configuration and throws a MissingAppKeyException if the key is empty.

Classes

There are no classes in this file.

<?php

namespace Illuminate\Encryption;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str;
use Laravel\SerializableClosure\SerializableClosure;

class EncryptionServiceProvider extends ServiceProvider
{
    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        $this->registerEncrypter();
        $this->registerSerializableClosureSecurityKey();
    }

    /**
     * Register the encrypter.
     *
     * @return void
     */
    protected function registerEncrypter()
    {
        $this->app->singleton('encrypter', function ($app) {
            $config = $app->make('config')->get('app');

            return new Encrypter($this->parseKey($config), $config['cipher']);
        });
    }

    /**
     * Configure Serializable Closure signing for security.
     *
     * @return void
     */
    protected function registerSerializableClosureSecurityKey()
    {
        $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.
     *
     * @param  array  $config
     * @return string
     *
     * @throws \Illuminate\Encryption\MissingAppKeyException
     */
    protected function key(array $config)
    {
        return tap($config['key'], function ($key) {
            if (empty($key)) {
                throw new MissingAppKeyException;
            }
        });
    }
}