oauth2-server/src/Server.php

138 lines
4.0 KiB
PHP
Raw Normal View History

2015-04-05 21:33:06 +05:30
<?php
namespace League\OAuth2\Server;
use DateInterval;
2015-04-06 01:44:22 +05:30
use League\OAuth2\Server\TokenTypes\BearerTokenType;
use League\OAuth2\Server\TokenTypes\TokenTypeInterface;
2015-04-05 21:33:06 +05:30
use Symfony\Component\HttpFoundation\Request;
class Server extends AbstractServer
{
/**
* @var \League\OAuth2\Server\Grant\GrantTypeInterface[]
*/
protected $enabledGrantTypes = [];
/**
2015-04-06 01:44:22 +05:30
* @var TokenTypeInterface[]
2015-04-05 21:33:06 +05:30
*/
2015-04-06 01:44:22 +05:30
protected $grantTypeTokenTypes = [];
2015-04-05 21:33:06 +05:30
/**
* @var DateInterval[]
*/
protected $grantTypeAccessTokenTTL = [];
/**
2015-04-06 01:44:22 +05:30
* @var TokenTypeInterface
2015-04-05 21:33:06 +05:30
*/
protected $defaultResponseType;
/**
* @var DateInterval
*/
protected $defaultAccessTokenTTL;
/**
* @var string
*/
protected $scopeDelimiter = ' ';
/**
* New server instance
*
2015-04-06 01:44:22 +05:30
* @param TokenTypeInterface $defaultResponseType
* @param DateInterval $defaultAccessTokenTTL
2015-04-05 21:33:06 +05:30
*/
public function __construct(
2015-04-06 01:44:22 +05:30
TokenTypeInterface $defaultResponseType = null,
2015-04-05 21:33:06 +05:30
DateInterval $defaultAccessTokenTTL = null
) {
2015-04-06 01:44:22 +05:30
$this->defaultResponseType = ($defaultResponseType instanceof TokenTypeInterface)
2015-04-05 21:33:06 +05:30
? $defaultResponseType
2015-04-06 01:44:22 +05:30
: new BearerTokenType();
2015-04-05 21:33:06 +05:30
$this->defaultAccessTokenTTL = ($defaultAccessTokenTTL instanceof DateInterval)
? $defaultAccessTokenTTL
: new DateInterval('PT01H'); // default of 1 hour
parent::__construct();
}
/**
2015-04-06 01:44:22 +05:30
* @param string $grantType
* @param TokenTypeInterface $tokenType
* @param DateInterval $accessTokenTTL
2015-04-05 21:33:06 +05:30
*
* @throws \Exception
*/
public function enableGrantType(
$grantType,
2015-04-06 01:44:22 +05:30
TokenTypeInterface $tokenType = null,
2015-04-05 21:33:06 +05:30
DateInterval $accessTokenTTL = null
) {
if ($this->getContainer()->isInServiceProvider($grantType)) {
$grant = $this->getContainer()->get($grantType);
$grantIdentifier = $grant->getIdentifier();
$this->enabledGrantTypes[$grantIdentifier] = $grant;
} else {
throw new \Exception('Unregistered grant type');
}
// Set grant response type
2015-04-06 01:44:22 +05:30
if ($tokenType instanceof TokenTypeInterface) {
$this->grantTypeTokenTypes[$grantIdentifier] = $tokenType;
2015-04-05 21:33:06 +05:30
} else {
2015-04-06 01:44:22 +05:30
$this->grantTypeTokenTypes[$grantIdentifier] = $this->defaultResponseType;
2015-04-05 21:33:06 +05:30
}
// Set grant access token TTL
if ($accessTokenTTL instanceof DateInterval) {
$this->grantTypeAccessTokenTTL[$grantIdentifier] = $accessTokenTTL;
} else {
$this->grantTypeAccessTokenTTL[$grantIdentifier] = $this->defaultAccessTokenTTL;
}
}
/**
* Return an access token response
*
* @param \Symfony\Component\HttpFoundation\Request $request
*
2015-04-06 01:44:22 +05:30
* @return TokenTypeInterface
2015-04-05 21:33:06 +05:30
* @throws \Exception
*/
public function getAccessTokenResponse(Request $request = null)
{
if ($request === null) {
$request = Request::createFromGlobals();
}
// Run the requested grant type
$grantType = $request->request->get('grant_type', null);
if ($grantType === null || !isset($this->enabledGrantTypes[$grantType])) {
2015-04-06 01:44:22 +05:30
throw new Exception\InvalidGrantException($grantType);
2015-04-05 21:33:06 +05:30
}
2015-04-06 01:44:22 +05:30
$tokenType = $this->enabledGrantTypes[$grantType]->getAccessTokenAsType(
2015-04-05 21:33:06 +05:30
$request,
2015-04-06 01:44:22 +05:30
$this->grantTypeTokenTypes[$grantType],
2015-04-05 21:33:06 +05:30
$this->grantTypeAccessTokenTTL[$grantType],
$this->scopeDelimiter
);
2015-04-06 01:44:22 +05:30
return $tokenType->generateHttpResponse();
2015-04-05 21:33:06 +05:30
}
/**
* Set the delimiter used to separate scopes in a request
*
* @param string $scopeDelimiter
*/
public function setScopeDelimiter($scopeDelimiter)
{
$this->scopeDelimiter = $scopeDelimiter;
}
}