Validates if the current request has a valid token.

@throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException if no valid token exists
761856
public function onKernelRequest(GetResponseEvent $event): void
    {
        if (!$event->isMasterRequest()) {
            return;
        }

        $request = $event->getRequest();

        if ($request->attributes->get(SetIsApiRequestListener::API_FLAG_NAME) !== true) {
            return;
        }

        if ($this->csrfTokenValidator->validateCsrfToken($request, $this->csrfTokenId)) {
            return;
        }

        throw new AccessDeniedHttpException('Missing or invalid CSRF token');
    }
1251233
protected function isTokenValid($tokenString)
    {
        try {
            $user = ApiToken::validateToken($tokenString);

            // We need to make the login provider understand that we're now authenticated.
            $this->logUserIn($user);
        } catch (TokenInvalidException $er) {
            throw new ForceResponseException(new TokenAuthorisationRequiredResponse($this));
        }

        return true;
    }
1028560
public function verify($token, $identifier = null)
    {
        $identifier = ($identifier !== null) ? $identifier : $this->identifier;

        // Throw error for empty token or empty message id, because the server will not handle this
        if (empty($token)) {
            throw new Totp\EmptyTokenException();
        }
        if (empty($this->identifier)) {
            throw new Totp\EmptyIdentifierException();
        }

        try {
            $this->sendApiCall(self::ACTION_RETRIEVE, sprintf('totp/%s?token=%s', $identifier, $token));
        } catch (Exception $e) {
            if (Totp\Exception::isTotpException($e)) {
                throw new Totp\Exception($e);
            } else {
                throw $e;
            }
        }
    }
806001
protected function isValid()
    {
        if (!$this->request->ajax()) {
            return false;
        }
        $search = $this->request->input('search');
        if (!is_string($search) or strlen($this->request->input('search')) <= 0) {
            return false;
        }
        $token = $this->request->header('search-protection');
        if (!$this->validateToken($token)) {
            return false;
        }
        return true;
    }
1341873
public function validateAccessToken($headersOnly = true, $accessToken = null)
    {
        return $this->resourceServer->isValidRequest($headersOnly, $accessToken);
    }
694142
private function _validate($token)
    {

        $request = $this->_httpClient->createRequest('POST', $this->_tokenServiceUrl . 'validate');
        $postBody = $request->getBody();


        // $postBody is an instance of GuzzleHttp\Post\PostBodyInterface
        $postBody->setField('service', $this->_appID);
        $postBody->setField('token', $token);

        // Send the POST request
        try {
            $response = $this->_httpClient->send($request);
        } catch (ClientException $e) {
            return "BAD_TOKEN";
        } catch (ServerException $e) {
            return "SERVER_ERROR";
        }

        return $response->json();

    }
1028389
public function check()
	{
		$request = $this->app['request'];
		$getter = new TokenGetter( new Header($request), new  QueryStr($request));
		return $this->auth->check($getter, $this->secretKey);
	}
310005
public function isValid()
    {
        $query = [
            'access_token' => $this['access_token'],
            'openid' => $this['openid'],
        ];

        $response = Http::request('GET', static::IS_VALID)
            ->withQuery($query)
            ->send();

        return 'ok' === $response['errmsg'];
    }
758331
public function isAuthorised( Request $request )
    {
        $header = $request->headers->get( 'Authorization' );
        $context = new Context( EncryptionFactory::create( $this->algorithm ) );

        if ( $header ) {
            try{
                $this->token = $this->jwt->deserialize( $this->extractJwtFromHeader($header) );
            } catch ( \Exception $e ){
                return false;
            }
        }

        if ( $this->algorithm instanceof None ) {
            return false;
        }

        if( $this->isExpired() || !$this->hasUserId() || !$this->isAllowedAppId() ) {
            return false;
        }

        try {
            return $this->jwt->verify( $this->token, $context );
        } catch ( VerificationException $e ) {
            return false;
        }
    }
88666
private function validateRequest(ServerRequestInterface $request, array &$tokens)
    {
        $data = $request->getParsedBody();

        if (!isset($data[$this->formIndex]) || !isset($data[$this->formToken])) {
            return false;
        }

        $index = $data[$this->formIndex];
        $token = $data[$this->formToken];

        if (!isset($tokens[$index])) {
            return false;
        }

        $stored = $tokens[$index];
        unset($tokens[$index]);

        $lockTo = $request->getUri()->getPath();

        if (!Utils\Helpers::hashEquals($lockTo, $stored['lockTo'])) {
            return false;
        }

        $expected = self::encode(hash_hmac('sha256', ClientIp::getIp($request), base64_decode($stored['token']), true));

        return Utils\Helpers::hashEquals($token, $expected);
    }
