diff --git a/Middleware/RequireCsrfToken.php b/Middleware/RequireCsrfToken.php new file mode 100644 index 0000000..38aaa80 --- /dev/null +++ b/Middleware/RequireCsrfToken.php @@ -0,0 +1,61 @@ +encrypter = $encrypter; + } + + /** + * Handle an incoming request. + * + * @param \Illuminate\Http\Request $request + * @param \Closure $next + * + * @return mixed + */ + public function handle($request, Closure $next) + { + if (! $this->tokensMatch($request)) { + throw new TokenMismatchException(); + } + + return $next($request); + } + + /** + * Determine if the session and input CSRF tokens match. + * + * @param \Illuminate\Http\Request $request + * + * @return bool + */ + protected function tokensMatch($request) + { + $token = $request->input('_token') ?: $request->header('X-CSRF-TOKEN'); + + if (! $token && $header = $request->header('X-XSRF-TOKEN')) { + $token = $this->encrypter->decrypt($header); + } + + return Str::equals($request->session()->token(), $token); + } +}