session === null) { throw new \RuntimeException(\sprintf('A %s object has not been set.', SessionInterface::class)); } return $this->session; } /** * Sets the session for the application to use, if required. * * @param SessionInterface $session A session object. * * @return $this * * @since 2.0.0 */ public function setSession(SessionInterface $session) { $this->session = $session; return $this; } /** * Checks for a form token in the request. * * @param string $method The request method in which to look for the token key. * * @return boolean * * @since 2.0.0 */ public function checkToken($method = 'post') { $token = $this->getFormToken(); // Support a token sent via the X-CSRF-Token header, then fall back to a token in the request $requestToken = $this->getInput()->server->get( 'HTTP_X_CSRF_TOKEN', $this->getInput()->$method->get($token, '', 'alnum'), 'alnum' ); if (!$requestToken) { return false; } return $this->getSession()->hasToken($token); } /** * Method to determine a hash for anti-spoofing variable names * * @param boolean $forceNew If true, force a new token to be created * * @return string Hashed var name * * @since 2.0.0 */ public function getFormToken($forceNew = false) { return $this->getSession()->getToken($forceNew); } }