oauth2-server/src/Server.php

243 lines
8.5 KiB
PHP
Raw Normal View History

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