oauth2-server/src/Server.php

185 lines
5.8 KiB
PHP
Raw Normal View History

2015-04-05 21:33:06 +05:30
<?php
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;
use League\OAuth2\Server\Grant\GrantTypeInterface;
//use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
//use League\OAuth2\Server\Repositories\AuthCodeRepositoryInterface;
//use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
use League\OAuth2\Server\Repositories\RepositoryInterface;
//use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
//use League\OAuth2\Server\Repositories\UserRepositoryInterface;
2015-04-06 01:44:22 +05:30
use League\OAuth2\Server\TokenTypes\BearerTokenType;
use League\OAuth2\Server\TokenTypes\TokenTypeInterface;
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-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
*/
2015-04-06 12:53:18 +05:30
protected $defaultTokenType;
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
* @var RepositoryInterface[]
*/
2015-10-14 14:21:53 +05:30
// protected $repositories = [];
2015-10-14 14:21:53 +05:30
/**
* New server instance
*/
public function __construct()
{
$this->setDefaultTokenType(new BearerTokenType());
$this->setDefaultAccessTokenTTL(new DateInterval('PT01H')); // default of 1 hour
}
/**
* Set the default token type that grants will return
2015-04-05 21:33:06 +05:30
*
2015-04-06 12:53:18 +05:30
* @param TokenTypeInterface $defaultTokenType
2015-04-05 21:33:06 +05:30
*/
public function setDefaultTokenType(TokenTypeInterface $defaultTokenType)
{
$this->defaultTokenType = $defaultTokenType;
}
2015-04-05 21:33:06 +05:30
/**
2015-10-14 14:21:53 +05:30
* Set the delimiter string used to separate scopes in a request
*
2015-10-14 14:21:53 +05:30
* @param string $scopeDelimiterString
*/
2015-10-14 14:21:53 +05:30
public function setScopeDelimiterString($scopeDelimiterString)
{
2015-10-14 14:21:53 +05:30
$this->scopeDelimiterString = $scopeDelimiterString;
}
2015-04-05 21:33:06 +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
* @param TokenTypeInterface $tokenType
* @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-04-06 01:44:22 +05:30
TokenTypeInterface $tokenType = 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-04-06 01:44:22 +05:30
if ($tokenType instanceof TokenTypeInterface) {
2015-10-14 14:21:53 +05:30
$this->grantTypeTokenTypes[$grantType->getIdentifier()] = $tokenType;
2015-04-05 21:33:06 +05:30
} else {
2015-10-14 14:21:53 +05:30
$this->grantTypeTokenTypes[$grantType->getIdentifier()] = $this->defaultTokenType;
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-10-14 14:21:53 +05:30
* @return \League\OAuth2\Server\TokenTypes\TokenTypeInterface
* @throws \League\OAuth2\Server\Exception\InvalidGrantException
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-10-14 14:21:53 +05:30
$response = null;
foreach ($this->enabledGrantTypes as $grantType) {
if ($grantType->canRespondToRequest($request)) {
$response = $grantType->respondToRequest(
$request,
$this->grantTypeTokenTypes[$grantType->getIdentifier()],
$this->grantTypeAccessTokenTTL[$grantType->getIdentifier()],
$this->scopeDelimiterString
);
}
2015-04-05 21:33:06 +05:30
}
2015-10-14 14:21:53 +05:30
if ($response === null) {
// do something here
}
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
}
2015-10-14 14:21:53 +05:30
/**
* @param \League\OAuth2\Server\Repositories\RepositoryInterface $repository
*/
/*public function addRepository(RepositoryInterface $repository)
{
switch ($repository) {
case ($repository instanceof AccessTokenRepositoryInterface):
$this->repositories[AccessTokenRepositoryInterface::class] = $repository;
break;
case ($repository instanceof ClientRepositoryInterface):
$this->repositories[ClientRepositoryInterface::class] = $repository;
break;
case ($repository instanceof ScopeRepositoryInterface):
$this->repositories[ScopeRepositoryInterface::class] = $repository;
break;
case ($repository instanceof UserRepositoryInterface):
$this->repositories[UserRepositoryInterface::class] = $repository;
break;
case ($repository instanceof AuthCodeRepositoryInterface):
$this->repositories[AuthCodeRepositoryInterface::class] = $repository;
break;
}
}*/
2015-04-05 21:33:06 +05:30
}