/**
  * {@inheritdoc}
  */
 public function validateAuthorization(\Phalcon\Http\RequestInterface $request)
 {
     if (!$request->getHeader('authorization')) {
         throw OAuthServerException::accessDenied('Missing "Authorization" header');
     }
     $header = $request->getHeader('authorization');
     $jwt = trim(preg_replace('/^(?:\\s+)?Bearer\\s/', '', $header));
     try {
         // Attempt to parse and validate the JWT
         $token = (new Parser())->parse($jwt);
         if ($token->verify(new Sha256(), $this->publicKey->getKeyPath()) === false) {
             throw OAuthServerException::accessDenied('Access token could not be verified');
         }
         // Ensure access token hasn't expired
         $data = new ValidationData();
         $data->setCurrentTime(time());
         if ($token->validate($data) === false) {
             throw OAuthServerException::accessDenied('Access token is invalid');
         }
         // Check if token has been revoked
         if ($this->accessTokenRepository->isAccessTokenRevoked($token->getClaim('jti'))) {
             throw OAuthServerException::accessDenied('Access token has been revoked');
         }
         // Return the response with additional attributes
         $response = ['oauth_access_token_id' => $token->getClaim('jti'), 'oauth_client_id' => $token->getClaim('aud'), 'oauth_user_id' => $token->getClaim('sub'), 'oauth_scopes' => $token->getClaim('scopes')];
         return $response;
     } catch (\InvalidArgumentException $exception) {
         // JWT couldn't be parsed so return the request as is
         throw OAuthServerException::accessDenied($exception->getMessage());
     }
 }
 /**
  * check if client have right scopes to access the route.
  *
  * @param $neededScopes
  * @param $requestedScopes
  *
  * @throws OAuthServerException
  *
  * @return bool
  */
 protected function validateScopes($neededScopes, $requestedScopes)
 {
     if (empty($neededScopes)) {
         return true;
     }
     foreach ($requestedScopes as $requestedScope) {
         if (in_array($requestedScope->getIdentifier(), $neededScopes)) {
             return true;
         }
     }
     throw OAuthServerException::accessDenied('you need right scope to access this resource');
 }
 /**
  * {@inheritdoc}
  */
 public function validateAuthorization(ServerRequestInterface $request)
 {
     if ($request->hasHeader('authorization') === false) {
         throw OAuthServerException::accessDenied('Missing "Authorization" header');
     }
     $header = $request->getHeader('authorization');
     $accessTokenId = trim($header[0]);
     try {
         $accessTokenEntity = $this->accessTokenRepository->findAccessToken($accessTokenId);
         // Check if token has been revoked
         if (is_null($accessTokenEntity)) {
             throw OAuthServerException::accessDenied('Access token has been revoked');
         }
         // Ensure access token hasn't expired
         if ($accessTokenEntity->getExpiryDateTime()->lt(Carbon::now())) {
             throw OAuthServerException::accessDenied('Access token is invalid');
         }
         // Return the request with additional attributes
         return $request->withAttribute('oauth_access_token_id', $accessTokenEntity->getIdentifier())->withAttribute('oauth_client_id', $accessTokenEntity->getClient()->getIdentifier())->withAttribute('oauth_user_id', $accessTokenEntity->getUserIdentifier())->withAttribute('oauth_scopes', $accessTokenEntity->getScopes());
     } catch (\InvalidArgumentException $exception) {
         // JWT couldn't be parsed so return the request as is
         throw OAuthServerException::accessDenied($exception->getMessage());
     }
 }
 /**
  * {@inheritdoc}
  */
 public function completeAuthorizationRequest(AuthorizationRequest $authorizationRequest)
 {
     if ($authorizationRequest->getUser() instanceof UserEntityInterface === false) {
         throw new \LogicException('An instance of UserEntityInterface should be set on the AuthorizationRequest');
     }
     $finalRedirectUri = $authorizationRequest->getRedirectUri() === null ? is_array($authorizationRequest->getClient()->getRedirectUri()) ? $authorizationRequest->getClient()->getRedirectUri()[0] : $authorizationRequest->getClient()->getRedirectUri() : $authorizationRequest->getRedirectUri();
     // The user approved the client, redirect them back with an auth code
     if ($authorizationRequest->isAuthorizationApproved() === true) {
         $authCode = $this->issueAuthCode($this->authCodeTTL, $authorizationRequest->getClient(), $authorizationRequest->getUser()->getIdentifier(), $authorizationRequest->getRedirectUri(), $authorizationRequest->getScopes());
         $response = new RedirectResponse();
         $response->setRedirectUri($this->makeRedirectUri($finalRedirectUri, ['code' => $this->encrypt(json_encode(['client_id' => $authCode->getClient()->getIdentifier(), 'redirect_uri' => $authCode->getRedirectUri(), 'auth_code_id' => $authCode->getIdentifier(), 'scopes' => $authCode->getScopes(), 'user_id' => $authCode->getUserIdentifier(), 'expire_time' => (new \DateTime())->add($this->authCodeTTL)->format('U'), 'code_challenge' => $authorizationRequest->getCodeChallenge(), 'code_challenge_method  ' => $authorizationRequest->getCodeChallengeMethod()])), 'state' => $authorizationRequest->getState()]));
         return $response;
     }
     // The user denied the client, redirect them back with an error
     throw OAuthServerException::accessDenied('The user denied the request', $this->makeRedirectUri($finalRedirectUri, ['state' => $authorizationRequest->getState()]));
 }
示例#5
0
 /**
  * {@inheritdoc}
  */
 public function completeAuthorizationRequest(AuthorizationRequest $authorizationRequest)
 {
     if ($authorizationRequest->getUser() instanceof UserEntityInterface === false) {
         throw new \LogicException('An instance of UserEntityInterface should be set on the AuthorizationRequest');
     }
     $finalRedirectUri = $authorizationRequest->getRedirectUri() === null ? is_array($authorizationRequest->getClient()->getRedirectUri()) ? $authorizationRequest->getClient()->getRedirectUri()[0] : $authorizationRequest->getClient()->getRedirectUri() : $authorizationRequest->getRedirectUri();
     // The user approved the client, redirect them back with an access token
     if ($authorizationRequest->isAuthorizationApproved() === true) {
         $accessToken = $this->issueAccessToken($this->accessTokenTTL, $authorizationRequest->getClient(), $authorizationRequest->getUser()->getIdentifier(), $authorizationRequest->getScopes());
         $response = new RedirectResponse();
         $response->setRedirectUri($this->makeRedirectUri($finalRedirectUri, ['access_token' => (string) $accessToken->convertToJWT($this->privateKey), 'token_type' => 'bearer', 'expires_in' => $accessToken->getExpiryDateTime()->getTimestamp() - (new \DateTime())->getTimestamp(), 'state' => $authorizationRequest->getState()], '#'));
         return $response;
     }
     // The user denied the client, redirect them back with an error
     throw OAuthServerException::accessDenied('The user denied the request', $this->makeRedirectUri($finalRedirectUri, ['state' => $authorizationRequest->getState()]));
 }