oauth2-server/src/AuthorizationServer.php

226 lines
6.6 KiB
PHP
Raw Normal View History

2015-04-05 17:03:06 +01:00
<?php
2016-04-17 13:06:05 +01:00
/**
* @author Alex Bilbie <hello@alexbilbie.com>
* @copyright Copyright (c) Alex Bilbie
* @license http://mit-license.org/
2017-10-23 15:26:10 +00:00
*
2016-04-17 13:06:05 +01:00
* @link https://github.com/thephpleague/oauth2-server
*/
2015-04-05 17:03:06 +01:00
namespace League\OAuth2\Server;
2015-10-14 09:51:53 +01:00
use League\Event\EmitterAwareInterface;
use League\Event\EmitterAwareTrait;
2015-11-13 17:41:05 +00:00
use League\OAuth2\Server\Exception\OAuthServerException;
2015-10-14 09:51:53 +01:00
use League\OAuth2\Server\Grant\GrantTypeInterface;
2016-01-17 14:03:41 +00:00
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
2016-04-10 11:48:32 +01:00
use League\OAuth2\Server\RequestTypes\AuthorizationRequest;
2015-11-13 17:41:05 +00:00
use League\OAuth2\Server\ResponseTypes\BearerTokenResponse;
use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface;
2016-01-15 14:02:47 +01:00
use Psr\Http\Message\ResponseInterface;
2015-10-14 09:51:53 +01:00
use Psr\Http\Message\ServerRequestInterface;
2015-04-05 17:03:06 +01:00
2016-04-17 12:54:25 +01:00
class AuthorizationServer implements EmitterAwareInterface
2015-04-05 17:03:06 +01:00
{
2015-10-14 09:51:53 +01:00
use EmitterAwareTrait;
2015-04-05 17:03:06 +01:00
/**
2016-07-09 01:00:44 +02:00
* @var GrantTypeInterface[]
2015-04-05 17:03:06 +01:00
*/
protected $enabledGrantTypes = [];
/**
2016-02-12 14:19:47 +01:00
* @var \DateInterval[]
2015-04-05 17:03:06 +01:00
*/
protected $grantTypeAccessTokenTTL = [];
2016-01-15 12:41:48 +01:00
/**
2016-07-09 01:00:44 +02:00
* @var CryptKey
2016-01-15 12:41:48 +01:00
*/
2016-03-28 16:42:34 +02:00
protected $privateKey;
2016-01-15 12:41:48 +01:00
2015-04-05 17:03:06 +01:00
/**
2016-07-09 01:00:44 +02:00
* @var CryptKey
2015-04-05 17:03:06 +01:00
*/
2016-03-28 16:42:34 +02:00
protected $publicKey;
2015-04-05 17:03:06 +01:00
/**
2016-07-09 01:00:44 +02:00
* @var null|ResponseTypeInterface
2015-04-05 17:03:06 +01:00
*/
2016-03-28 16:42:34 +02:00
protected $responseType;
2015-04-05 17:03:06 +01:00
2015-10-14 09:51:53 +01:00
/**
2016-07-09 01:00:44 +02:00
* @var ClientRepositoryInterface
2015-10-14 09:51:53 +01:00
*/
2016-01-17 14:03:41 +00:00
private $clientRepository;
/**
2016-07-09 01:00:44 +02:00
* @var AccessTokenRepositoryInterface
2016-01-17 14:03:41 +00:00
*/
private $accessTokenRepository;
/**
2016-07-09 01:00:44 +02:00
* @var ScopeRepositoryInterface
2016-01-17 14:03:41 +00:00
*/
private $scopeRepository;
/**
* @var string
*/
private $encryptionKey;
/**
* @var string
*/
private $defaultScope = '';
/**
2016-02-19 18:09:39 -05:00
* New server instance.
2015-04-05 17:03:06 +01:00
*
2016-07-09 01:00:44 +02:00
* @param ClientRepositoryInterface $clientRepository
* @param AccessTokenRepositoryInterface $accessTokenRepository
* @param ScopeRepositoryInterface $scopeRepository
* @param CryptKey|string $privateKey
* @param string $encryptionKey
2016-07-09 01:00:44 +02:00
* @param null|ResponseTypeInterface $responseType
2015-04-05 17:03:06 +01:00
*/
2016-01-17 14:03:41 +00:00
public function __construct(
ClientRepositoryInterface $clientRepository,
AccessTokenRepositoryInterface $accessTokenRepository,
ScopeRepositoryInterface $scopeRepository,
2016-03-28 16:42:34 +02:00
$privateKey,
$encryptionKey,
2016-04-17 12:42:42 +01:00
ResponseTypeInterface $responseType = null
2016-01-17 14:03:41 +00:00
) {
$this->clientRepository = $clientRepository;
$this->accessTokenRepository = $accessTokenRepository;
$this->scopeRepository = $scopeRepository;
2016-03-28 16:42:34 +02:00
if ($privateKey instanceof CryptKey === false) {
2016-03-28 16:42:34 +02:00
$privateKey = new CryptKey($privateKey);
}
$this->privateKey = $privateKey;
$this->encryptionKey = $encryptionKey;
$this->responseType = $responseType;
}
2015-04-05 17:03:06 +01:00
/**
2016-02-19 18:09:39 -05:00
* Enable a grant type on the server.
2015-04-05 17:03:06 +01:00
*
2016-07-09 01:00:44 +02:00
* @param GrantTypeInterface $grantType
* @param null|\DateInterval $accessTokenTTL
2015-04-05 17:03:06 +01:00
*/
2016-07-09 01:00:44 +02:00
public function enableGrantType(GrantTypeInterface $grantType, \DateInterval $accessTokenTTL = null)
{
if ($accessTokenTTL instanceof \DateInterval === false) {
2016-04-10 14:31:05 +01:00
$accessTokenTTL = new \DateInterval('PT1H');
}
2016-01-17 14:03:07 +00:00
$grantType->setAccessTokenRepository($this->accessTokenRepository);
$grantType->setClientRepository($this->clientRepository);
$grantType->setScopeRepository($this->scopeRepository);
2017-11-13 22:20:16 +00:00
$grantType->setDefaultScope($this->defaultScope);
2016-03-28 16:42:34 +02:00
$grantType->setPrivateKey($this->privateKey);
2015-10-14 09:51:53 +01:00
$grantType->setEmitter($this->getEmitter());
$grantType->setEncryptionKey($this->encryptionKey);
2016-01-20 12:21:44 +01:00
$this->enabledGrantTypes[$grantType->getIdentifier()] = $grantType;
2016-01-17 14:03:33 +00:00
$this->grantTypeAccessTokenTTL[$grantType->getIdentifier()] = $accessTokenTTL;
2015-04-05 17:03:06 +01:00
}
2016-04-10 10:07:08 +01:00
/**
2016-04-10 11:48:32 +01:00
* Validate an authorization request
*
2016-07-09 01:00:44 +02:00
* @param ServerRequestInterface $request
2016-04-10 10:07:08 +01:00
*
2016-07-09 01:00:44 +02:00
* @throws OAuthServerException
2016-04-10 06:48:46 -04:00
*
2016-07-09 01:00:44 +02:00
* @return AuthorizationRequest
2016-04-10 10:07:08 +01:00
*/
2016-04-10 11:48:32 +01:00
public function validateAuthorizationRequest(ServerRequestInterface $request)
2016-04-10 10:07:08 +01:00
{
foreach ($this->enabledGrantTypes as $grantType) {
2016-04-10 11:48:32 +01:00
if ($grantType->canRespondToAuthorizationRequest($request)) {
return $grantType->validateAuthorizationRequest($request);
2016-04-10 10:07:08 +01:00
}
}
2016-04-10 11:48:32 +01:00
2016-04-10 10:07:08 +01:00
throw OAuthServerException::unsupportedGrantType();
}
2016-04-10 11:48:32 +01:00
/**
* Complete an authorization request
*
2016-07-09 01:00:44 +02:00
* @param AuthorizationRequest $authRequest
* @param ResponseInterface $response
2016-04-10 11:48:32 +01:00
*
2016-07-09 01:00:44 +02:00
* @return ResponseInterface
2016-04-10 11:48:32 +01:00
*/
public function completeAuthorizationRequest(AuthorizationRequest $authRequest, ResponseInterface $response)
{
return $this->enabledGrantTypes[$authRequest->getGrantTypeId()]
->completeAuthorizationRequest($authRequest)
->generateHttpResponse($response);
}
2015-04-05 17:03:06 +01:00
/**
2016-02-19 18:09:39 -05:00
* Return an access token response.
2015-04-05 17:03:06 +01:00
*
2016-07-09 01:00:44 +02:00
* @param ServerRequestInterface $request
* @param ResponseInterface $response
2015-04-05 17:03:06 +01:00
*
2016-07-09 01:00:44 +02:00
* @throws OAuthServerException
2016-02-19 18:09:39 -05:00
*
2016-07-09 01:00:44 +02:00
* @return ResponseInterface
2015-04-05 17:03:06 +01:00
*/
public function respondToAccessTokenRequest(ServerRequestInterface $request, ResponseInterface $response)
2015-04-05 17:03:06 +01:00
{
foreach ($this->enabledGrantTypes as $grantType) {
2017-11-23 21:26:39 +03:30
if (!$grantType->canRespondToAccessTokenRequest($request)) {
continue;
}
2017-11-23 21:26:39 +03:30
$tokenResponse = $grantType->respondToAccessTokenRequest(
$request,
$this->getResponseType(),
$this->grantTypeAccessTokenTTL[$grantType->getIdentifier()]
);
if ($tokenResponse instanceof ResponseTypeInterface) {
return $tokenResponse->generateHttpResponse($response);
}
2016-01-15 14:02:47 +01:00
}
throw OAuthServerException::unsupportedGrantType();
2016-01-15 14:02:47 +01:00
}
2016-01-20 12:21:44 +01:00
/**
2016-02-19 18:09:39 -05:00
* Get the token type that grants will return in the HTTP response.
2016-01-20 12:21:44 +01:00
*
* @return ResponseTypeInterface
*/
2016-02-12 14:19:47 +01:00
protected function getResponseType()
2016-01-20 12:21:44 +01:00
{
if ($this->responseType instanceof ResponseTypeInterface === false) {
2016-04-18 12:10:57 +01:00
$this->responseType = new BearerTokenResponse();
2016-01-20 12:21:44 +01:00
}
2016-03-28 16:42:34 +02:00
$this->responseType->setPrivateKey($this->privateKey);
$this->responseType->setEncryptionKey($this->encryptionKey);
2016-03-17 14:37:21 +00:00
2016-01-20 12:21:44 +01:00
return $this->responseType;
}
/**
* Set the default scope for the authorization server.
*
* @param string $defaultScope
*/
public function setDefaultScope($defaultScope)
{
$this->defaultScope = $defaultScope;
}
2015-04-05 17:03:06 +01:00
}