/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php
https://github.com/immortalChensm/laravel-work · PHP · 180 lines · 83 code · 23 blank · 74 comment · 7 complexity · a407c3a5d6970902e1af833eb2b28223 MD5 · raw file
- <?php
- namespace Illuminate\Foundation\Http\Middleware;
- use Closure;
- use Illuminate\Foundation\Application;
- use Illuminate\Support\InteractsWithTime;
- use Symfony\Component\HttpFoundation\Cookie;
- use Illuminate\Contracts\Encryption\Encrypter;
- use Illuminate\Session\TokenMismatchException;
- use Illuminate\Cookie\Middleware\EncryptCookies;
- class VerifyCsrfToken
- {
- use InteractsWithTime;
- /**
- * The application instance.
- *
- * @var \Illuminate\Foundation\Application
- */
- protected $app;
- /**
- * The encrypter implementation.
- *
- * @var \Illuminate\Contracts\Encryption\Encrypter
- */
- protected $encrypter;
- /**
- * The URIs that should be excluded from CSRF verification.
- *
- * @var array
- */
- protected $except = [];
- /**
- * Create a new middleware instance.
- *
- * @param \Illuminate\Foundation\Application $app
- * @param \Illuminate\Contracts\Encryption\Encrypter $encrypter
- * @return void
- */
- public function __construct(Application $app, Encrypter $encrypter)
- {
- $this->app = $app;
- $this->encrypter = $encrypter;
- }
- /**
- * Handle an incoming request.
- *
- * @param \Illuminate\Http\Request $request
- * @param \Closure $next
- * @return mixed
- *
- * @throws \Illuminate\Session\TokenMismatchException
- */
- public function handle($request, Closure $next)
- {
- if (
- $this->isReading($request) ||
- $this->runningUnitTests() ||
- $this->inExceptArray($request) ||
- $this->tokensMatch($request)
- ) {
- return $this->addCookieToResponse($request, $next($request));
- }
- throw new TokenMismatchException;
- }
- /**
- * Determine if the HTTP request uses a ‘read’ verb.
- *
- * @param \Illuminate\Http\Request $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.
- *验证当前的请求是我当前设置的csrf应该要跳过的地址
- * @param \Illuminate\Http\Request $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 \Illuminate\Http\Request $request
- * @return bool
- */
- protected function tokensMatch($request)
- {
- //会完成对请求的数据验签和openssl验证
- //得 到openssl解密的数据
- $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 \Illuminate\Http\Request $request
- * @return string
- */
- protected function getTokenFromRequest($request)
- {
- $token = $request->input('_token') ?: $request->header('X-CSRF-TOKEN');
- if (! $token && $header = $request->header('X-XSRF-TOKEN')) {
- $token = $this->encrypter->decrypt($header, static::serialized());
- }
- return $token;
- }
- /**
- * Add the CSRF token to the response cookies.
- *
- * @param \Illuminate\Http\Request $request
- * @param \Symfony\Component\HttpFoundation\Response $response
- * @return \Symfony\Component\HttpFoundation\Response
- */
- protected function addCookieToResponse($request, $response)
- {
- $config = config('session');
- $response->headers->setCookie(
- new Cookie(
- 'XSRF-TOKEN', $request->session()->token(), $this->availableAt(60 * $config['lifetime']),
- $config['path'], $config['domain'], $config['secure'], false, false, $config['same_site'] ?? null
- )
- );
- return $response;
- }
- /**
- * Determine if the cookie contents should be serialized.
- *
- * @return bool
- */
- public static function serialized()
- {
- return EncryptCookies::serialized('XSRF-TOKEN');
- }
- }