156370
public function validateRequest(): bool
    {
        if ($this->useNativeSession) {
            if (!isset($_SESSION[$this->sessionIndex])) {
                return false;
            }
            /** @var array<string, array<string, mixed>> $sess */
            $sess =& $_SESSION[$this->sessionIndex];
        } else {
            if (!isset($this->session[$this->sessionIndex])) {
                return false;
            }
            /** @var array<string, array<string, mixed>> $sess */
            $sess =& $this->session[$this->sessionIndex];
        }

        if (
            empty($this->post[$this->formIndex]) ||
            empty($this->post[$this->formToken])
        ) {
            // User must transmit a complete index/token pair
            return false;
        }

        // Let's pull the POST data
        /** @var string $index */
        $index = $this->post[$this->formIndex];
        /** @var string $token */
        $token = $this->post[$this->formToken];
        if (!\is_string($index) || !\is_string($token)) {
            return false;
        }

        if (!isset($sess[$index])) {
            // CSRF Token not found
            return false;
        }

        if (!\is_string($index) || !\is_string($token)) {
            return false;
        }

        // Grab the value stored at $index
        /** @var array<string, mixed> $stored */
        $stored = $sess[$index];

        // We don't need this anymore
        if ($this->deleteToken($sess[$index])) {
            unset($sess[$index]);
        }

        // Which form action="" is this token locked to?
        /** @var string $lockTo */
        $lockTo = $this->server[$this->lock_type];
        if (\preg_match('#/$#', $lockTo)) {
            // Trailing slashes are to be ignored
            $lockTo = Binary::safeSubstr(
                $lockTo,
                0,
                Binary::safeStrlen($lockTo) - 1
            );
        }

        if (!\hash_equals($lockTo, (string) $stored['lockTo'])) {
            // Form target did not match the request this token is locked to!
            return false;
        }

        // This is the expected token value
        if ($this->hmac_ip === false) {
            // We just stored it wholesale
            /** @var string $expected */
            $expected = $stored['token'];
        } else {
            // We mixed in the client IP address to generate the output
            /** @var string $expected */
            $expected = Base64UrlSafe::encode(
                \hash_hmac(
                    $this->hashAlgo,
                    isset($this->server['REMOTE_ADDR'])
                        ? (string) $this->server['REMOTE_ADDR']
                        : '127.0.0.1',
                    (string) Base64UrlSafe::decode((string) $stored['token']),
                    true
                )
            );
        }
        return \hash_equals($token, $expected);
    }
359115
public function validate(Request $request)
    {
        return ! \is_null($this->domain) && $request->getHost() === $this->getStrippedDomain();
    }
667096
public function validateAcceptHeader(): void
    {
        if ($this->isValidMediaTypeHeader("accept") === false) {
            throw $this->exceptionFactory->createMediaTypeUnacceptableException($this, $this->getHeaderLine("accept"));
        }
    }
1371479
public function validateCall(): void
    {

        if (! in_array($this->method, ['get', 'post', 'put', 'patch', 'delete']))
            throw new Exception('Invalid HTTP method used');

        if (trim($this->endpoint) === '')
            throw new Exception('Empty endpoint used');

        // Enfore a version specification unless this is a 'meta' call.
        if (trim($this->version) === '' && ! (in_array('meta', $this->tags())))
            throw new Exception('Version is empty');
    }
372877
public function isValidRequest()
    {
        $message = language()->getLine('OAUTH_SIGNATURE_METHOD_REJECTED');

        if (empty($this->oauth->callback)) {
            $consumer = new Consumer($this->oauth->consumer_key, $this->oauth->consumer_secret);

            $signature = $consumer->getSignature(
                $this->oauth->signature_method,
                null,
                null,
                [
                    'oauth_nonce'            => $this->oauth->nonce,
                    'oauth_signature_method' => $this->oauth->signature_method,
                    'oauth_timestamp'        => $this->oauth->timestamp,
                    'oauth_consumer_key'     => $this->oauth->consumer_key,
                    'oauth_version'          => $this->oauth->version,
                ]);

            if ($signature === $this->oauth->signature) {
                return true;
            }
        } elseif (empty($this->oauth->signature)) {
            $message = language()->getLine('OAUTH_SIGNATURE_MISSING');
        } else {
            $consumer = new Consumer($this->oauth->consumer_key, $this->oauth->consumer_secret);

            $signature = $consumer->getSignature(
                $this->oauth->signature_method,
                $this->oauth->callback,
                input()->server('REQUEST_METHOD'),
                [
                    'oauth_nonce'            => $this->oauth->nonce,
                    'oauth_signature_method' => $this->oauth->signature_method,
                    'oauth_timestamp'        => $this->oauth->timestamp,
                    'oauth_consumer_key'     => $this->oauth->consumer_key,
                    'oauth_version'          => $this->oauth->version,
                ]);

            if ($signature === $this->oauth->signature) {
                return true;
            }
        }

        $this->addError(OAUTH_SIGNATURE_METHOD_REJECTED, $message);

        return false;
    }
680322
public function requestToken(Request $request)
    {
        $client = $this->getClient($request);
        $refreshToken = $request->get('refresh_token');

        /** @var TokenInterface $accessToken */
        $accessToken = $this->accessTokenRepository->findOneByClientAndRefreshToken($client, $refreshToken);

        if (! $accessToken instanceof TokenInterface) {
            throw new TokenNotFoundHttpException();
        }
        if ($accessToken->isBlocked()) {
            throw new TokenBlockedHttpException();
        }

        $newAccessToken = $this->accessTokenManager->create($client, $accessToken->getUser());
        if (!$newAccessToken->isValid($this->validator)) {
            return $newAccessToken->getViolations();
        }

        $this->accessTokenManager->save($newAccessToken);

        $tokenFacade = new AccessTokenFacade();
        $tokenFacade->accessToken   = $newAccessToken->getCode();
        $tokenFacade->expiresAt     = $newAccessToken->getExpiredAt();
        $tokenFacade->refreshToken = $newAccessToken->getRefreshCode();

        return $tokenFacade;
    }
1022682
public function handle(GetResponseEvent $event)
    {
        if (!$event->isMasterRequest()) {
            return;
        }

        if (!$this->authorizationChecker->isGranted(self::ATTRIBUTE, $event->getRequest())) {
            throw new AccessDeniedException();
        }
    }