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