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-01-16 00:07:26 +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;
|
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;
|
2016-01-15 18:32:47 +05:30
|
|
|
use Zend\Diactoros\Response;
|
2015-10-14 14:21:53 +05:30
|
|
|
use Zend\Diactoros\ServerRequestFactory;
|
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 = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var DateInterval[]
|
|
|
|
*/
|
2016-01-20 16:51:44 +05:30
|
|
|
protected $grantTypeTokensTTL = [];
|
2015-04-05 21:33:06 +05:30
|
|
|
|
2016-01-15 17:11:48 +05:30
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-01-17 19:53:02 +05:30
|
|
|
protected $privateKeyPath;
|
2016-01-15 17:11:48 +05:30
|
|
|
|
2015-04-05 21:33:06 +05:30
|
|
|
/**
|
2015-11-13 23:11:05 +05:30
|
|
|
* @var ResponseTypeInterface
|
2015-04-05 21:33:06 +05:30
|
|
|
*/
|
2016-01-17 19:53:18 +05:30
|
|
|
protected $responseType;
|
2015-04-05 21:33:06 +05:30
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-01-17 19:53:02 +05:30
|
|
|
private $publicKeyPath;
|
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-01-17 19:33:41 +05:30
|
|
|
* New server instance
|
2015-04-05 21:33:06 +05:30
|
|
|
*
|
2016-01-17 19:33:41 +05:30
|
|
|
* @param \League\OAuth2\Server\Repositories\ClientRepositoryInterface $clientRepository
|
|
|
|
* @param \League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface $accessTokenRepository
|
|
|
|
* @param \League\OAuth2\Server\Repositories\ScopeRepositoryInterface $scopeRepository
|
2016-01-17 19:53:02 +05:30
|
|
|
* @param string $privateKeyPath
|
|
|
|
* @param string $publicKeyPath
|
2016-01-17 19:33:41 +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-01-17 19:53:02 +05:30
|
|
|
$privateKeyPath,
|
|
|
|
$publicKeyPath,
|
2016-01-17 19:33:41 +05:30
|
|
|
ResponseTypeInterface $responseType = null
|
|
|
|
) {
|
|
|
|
$this->clientRepository = $clientRepository;
|
|
|
|
$this->accessTokenRepository = $accessTokenRepository;
|
|
|
|
$this->scopeRepository = $scopeRepository;
|
2016-01-17 19:53:02 +05:30
|
|
|
$this->privateKeyPath = $privateKeyPath;
|
|
|
|
$this->publicKeyPath = $publicKeyPath;
|
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
|
|
|
|
|
|
|
/**
|
2015-10-14 14:21:53 +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-01-20 16:51:44 +05:30
|
|
|
* @param DateInterval|null $accessTokenTTL
|
|
|
|
* @param DateInterval|null $refreshTokenTTL
|
2015-04-05 21:33:06 +05:30
|
|
|
*/
|
|
|
|
public function enableGrantType(
|
2015-10-14 14:21:53 +05:30
|
|
|
GrantTypeInterface $grantType,
|
2016-01-20 16:51:44 +05:30
|
|
|
\DateInterval $accessTokenTTL,
|
|
|
|
\DateInterval $refreshTokenTTL = null
|
2015-04-05 21:33:06 +05:30
|
|
|
) {
|
2016-01-17 19:33:07 +05:30
|
|
|
$grantType->setAccessTokenRepository($this->accessTokenRepository);
|
|
|
|
$grantType->setClientRepository($this->clientRepository);
|
|
|
|
$grantType->setScopeRepository($this->scopeRepository);
|
2016-01-17 19:53:02 +05:30
|
|
|
$grantType->setPathToPrivateKey($this->privateKeyPath);
|
|
|
|
$grantType->setPathToPublicKey($this->publicKeyPath);
|
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-20 16:51:44 +05:30
|
|
|
$this->grantTypeTokensTTL[$grantType->getIdentifier()] = [
|
|
|
|
'access' => $accessTokenTTL,
|
|
|
|
'refresh' => $refreshTokenTTL !== null ? $refreshTokenTTL : new \DateInterval('P1M'),
|
|
|
|
];
|
2015-04-05 21:33:06 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return an access token response
|
|
|
|
*
|
2016-01-16 00:06:34 +05:30
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface|null $request
|
|
|
|
* @param \Psr\Http\Message\ResponseInterface|null $response
|
2015-04-05 21:33:06 +05:30
|
|
|
*
|
2016-01-15 18:32:47 +05:30
|
|
|
* @return \Psr\Http\Message\ResponseInterface
|
2015-11-13 23:11:05 +05:30
|
|
|
* @throws \League\OAuth2\Server\Exception\OAuthServerException
|
2015-04-05 21:33:06 +05:30
|
|
|
*/
|
2016-01-15 18:32:47 +05:30
|
|
|
public function respondToRequest(ServerRequestInterface $request = null, ResponseInterface $response = null)
|
2015-04-05 21:33:06 +05:30
|
|
|
{
|
2016-01-15 18:32:47 +05:30
|
|
|
if (!$request instanceof ServerRequestInterface) {
|
2015-10-14 14:21:53 +05:30
|
|
|
$request = ServerRequestFactory::fromGlobals();
|
2015-04-05 21:33:06 +05:30
|
|
|
}
|
|
|
|
|
2016-01-15 18:32:47 +05:30
|
|
|
if (!$response instanceof ResponseInterface) {
|
|
|
|
$response = new Response();
|
|
|
|
}
|
|
|
|
|
2015-11-13 23:11:05 +05:30
|
|
|
$tokenResponse = null;
|
2015-10-14 14:21:53 +05:30
|
|
|
foreach ($this->enabledGrantTypes as $grantType) {
|
|
|
|
if ($grantType->canRespondToRequest($request)) {
|
2015-11-13 23:11:05 +05:30
|
|
|
$tokenResponse = $grantType->respondToRequest(
|
2015-10-14 14:21:53 +05:30
|
|
|
$request,
|
2016-01-20 16:51:44 +05:30
|
|
|
$this->getResponseType(),
|
|
|
|
$this->grantTypeTokensTTL[$grantType->getIdentifier()]['access'],
|
|
|
|
$this->grantTypeTokensTTL[$grantType->getIdentifier()]['refresh']
|
2015-10-14 14:21:53 +05:30
|
|
|
);
|
|
|
|
}
|
2015-04-05 21:33:06 +05:30
|
|
|
}
|
|
|
|
|
2016-01-15 18:32:47 +05:30
|
|
|
if (!$tokenResponse instanceof ResponseTypeInterface) {
|
|
|
|
return OAuthServerException::unsupportedGrantType()->generateHttpResponse($response);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $tokenResponse->generateHttpResponse($response);
|
|
|
|
}
|
2016-01-20 16:51:44 +05:30
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the token type that grants will return in the HTTP response
|
|
|
|
*
|
|
|
|
* @return ResponseTypeInterface
|
|
|
|
*/
|
|
|
|
public function getResponseType()
|
|
|
|
{
|
|
|
|
if (!$this->responseType instanceof ResponseTypeInterface) {
|
|
|
|
$this->responseType = new BearerTokenResponse(
|
|
|
|
$this->privateKeyPath,
|
|
|
|
$this->publicKeyPath,
|
|
|
|
$this->accessTokenRepository
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->responseType;
|
|
|
|
}
|
2015-04-05 21:33:06 +05:30
|
|
|
}
|