clientRepository = $clientRepository; $this->accessTokenRepository = $accessTokenRepository; $this->scopeRepository = $scopeRepository; if (!$privateKey instanceof CryptKey) { $privateKey = new CryptKey($privateKey); } $this->privateKey = $privateKey; if (!$publicKey instanceof CryptKey) { $publicKey = new CryptKey($publicKey); } $this->publicKey = $publicKey; $this->responseType = $responseType; $this->authorizationValidator = $authorizationValidator; } /** * Enable a grant type on the server. * * @param \League\OAuth2\Server\Grant\GrantTypeInterface $grantType * @param \DateInterval $accessTokenTTL */ public function enableGrantType(GrantTypeInterface $grantType, DateInterval $accessTokenTTL) { $grantType->setAccessTokenRepository($this->accessTokenRepository); $grantType->setClientRepository($this->clientRepository); $grantType->setScopeRepository($this->scopeRepository); $grantType->setPrivateKey($this->privateKey); $grantType->setPublicKey($this->publicKey); $grantType->setEmitter($this->getEmitter()); $this->enabledGrantTypes[$grantType->getIdentifier()] = $grantType; $this->grantTypeAccessTokenTTL[$grantType->getIdentifier()] = $accessTokenTTL; } /** * Return an access token response. * * @param \Psr\Http\Message\ServerRequestInterface $request * @param \Psr\Http\Message\ResponseInterface $response * * @throws \League\OAuth2\Server\Exception\OAuthServerException * * @return \Psr\Http\Message\ResponseInterface */ public function respondToRequest(ServerRequestInterface $request, ResponseInterface $response) { $tokenResponse = null; while ($tokenResponse === null && $grantType = array_shift($this->enabledGrantTypes)) { /** @var \League\OAuth2\Server\Grant\GrantTypeInterface $grantType */ if ($grantType->canRespondToRequest($request)) { $tokenResponse = $grantType->respondToRequest( $request, $this->getResponseType(), $this->grantTypeAccessTokenTTL[$grantType->getIdentifier()] ); } } if ($tokenResponse instanceof ResponseTypeInterface) { return $tokenResponse->generateHttpResponse($response); } throw OAuthServerException::unsupportedGrantType(); } /** * Determine the access token validity. * * @param \Psr\Http\Message\ServerRequestInterface $request * * @throws \League\OAuth2\Server\Exception\OAuthServerException * * @return \Psr\Http\Message\ServerRequestInterface */ public function validateAuthenticatedRequest(ServerRequestInterface $request) { return $this->getAuthorizationValidator()->validateAuthorization($request); } /** * Get the token type that grants will return in the HTTP response. * * @return ResponseTypeInterface */ protected function getResponseType() { if (!$this->responseType instanceof ResponseTypeInterface) { $this->responseType = new BearerTokenResponse($this->accessTokenRepository); } $this->responseType->setPrivateKey($this->privateKey); return $this->responseType; } /** * @return \League\OAuth2\Server\AuthorizationValidators\AuthorizationValidatorInterface */ protected function getAuthorizationValidator() { if (!$this->authorizationValidator instanceof AuthorizationValidatorInterface) { $this->authorizationValidator = new BearerTokenValidator($this->accessTokenRepository); } $this->authorizationValidator->setPublicKey($this->publicKey); return $this->authorizationValidator; } }