oauth2-server/src/Server.php

224 lines
7.8 KiB
PHP
Raw Normal View History

2015-04-05 21:33:06 +05:30
<?php
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;
2016-03-17 20:07:21 +05:30
use League\OAuth2\Server\AuthorizationValidators\AuthorizationValidatorInterface;
use League\OAuth2\Server\AuthorizationValidators\BearerTokenValidator;
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;
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
2015-10-14 14:21:53 +05:30
class Server 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;
2016-03-17 20:07:21 +05:30
/**
* @var \League\OAuth2\Server\AuthorizationValidators\AuthorizationValidatorInterface
*/
private $authorizationValidator;
/**
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
2016-03-17 20:07:21 +05:30
* @param null|\League\OAuth2\Server\AuthorizationValidators\AuthorizationValidatorInterface $authorizationValidator
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-03-17 20:07:21 +05:30
ResponseTypeInterface $responseType = null,
AuthorizationValidatorInterface $authorizationValidator = 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;
$this->responseType = $responseType;
2016-03-17 20:07:21 +05:30
$this->authorizationValidator = $authorizationValidator;
}
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-02-12 19:48:34 +05:30
public function enableGrantType(GrantTypeInterface $grantType, DateInterval $accessTokenTTL)
{
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;
2015-04-05 21:33:06 +05:30
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
/**
* @param \Psr\Http\Message\ServerRequestInterface $request
*
* @throws \League\OAuth2\Server\Exception\OAuthServerException
*/
public function respondToAuthorizationRequest(ServerRequestInterface $request)
{
$authRequest = null;
while ($authRequest === null && $grantType = array_shift($this->enabledGrantTypes)) {
/** @var \League\OAuth2\Server\Grant\GrantTypeInterface $grantType */
if ($grantType->canRespondToAccessTokenRequest($request)) {
$authRequest = $grantType->respondToRequest(
$request,
$this->getResponseType(),
$this->grantTypeAccessTokenTTL[$grantType->getIdentifier()]
);
}
}
throw OAuthServerException::unsupportedGrantType();
}
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
*/
public function respondToAccessTokenRequest(ServerRequestInterface $request, ResponseInterface $response)
2015-04-05 21:33:06 +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)) {
$tokenResponse = $grantType->respondToRequest(
$request,
$this->getResponseType(),
$this->grantTypeAccessTokenTTL[$grantType->getIdentifier()]
);
2016-03-17 20:07:21 +05:30
}
}
if ($tokenResponse instanceof ResponseTypeInterface) {
return $tokenResponse->generateHttpResponse($response);
2016-01-15 18:32:47 +05:30
}
throw OAuthServerException::unsupportedGrantType();
2016-01-15 18:32:47 +05:30
}
2016-01-20 16:51:44 +05:30
2016-02-12 18:49:47 +05:30
/**
2016-02-20 04:39:39 +05:30
* Determine the access token validity.
2016-02-12 18:49:47 +05:30
*
* @param \Psr\Http\Message\ServerRequestInterface $request
*
* @throws \League\OAuth2\Server\Exception\OAuthServerException
2016-02-20 04:39:39 +05:30
*
* @return \Psr\Http\Message\ServerRequestInterface
2016-02-12 18:49:47 +05:30
*/
2016-02-13 19:08:23 +05:30
public function validateAuthenticatedRequest(ServerRequestInterface $request)
2016-02-12 18:49:47 +05:30
{
2016-03-17 20:07:21 +05:30
return $this->getAuthorizationValidator()->validateAuthorization($request);
2016-02-12 18:49: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;
}
2016-03-17 20:07:21 +05:30
/**
* @return \League\OAuth2\Server\AuthorizationValidators\AuthorizationValidatorInterface
*/
protected function getAuthorizationValidator()
{
if (!$this->authorizationValidator instanceof AuthorizationValidatorInterface) {
$this->authorizationValidator = new BearerTokenValidator($this->accessTokenRepository);
}
2016-03-28 20:12:34 +05:30
$this->authorizationValidator->setPublicKey($this->publicKey);
2016-03-17 20:07:21 +05:30
return $this->authorizationValidator;
}
2015-04-05 21:33:06 +05:30
}