oauth2-server/src/AuthorizationServer.php

214 lines
6.4 KiB
PHP
Raw Normal View History

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/
*
* @link https://github.com/thephpleague/oauth2-server
*/
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;
/**
* @var string
*/
private $encryptionKey;
/**
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
* @param CryptKey|string $publicKey
* @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,
$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 === false) {
2016-03-28 20:12:34 +05:30
$privateKey = new CryptKey($privateKey);
}
$this->privateKey = $privateKey;
if ($publicKey instanceof CryptKey === false) {
2016-03-28 20:12:34 +05:30
$publicKey = new CryptKey($publicKey);
}
$this->publicKey = $publicKey;
$this->responseType = $responseType;
}
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)
{
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);
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());
$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
{
foreach ($this->enabledGrantTypes as $grantType) {
2016-04-10 16:18:32 +05:30
if ($grantType->canRespondToAuthorizationRequest($request)) {
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
*/
public function respondToAccessTokenRequest(ServerRequestInterface $request, ResponseInterface $response)
2015-04-05 21:33:06 +05:30
{
foreach ($this->enabledGrantTypes as $grantType) {
2016-04-09 20:52:22 +05:30
if ($grantType->canRespondToAccessTokenRequest($request)) {
2016-04-10 16:18:32 +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
}
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 === 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);
$this->responseType->setEncryptionKey($this->encryptionKey);
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
}