VerifyCsrfToken.php 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php namespace App\Http\Middleware;
  2. use Closure;
  3. use Illuminate\Contracts\Routing\Middleware;
  4. use Illuminate\Session\TokenMismatchException;
  5. class VerifyCsrfToken implements Middleware {
  6. /**
  7. * Handle an incoming request.
  8. *
  9. * @param \Illuminate\Http\Request $request
  10. * @param \Closure $next
  11. * @return mixed
  12. *
  13. * @throws TokenMismatchException
  14. */
  15. public function handle($request, Closure $next)
  16. {
  17. if ($this->isReading($request) || $this->tokensMatch($request))
  18. {
  19. return $next($request);
  20. }
  21. throw new TokenMismatchException;
  22. }
  23. /**
  24. * Determine if the session and input CSRF tokens match.
  25. *
  26. * @param \Illuminate\Http\Request $request
  27. * @return bool
  28. */
  29. protected function tokensMatch($request)
  30. {
  31. return $request->session()->token() == $request->input('_token');
  32. }
  33. /**
  34. * Determine if the HTTP request uses a ‘read’ verb.
  35. *
  36. * @param \Illuminate\Http\Request $request
  37. * @return bool
  38. */
  39. protected function isReading($request)
  40. {
  41. return in_array($request->method(), ['HEAD', 'GET', 'OPTIONS']);
  42. }
  43. }