defaultResponseType = ($defaultResponseType instanceof TokenTypeInterface) ? $defaultResponseType : new BearerTokenType(); $this->defaultAccessTokenTTL = ($defaultAccessTokenTTL instanceof DateInterval) ? $defaultAccessTokenTTL : new DateInterval('PT01H'); // default of 1 hour parent::__construct(); } /** * @param string $grantType * @param TokenTypeInterface $tokenType * @param DateInterval $accessTokenTTL * * @throws \Exception */ public function enableGrantType( $grantType, TokenTypeInterface $tokenType = null, DateInterval $accessTokenTTL = null ) { if ($this->getContainer()->isInServiceProvider($grantType)) { $grant = $this->getContainer()->get($grantType); $grantIdentifier = $grant->getIdentifier(); $this->enabledGrantTypes[$grantIdentifier] = $grant; } else { throw new \Exception('Unregistered grant type'); } // Set grant response type if ($tokenType instanceof TokenTypeInterface) { $this->grantTypeTokenTypes[$grantIdentifier] = $tokenType; } else { $this->grantTypeTokenTypes[$grantIdentifier] = $this->defaultResponseType; } // Set grant access token TTL if ($accessTokenTTL instanceof DateInterval) { $this->grantTypeAccessTokenTTL[$grantIdentifier] = $accessTokenTTL; } else { $this->grantTypeAccessTokenTTL[$grantIdentifier] = $this->defaultAccessTokenTTL; } } /** * Return an access token response * * @param \Symfony\Component\HttpFoundation\Request $request * * @return TokenTypeInterface * @throws \Exception */ public function getAccessTokenResponse(Request $request = null) { if ($request === null) { $request = Request::createFromGlobals(); } // Run the requested grant type $grantType = $request->request->get('grant_type', null); if ($grantType === null || !isset($this->enabledGrantTypes[$grantType])) { throw new Exception\InvalidGrantException($grantType); } $tokenType = $this->enabledGrantTypes[$grantType]->getAccessTokenAsType( $request, $this->grantTypeTokenTypes[$grantType], $this->grantTypeAccessTokenTTL[$grantType], $this->scopeDelimiter ); return $tokenType->generateHttpResponse(); } /** * Set the delimiter used to separate scopes in a request * * @param string $scopeDelimiter */ public function setScopeDelimiter($scopeDelimiter) { $this->scopeDelimiter = $scopeDelimiter; } }