getName(); return $class_name == 'oauth2\requests\OAuth2AccessTokenValidationRequest' && $request->isValid(); } public function getType() { return self::OAuth2Protocol_GrantType_Extension_ValidateBearerToken; } /** * @param OAuth2Request $request * @return mixed|void * @throws \oauth2\exceptions\InvalidOAuth2Request */ public function handle(OAuth2Request $request) { throw new InvalidOAuth2Request('Not Implemented!'); } /** * @param OAuth2Request $request * @return mixed|OAuth2AccessTokenValidationResponse|void * @throws \oauth2\exceptions\InvalidOAuth2Request * @throws \oauth2\exceptions\LockedClientException * @throws \oauth2\exceptions\InvalidApplicationType * @throws \oauth2\exceptions\BearerTokenDisclosureAttemptException */ public function completeFlow(OAuth2Request $request) { $reflector = new ReflectionClass($request); $class_name = $reflector->getName(); if ($class_name == 'oauth2\requests\OAuth2AccessTokenValidationRequest') { parent::completeFlow($request); $token_value = $request->getToken(); try{ $access_token = $this->token_service->getAccessToken($token_value); if(!$this->current_client->isResourceServerClient()){ // if current client is not a resource server, then we could only access to our own tokens if($access_token->getClientId()!== $this->current_client_id) throw new BearerTokenDisclosureAttemptException($this->current_client_id,sprintf('access token %s does not belongs to client id %s',$token_value, $this->current_client_id)); } else{ // current client is a resource server, validate client type (must be confidential) if($this->current_client->getClientType()!== IClient::ClientType_Confidential) throw new InvalidApplicationType($this->current_client_id,'resource server client is not of confidential type!'); //validate resource server IP address $current_ip = IPHelper::getUserIp(); $resource_server = $this->current_client->getResourceServer(); //check if resource server is active if(!$resource_server->active) throw new LockedClientException($this->current_client_id,'resource server is disabled!'); //check resource server ip address if($current_ip !== $resource_server->ip) throw new BearerTokenDisclosureAttemptException($this->current_client_id,sprintf('resource server ip (%s) differs from current request ip %s',$resource_server->ip,$current_ip)); // check if current ip belongs to a registered resource server audience if(!$this->token_service->checkAccessTokenAudience($access_token,$current_ip)) throw new BearerTokenDisclosureAttemptException($this->current_client_id,sprintf('access token current audience does not match with current request ip %s', $current_ip)); } return new OAuth2AccessTokenValidationResponse($token_value, $access_token->getScope(), $access_token->getAudience(),$access_token->getClientId(),$access_token->getRemainingLifetime(),$access_token->getUserId()); } catch(InvalidAccessTokenException $ex1){ $this->log_service->error($ex1); throw new BearerTokenDisclosureAttemptException($this->current_client_id,$ex1->getMessage()); } catch(InvalidGrantTypeException $ex2){ $this->log_service->error($ex2); throw new BearerTokenDisclosureAttemptException($this->current_client_id,$ex2->getMessage()); } } throw new InvalidOAuth2Request; } public function getResponseType() { throw new InvalidOAuth2Request('Not Implemented!'); } public function buildTokenRequest(OAuth2Request $request) { throw new InvalidOAuth2Request('Not Implemented!'); } }