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;
|
|
|
|
|
|
|
|
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;
|
2015-11-13 23:11:05 +05:30
|
|
|
use League\OAuth2\Server\ResponseTypes\BearerTokenResponse;
|
|
|
|
use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface;
|
2015-10-14 14:21:53 +05:30
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
|
|
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 = [];
|
|
|
|
|
|
|
|
/**
|
2015-11-13 23:11:05 +05:30
|
|
|
* @var ResponseTypeInterface[]
|
2015-04-05 21:33:06 +05:30
|
|
|
*/
|
2015-11-13 23:11:05 +05:30
|
|
|
protected $grantResponseTypes = [];
|
2015-04-05 21:33:06 +05:30
|
|
|
|
|
|
|
/**
|
|
|
|
* @var DateInterval[]
|
|
|
|
*/
|
|
|
|
protected $grantTypeAccessTokenTTL = [];
|
|
|
|
|
|
|
|
/**
|
2015-11-13 23:11:05 +05:30
|
|
|
* @var ResponseTypeInterface
|
2015-04-05 21:33:06 +05:30
|
|
|
*/
|
2015-11-13 23:11:05 +05:30
|
|
|
protected $defaultResponseType;
|
2015-04-05 21:33:06 +05:30
|
|
|
|
|
|
|
/**
|
|
|
|
* @var DateInterval
|
|
|
|
*/
|
|
|
|
protected $defaultAccessTokenTTL;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2015-10-14 14:21:53 +05:30
|
|
|
protected $scopeDelimiterString = ' ';
|
2015-04-05 21:33:06 +05:30
|
|
|
|
2015-10-14 14:21:53 +05:30
|
|
|
/**
|
|
|
|
* New server instance
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
2015-11-13 23:11:05 +05:30
|
|
|
$this->setDefaultResponseType(new BearerTokenResponse());
|
|
|
|
$this->setDefaultAccessTokenTTL(new DateInterval('PT01H')); // default token TTL of 1 hour
|
2015-04-06 13:02:44 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the default token type that grants will return
|
2015-04-05 21:33:06 +05:30
|
|
|
*
|
2015-11-13 23:11:05 +05:30
|
|
|
* @param ResponseTypeInterface $defaultTokenType
|
2015-04-05 21:33:06 +05:30
|
|
|
*/
|
2015-11-13 23:11:05 +05:30
|
|
|
public function setDefaultResponseType(ResponseTypeInterface $defaultTokenType)
|
2015-04-06 13:02:44 +05:30
|
|
|
{
|
2015-11-13 23:11:05 +05:30
|
|
|
$this->defaultResponseType = $defaultTokenType;
|
2015-04-06 13:02:44 +05:30
|
|
|
}
|
2015-04-05 21:33:06 +05:30
|
|
|
|
2015-04-06 13:02:44 +05:30
|
|
|
/**
|
2015-10-14 14:21:53 +05:30
|
|
|
* Set the delimiter string used to separate scopes in a request
|
2015-04-06 13:02:44 +05:30
|
|
|
*
|
2015-10-14 14:21:53 +05:30
|
|
|
* @param string $scopeDelimiterString
|
2015-04-06 13:02:44 +05:30
|
|
|
*/
|
2015-10-14 14:21:53 +05:30
|
|
|
public function setScopeDelimiterString($scopeDelimiterString)
|
2015-04-06 13:02:44 +05:30
|
|
|
{
|
2015-10-14 14:21:53 +05:30
|
|
|
$this->scopeDelimiterString = $scopeDelimiterString;
|
2015-04-06 13:02:44 +05:30
|
|
|
}
|
2015-04-05 21:33:06 +05:30
|
|
|
|
2015-04-06 13:02:44 +05:30
|
|
|
/**
|
|
|
|
* Set the default TTL of access tokens
|
|
|
|
*
|
|
|
|
* @param DateInterval $defaultAccessTokenTTL
|
|
|
|
*/
|
|
|
|
public function setDefaultAccessTokenTTL(DateInterval $defaultAccessTokenTTL)
|
|
|
|
{
|
|
|
|
$this->defaultAccessTokenTTL = $defaultAccessTokenTTL;
|
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
|
2015-11-13 23:11:05 +05:30
|
|
|
* @param ResponseTypeInterface $responseType
|
2015-10-14 14:21:53 +05:30
|
|
|
* @param DateInterval $accessTokenTTL
|
2015-04-05 21:33:06 +05:30
|
|
|
*/
|
|
|
|
public function enableGrantType(
|
2015-10-14 14:21:53 +05:30
|
|
|
GrantTypeInterface $grantType,
|
2015-11-13 23:11:05 +05:30
|
|
|
ResponseTypeInterface $responseType = null,
|
2015-04-05 21:33:06 +05:30
|
|
|
DateInterval $accessTokenTTL = null
|
|
|
|
) {
|
2015-10-14 14:21:53 +05:30
|
|
|
$grantType->setEmitter($this->getEmitter());
|
|
|
|
$this->enabledGrantTypes[$grantType->getIdentifier()] = $grantType;
|
2015-04-05 21:33:06 +05:30
|
|
|
|
|
|
|
// Set grant response type
|
2015-11-13 23:11:05 +05:30
|
|
|
if ($responseType instanceof ResponseTypeInterface) {
|
|
|
|
$this->grantResponseTypes[$grantType->getIdentifier()] = $responseType;
|
2015-04-05 21:33:06 +05:30
|
|
|
} else {
|
2015-11-13 23:11:05 +05:30
|
|
|
$this->grantResponseTypes[$grantType->getIdentifier()] = $this->defaultResponseType;
|
2015-04-05 21:33:06 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// Set grant access token TTL
|
|
|
|
if ($accessTokenTTL instanceof DateInterval) {
|
2015-10-14 14:21:53 +05:30
|
|
|
$this->grantTypeAccessTokenTTL[$grantType->getIdentifier()] = $accessTokenTTL;
|
2015-04-05 21:33:06 +05:30
|
|
|
} else {
|
2015-10-14 14:21:53 +05:30
|
|
|
$this->grantTypeAccessTokenTTL[$grantType->getIdentifier()] = $this->defaultAccessTokenTTL;
|
2015-04-05 21:33:06 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return an access token response
|
|
|
|
*
|
2015-10-14 14:21:53 +05:30
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request
|
2015-04-05 21:33:06 +05:30
|
|
|
*
|
2015-11-13 23:11:05 +05:30
|
|
|
* @return \League\OAuth2\Server\ResponseTypes\ResponseTypeInterface
|
|
|
|
* @throws \League\OAuth2\Server\Exception\OAuthServerException
|
2015-04-05 21:33:06 +05:30
|
|
|
*/
|
2015-10-14 14:21:53 +05:30
|
|
|
public function respondToRequest(ServerRequestInterface $request = null)
|
2015-04-05 21:33:06 +05:30
|
|
|
{
|
|
|
|
if ($request === null) {
|
2015-10-14 14:21:53 +05:30
|
|
|
$request = ServerRequestFactory::fromGlobals();
|
2015-04-05 21:33:06 +05:30
|
|
|
}
|
|
|
|
|
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,
|
2015-11-13 23:11:05 +05:30
|
|
|
$this->grantResponseTypes[$grantType->getIdentifier()],
|
2015-10-14 14:21:53 +05:30
|
|
|
$this->grantTypeAccessTokenTTL[$grantType->getIdentifier()],
|
|
|
|
$this->scopeDelimiterString
|
|
|
|
);
|
|
|
|
}
|
2015-04-05 21:33:06 +05:30
|
|
|
}
|
|
|
|
|
2015-11-13 23:11:05 +05:30
|
|
|
if ($tokenResponse instanceof ResponseTypeInterface) {
|
|
|
|
return $tokenResponse->generateHttpResponse();
|
|
|
|
} else {
|
|
|
|
$response = OAuthServerException::unsupportedGrantType()->generateHttpResponse();
|
2015-10-14 14:21:53 +05:30
|
|
|
}
|
2015-04-05 21:33:06 +05:30
|
|
|
|
2015-10-14 14:21:53 +05:30
|
|
|
return $response;
|
2015-04-05 21:33:06 +05:30
|
|
|
}
|
|
|
|
}
|