2015-04-05 21:33:06 +05:30
|
|
|
<?php
|
2015-04-06 13:02:44 +05:30
|
|
|
|
2015-04-05 21:33:06 +05:30
|
|
|
namespace League\OAuth2\Server;
|
|
|
|
|
2016-02-12 19:48:34 +05:30
|
|
|
use DateInterval;
|
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
|
|
|
/**
|
|
|
|
* @var \League\OAuth2\Server\Grant\GrantTypeInterface[]
|
|
|
|
*/
|
|
|
|
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-03-28 20:12:34 +05:30
|
|
|
* @var \League\OAuth2\Server\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-03-28 20:12:34 +05:30
|
|
|
* @var \League\OAuth2\Server\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-03-28 20:12:34 +05:30
|
|
|
* @var 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-01-17 19:33:41 +05:30
|
|
|
* @var \League\OAuth2\Server\Repositories\ClientRepositoryInterface
|
2015-10-14 14:21:53 +05:30
|
|
|
*/
|
2016-01-17 19:33:41 +05:30
|
|
|
private $clientRepository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface
|
|
|
|
*/
|
|
|
|
private $accessTokenRepository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \League\OAuth2\Server\Repositories\ScopeRepositoryInterface
|
|
|
|
*/
|
|
|
|
private $scopeRepository;
|
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-03-17 20:07:48 +05:30
|
|
|
* @param \League\OAuth2\Server\Repositories\ClientRepositoryInterface $clientRepository
|
|
|
|
* @param \League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface $accessTokenRepository
|
|
|
|
* @param \League\OAuth2\Server\Repositories\ScopeRepositoryInterface $scopeRepository
|
2016-03-28 20:12:34 +05:30
|
|
|
* @param \League\OAuth2\Server\CryptKey|string $privateKey
|
|
|
|
* @param \League\OAuth2\Server\CryptKey|string $publicKey
|
2016-03-17 20:07:48 +05:30
|
|
|
* @param null|\League\OAuth2\Server\ResponseTypes\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,
|
|
|
|
$publicKey,
|
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
|
|
|
|
|
|
|
if (!$privateKey instanceof CryptKey) {
|
|
|
|
$privateKey = new CryptKey($privateKey);
|
|
|
|
}
|
|
|
|
$this->privateKey = $privateKey;
|
|
|
|
|
|
|
|
if (!$publicKey instanceof CryptKey) {
|
|
|
|
$publicKey = new CryptKey($publicKey);
|
|
|
|
}
|
|
|
|
$this->publicKey = $publicKey;
|
|
|
|
|
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
|
|
|
*
|
2015-10-14 14:21:53 +05:30
|
|
|
* @param \League\OAuth2\Server\Grant\GrantTypeInterface $grantType
|
2016-02-12 18:49:47 +05:30
|
|
|
* @param \DateInterval $accessTokenTTL
|
2015-04-05 21:33:06 +05:30
|
|
|
*/
|
2016-04-10 19:01:05 +05:30
|
|
|
public function enableGrantType(GrantTypeInterface $grantType, DateInterval $accessTokenTTL = null)
|
2016-01-21 22:41:53 +05:30
|
|
|
{
|
2016-04-10 19:01:05 +05:30
|
|
|
if ($accessTokenTTL instanceof DateInterval === false) {
|
|
|
|
$accessTokenTTL = new \DateInterval('PT1H');
|
|
|
|
}
|
|
|
|
|
2016-01-17 19:33:07 +05:30
|
|
|
$grantType->setAccessTokenRepository($this->accessTokenRepository);
|
|
|
|
$grantType->setClientRepository($this->clientRepository);
|
|
|
|
$grantType->setScopeRepository($this->scopeRepository);
|
2016-03-28 20:12:34 +05:30
|
|
|
$grantType->setPrivateKey($this->privateKey);
|
|
|
|
$grantType->setPublicKey($this->publicKey);
|
2015-10-14 14:21:53 +05:30
|
|
|
$grantType->setEmitter($this->getEmitter());
|
2015-04-05 21:33:06 +05:30
|
|
|
|
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-04-10 14:37:08 +05:30
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request
|
|
|
|
*
|
|
|
|
* @throws \League\OAuth2\Server\Exception\OAuthServerException
|
2016-04-10 16:18:46 +05:30
|
|
|
*
|
|
|
|
* @return \League\OAuth2\Server\RequestTypes\AuthorizationRequest|null
|
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
|
|
|
{
|
|
|
|
$authRequest = null;
|
2016-04-10 16:18:32 +05:30
|
|
|
$enabledGrantTypes = $this->enabledGrantTypes;
|
|
|
|
while ($authRequest === null && $grantType = array_shift($enabledGrantTypes)) {
|
2016-04-10 14:37:08 +05:30
|
|
|
/** @var \League\OAuth2\Server\Grant\GrantTypeInterface $grantType */
|
2016-04-10 16:18:32 +05:30
|
|
|
if ($grantType->canRespondToAuthorizationRequest($request)) {
|
|
|
|
$authRequest = $grantType->validateAuthorizationRequest($request);
|
|
|
|
|
|
|
|
return $authRequest;
|
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
|
|
|
|
*
|
|
|
|
* @param \League\OAuth2\Server\RequestTypes\AuthorizationRequest $authRequest
|
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response
|
|
|
|
*
|
|
|
|
* @return \League\OAuth2\Server\ResponseTypes\ResponseTypeInterface
|
|
|
|
*/
|
|
|
|
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-03-28 20:12:34 +05:30
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request
|
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response
|
2015-04-05 21:33:06 +05:30
|
|
|
*
|
2015-11-13 23:11:05 +05:30
|
|
|
* @throws \League\OAuth2\Server\Exception\OAuthServerException
|
2016-02-20 04:39:39 +05:30
|
|
|
*
|
|
|
|
* @return \Psr\Http\Message\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-03-18 04:55:32 +05:30
|
|
|
$tokenResponse = null;
|
|
|
|
while ($tokenResponse === null && $grantType = array_shift($this->enabledGrantTypes)) {
|
|
|
|
/** @var \League\OAuth2\Server\Grant\GrantTypeInterface $grantType */
|
2016-04-09 20:52:22 +05:30
|
|
|
if ($grantType->canRespondToAccessTokenRequest($request)) {
|
2016-04-10 16:18:32 +05:30
|
|
|
$tokenResponse = $grantType->respondToAccessTokenRequest(
|
2016-03-18 04:55:32 +05:30
|
|
|
$request,
|
|
|
|
$this->getResponseType(),
|
|
|
|
$this->grantTypeAccessTokenTTL[$grantType->getIdentifier()]
|
|
|
|
);
|
2016-03-17 20:07:21 +05:30
|
|
|
}
|
2016-03-18 04:55:32 +05:30
|
|
|
}
|
2016-02-12 19:48:45 +05:30
|
|
|
|
2016-03-18 04:55:32 +05:30
|
|
|
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
|
|
|
{
|
|
|
|
if (!$this->responseType instanceof ResponseTypeInterface) {
|
2016-03-17 20:07:21 +05:30
|
|
|
$this->responseType = new BearerTokenResponse($this->accessTokenRepository);
|
2016-01-20 16:51:44 +05:30
|
|
|
}
|
|
|
|
|
2016-03-28 20:12:34 +05:30
|
|
|
$this->responseType->setPrivateKey($this->privateKey);
|
2016-03-17 20:07:21 +05:30
|
|
|
|
2016-01-20 16:51:44 +05:30
|
|
|
return $this->responseType;
|
|
|
|
}
|
2015-04-05 21:33:06 +05:30
|
|
|
}
|