/** * Execute the middleware. * * @param ServerRequestInterface $request * @param ResponseInterface $response * @param callable $next * * @return ResponseInterface */ public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) { if (!self::hasAttribute($request, ClientIp::KEY)) { throw new RuntimeException('Csrf middleware needs ClientIp executed before'); } if (Utils\Helpers::getMimeType($response) !== 'text/html') { return $next($request, $response); } $tokens =& self::getStorage($request, self::KEY); if (Utils\Helpers::isPost($request) && !$this->validateRequest($request, $tokens)) { return $response->withStatus(403); } $generator = function ($action = null) use($request, &$tokens) { if (empty($action)) { $action = $request->getUri()->getPath(); } return $this->generateTokens($request, $action, $tokens); }; if (!$this->autoInsert) { $request = self::setAttribute($request, self::KEY_GENERATOR, $generator); return $next($request, $response); } $response = $next($request, $response); return $this->insertIntoPostForms($response, function ($match) use($generator) { preg_match('/action=["\']?([^"\'\\s]+)["\']?/i', $match[0], $matches); return $match[0] . $generator(isset($matches[1]) ? $matches[1] : null); }); }
/** * Execute the middleware. * * @param ServerRequestInterface $request * @param ResponseInterface $response * @param callable $next * * @return ResponseInterface */ public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) { if (!Middleware::hasAttribute($request, FormatNegotiator::KEY)) { throw new RuntimeException('Csrf middleware needs FormatNegotiator executed before'); } if (!Middleware::hasAttribute($request, ClientIp::KEY)) { throw new RuntimeException('Csrf middleware needs ClientIp executed before'); } if ($this->storage === null) { if (session_status() !== PHP_SESSION_ACTIVE) { throw new RuntimeException('Csrf middleware needs an active php session or a storage defined'); } if (!isset($_SESSION[$this->sessionIndex])) { $_SESSION[$this->sessionIndex] = []; } $this->storage =& $_SESSION[$this->sessionIndex]; } if (FormatNegotiator::getFormat($request) !== 'html') { return $next($request, $response); } if (Utils\Helpers::isPost($request) && !$this->validateRequest($request)) { return $response->withStatus(403); } $response = $next($request, $response); return $this->insertIntoPostForms($response, function ($match) use($request) { preg_match('/action=["\']?([^"\'\\s]+)["\']?/i', $match[0], $matches); $action = empty($matches[1]) ? $request->getUri()->getPath() : $matches[1]; return $match[0] . $this->generateTokens($request, $action); }); }
/** * Execute the middleware. * * @param ServerRequestInterface $request * @param ResponseInterface $response * @param callable $next * * @return ResponseInterface */ public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) { if (!Middleware::hasAttribute($request, ClientIp::KEY)) { throw new RuntimeException('Recaptcha middleware needs ClientIp executed before'); } if (Utils\Helpers::isPost($request)) { $recaptcha = new GoogleRecaptcha($this->secret); $data = $request->getParsedBody(); $res = $recaptcha->verify(isset($data['g-recaptcha-response']) ? $data['g-recaptcha-response'] : '', ClientIp::getIp($request)); if (!$res->isSuccess()) { return $response->withStatus(403); } } return $next($request, $response); }
/** * Execute the middleware. * * @param ServerRequestInterface $request * @param ResponseInterface $response * @param callable $next * * @return ResponseInterface */ public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) { if (!Middleware::hasAttribute($request, FormatNegotiator::KEY)) { throw new RuntimeException('Honeypot middleware needs FormatNegotiator executed before'); } if (FormatNegotiator::getFormat($request) !== 'html') { return $next($request, $response); } if (Utils\Helpers::isPost($request) && !$this->isValid($request)) { return $response->withStatus(403); } $response = $next($request, $response); return $this->insertIntoPostForms($response, function ($match) { return $match[0] . '<input type="text" name="' . $this->inputName . '" class="' . $this->inputClass . '">'; }); }
/** * Execute the middleware. * * @param ServerRequestInterface $request * @param ResponseInterface $response * @param callable $next * * @return ResponseInterface */ public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) { if (Utils\Helpers::getMimeType($response) !== 'text/html') { return $next($request, $response); } if (Utils\Helpers::isPost($request) && !$this->isValid($request)) { return $response->withStatus(403); } $generator = function () { return '<input type="text" name="' . $this->inputName . '" class="' . $this->inputClass . '">'; }; if (!$this->autoInsert) { $request = self::setAttribute($request, self::KEY_GENERATOR, $generator); return $next($request, $response); } $response = $next($request, $response); return $this->insertIntoPostForms($response, function ($match) use($generator) { return $match[0] . $generator(); }); }