Вход Регистрация
Файл: vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php
Строк: 278
<?php

namespace IlluminateFoundationHttpMiddleware;

use 
Closure;
use 
IlluminateContractsEncryptionDecryptException;
use 
IlluminateContractsEncryptionEncrypter;
use 
IlluminateContractsFoundationApplication;
use 
IlluminateContractsSupportResponsable;
use 
IlluminateCookieCookieValuePrefix;
use 
IlluminateCookieMiddlewareEncryptCookies;
use 
IlluminateSessionTokenMismatchException;
use 
IlluminateSupportInteractsWithTime;
use 
SymfonyComponentHttpFoundationCookie;

class 
VerifyCsrfToken
{
    use 
InteractsWithTime;

    
/**
     * The application instance.
     *
     * @var IlluminateContractsFoundationApplication
     */
    
protected $app;

    
/**
     * The encrypter implementation.
     *
     * @var IlluminateContractsEncryptionEncrypter
     */
    
protected $encrypter;

    
/**
     * The URIs that should be excluded from CSRF verification.
     *
     * @var array<int, string>
     */
    
protected $except = [];

    
/**
     * Indicates whether the XSRF-TOKEN cookie should be set on the response.
     *
     * @var bool
     */
    
protected $addHttpCookie true;

    
/**
     * Create a new middleware instance.
     *
     * @param  IlluminateContractsFoundationApplication  $app
     * @param  IlluminateContractsEncryptionEncrypter  $encrypter
     * @return void
     */
    
public function __construct(Application $appEncrypter $encrypter)
    {
        
$this->app $app;
        
$this->encrypter $encrypter;
    }

    
/**
     * Handle an incoming request.
     *
     * @param  IlluminateHttpRequest  $request
     * @param  Closure  $next
     * @return mixed
     *
     * @throws IlluminateSessionTokenMismatchException
     */
    
public function handle($requestClosure $next)
    {
        if (
            
$this->isReading($request) ||
            
$this->runningUnitTests() ||
            
$this->inExceptArray($request) ||
            
$this->tokensMatch($request)
        ) {
            return 
tap($next($request), function ($response) use ($request) {
                if (
$this->shouldAddXsrfTokenCookie()) {
                    
$this->addCookieToResponse($request$response);
                }
            });
        }

        throw new 
TokenMismatchException('CSRF token mismatch.');
    }

    
/**
     * Determine if the HTTP request uses a ‘read’ verb.
     *
     * @param  IlluminateHttpRequest  $request
     * @return bool
     */
    
protected function isReading($request)
    {
        return 
in_array($request->method(), ['HEAD''GET''OPTIONS']);
    }

    
/**
     * Determine if the application is running unit tests.
     *
     * @return bool
     */
    
protected function runningUnitTests()
    {
        return 
$this->app->runningInConsole() && $this->app->runningUnitTests();
    }

    
/**
     * Determine if the request has a URI that should pass through CSRF verification.
     *
     * @param  IlluminateHttpRequest  $request
     * @return bool
     */
    
protected function inExceptArray($request)
    {
        foreach (
$this->except as $except) {
            if (
$except !== '/') {
                
$except trim($except'/');
            }

            if (
$request->fullUrlIs($except) || $request->is($except)) {
                return 
true;
            }
        }

        return 
false;
    }

    
/**
     * Determine if the session and input CSRF tokens match.
     *
     * @param  IlluminateHttpRequest  $request
     * @return bool
     */
    
protected function tokensMatch($request)
    {
        
$token $this->getTokenFromRequest($request);

        return 
is_string($request->session()->token()) &&
               
is_string($token) &&
               
hash_equals($request->session()->token(), $token);
    }

    
/**
     * Get the CSRF token from the request.
     *
     * @param  IlluminateHttpRequest  $request
     * @return string|null
     */
    
protected function getTokenFromRequest($request)
    {
        
$token $request->input('_token') ?: $request->header('X-CSRF-TOKEN');

        if (! 
$token && $header $request->header('X-XSRF-TOKEN')) {
            try {
                
$token CookieValuePrefix::remove($this->encrypter->decrypt($header, static::serialized()));
            } catch (
DecryptException) {
                
$token '';
            }
        }

        return 
$token;
    }

    
/**
     * Determine if the cookie should be added to the response.
     *
     * @return bool
     */
    
public function shouldAddXsrfTokenCookie()
    {
        return 
$this->addHttpCookie;
    }

    
/**
     * Add the CSRF token to the response cookies.
     *
     * @param  IlluminateHttpRequest  $request
     * @param  SymfonyComponentHttpFoundationResponse  $response
     * @return SymfonyComponentHttpFoundationResponse
     */
    
protected function addCookieToResponse($request$response)
    {
        
$config config('session');

        if (
$response instanceof Responsable) {
            
$response $response->toResponse($request);
        }

        
$response->headers->setCookie($this->newCookie($request$config));

        return 
$response;
    }

    
/**
     * Create a new "XSRF-TOKEN" cookie that contains the CSRF token.
     *
     * @param  IlluminateHttpRequest  $request
     * @param  array  $config
     * @return SymfonyComponentHttpFoundationCookie
     */
    
protected function newCookie($request$config)
    {
        return new 
Cookie(
            
'XSRF-TOKEN',
            
$request->session()->token(),
            
$this->availableAt(60 $config['lifetime']),
            
$config['path'],
            
$config['domain'],
            
$config['secure'],
            
false,
            
false,
            
$config['same_site'] ?? null,
            
$config['partitioned'] ?? false
        
);
    }

    
/**
     * Determine if the cookie contents should be serialized.
     *
     * @return bool
     */
    
public static function serialized()
    {
        return 
EncryptCookies::serialized('XSRF-TOKEN');
    }
}
Онлайн: 1
Реклама