mirror of
https://github.com/elyby/oauth2-server.git
synced 2024-12-16 10:18:55 +05:30
First commit of updated ResourceServer
This commit is contained in:
parent
b86d1f1406
commit
449ba5005c
@ -13,8 +13,9 @@ namespace League\OAuth2\Server;
|
|||||||
|
|
||||||
use OutOfBoundsException;
|
use OutOfBoundsException;
|
||||||
use League\OAuth2\Server\Storage\SessionInterface;
|
use League\OAuth2\Server\Storage\SessionInterface;
|
||||||
use League\OAuth2\Server\Util\RequestInterface;
|
use League\OAuth2\Server\Storage\AccessTokenInterface;
|
||||||
use League\OAuth2\Server\Util\Request;
|
use League\OAuth2\Server\Storage\ClientInterface;
|
||||||
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* OAuth 2.0 Resource Server
|
* OAuth 2.0 Resource Server
|
||||||
@ -23,92 +24,100 @@ class Resource
|
|||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* The access token
|
* The access token
|
||||||
* @var string
|
*
|
||||||
|
* @var League\OAuth2\Server\AccessToken
|
||||||
*/
|
*/
|
||||||
protected $accessToken = null;
|
protected $accessToken = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The session ID
|
* The session
|
||||||
* @var string
|
*
|
||||||
|
* @var \League\OAuth2\Server\Session
|
||||||
*/
|
*/
|
||||||
protected $sessionId = null;
|
protected $session = null;
|
||||||
|
|
||||||
/**
|
|
||||||
* The type of the owner of the access token
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
protected $ownerType = null;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The ID of the owner of the access token
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
protected $ownerId = null;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The scopes associated with the access token
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
protected $sessionScopes = array();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The client, scope and session storage classes
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
protected $storages = array();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The request object
|
* The request object
|
||||||
|
*
|
||||||
* @var Util\RequestInterface
|
* @var Util\RequestInterface
|
||||||
*/
|
*/
|
||||||
protected $request = null;
|
protected $request = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The query string key which is used by clients to present the access token (default: access_token)
|
* The query string key which is used by clients to present the access token (default: access_token)
|
||||||
|
*
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
protected $tokenKey = 'access_token';
|
protected $tokenKey = 'access_token';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The client ID
|
* The client ID
|
||||||
* @var string
|
*
|
||||||
|
* @var League\OAuth2\Server\Client
|
||||||
*/
|
*/
|
||||||
protected $clientId = null;
|
protected $client = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets up the Resource
|
* Session storage
|
||||||
*
|
*
|
||||||
* @param SessionInterface The Session Storage Object
|
* @var League\OAuth2\Server\Storage\SessionInterface
|
||||||
*/
|
*/
|
||||||
public function __construct(SessionInterface $session)
|
protected $sessionStorage = null;
|
||||||
{
|
|
||||||
$this->storages['session'] = $session;
|
/**
|
||||||
|
* Access token storage
|
||||||
|
*
|
||||||
|
* @var League\OAuth2\Server\Storage\AccessTokenInterface
|
||||||
|
*/
|
||||||
|
protected $accessTokenStorage = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Client storage
|
||||||
|
*
|
||||||
|
* @var League\OAuth2\Server\Storage\ClientInterface
|
||||||
|
*/
|
||||||
|
protected $clientStorage = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialise the resource server
|
||||||
|
*
|
||||||
|
* @param SessionInterface $sessionStorage [description]
|
||||||
|
* @param AccessTokenInteface $accessTokenStorage [description]
|
||||||
|
* @param ClientInterface $clientStorage [description]
|
||||||
|
*
|
||||||
|
* @return self
|
||||||
|
*/
|
||||||
|
public function __construct(
|
||||||
|
SessionInterface $sessionStorage,
|
||||||
|
AccessTokenInteface $accessTokenStorage,
|
||||||
|
ClientInterface $clientStorage
|
||||||
|
) {
|
||||||
|
$this->sessionStorage = $sessionStorage;
|
||||||
|
$this->accessTokenStorage = $accessTokenStorage;
|
||||||
|
$this->clientStorage = $clientStorage;
|
||||||
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the Request Object
|
* Sets the Request Object
|
||||||
*
|
*
|
||||||
* @param RequestInterface The Request Object
|
* @param \Symfony\Component\HttpFoundation\Request The Request Object
|
||||||
|
*
|
||||||
|
* @return self
|
||||||
*/
|
*/
|
||||||
public function setRequest(RequestInterface $request)
|
public function setRequest(Request $request)
|
||||||
{
|
{
|
||||||
$this->request = $request;
|
$this->request = $request;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the Request object. It will create one from the globals if one is not set.
|
* Gets the Request object. It will create one from the globals if one is not set.
|
||||||
*
|
*
|
||||||
* @return Util\RequestInterface
|
* @return Symfony\Component\HttpFoundation\Request
|
||||||
*/
|
*/
|
||||||
public function getRequest()
|
public function getRequest()
|
||||||
{
|
{
|
||||||
if ($this->request === null) {
|
|
||||||
// @codeCoverageIgnoreStart
|
|
||||||
$this->request = Request::buildFromGlobals();
|
|
||||||
}
|
|
||||||
// @codeCoverageIgnoreEnd
|
|
||||||
|
|
||||||
return $this->request;
|
return $this->request;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -126,6 +135,8 @@ class Resource
|
|||||||
* Sets the query string key for the access token.
|
* Sets the query string key for the access token.
|
||||||
*
|
*
|
||||||
* @param $key The new query string key
|
* @param $key The new query string key
|
||||||
|
*
|
||||||
|
* @return self
|
||||||
*/
|
*/
|
||||||
public function setTokenKey($key)
|
public function setTokenKey($key)
|
||||||
{
|
{
|
||||||
@ -134,119 +145,147 @@ class Resource
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the access token owner ID.
|
* Gets the access token owner ID
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getOwnerId()
|
public function getOwnerId()
|
||||||
{
|
{
|
||||||
return $this->ownerId;
|
return $this->session->getOwnerId();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the owner type.
|
* Gets the owner type
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getOwnerType()
|
public function getOwnerType()
|
||||||
{
|
{
|
||||||
return $this->ownerType;
|
return $this->session->getOwnerType();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the access token.
|
* Gets the access token
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getAccessToken()
|
public function getAccessToken()
|
||||||
{
|
{
|
||||||
return $this->accessToken;
|
return $this->accessToken->getId();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the client ID that created the session
|
* Gets the client ID that created the session
|
||||||
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getClientId()
|
public function getClientId()
|
||||||
{
|
{
|
||||||
return $this->clientId;
|
return $this->client->getId();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks if the access token is valid or not.
|
* Checks if the access token is valid or not
|
||||||
*
|
*
|
||||||
* @param $headersOnly Limit Access Token to Authorization header only
|
* @param $headersOnly Limit Access Token to Authorization header only
|
||||||
* @throws Exception\InvalidAccessTokenException Thrown if the presented access token is not valid
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function isValid($headersOnly = false)
|
public function isValid($headersOnly = false)
|
||||||
{
|
{
|
||||||
$accessToken = $this->determineAccessToken($headersOnly);
|
try {
|
||||||
|
$accessToken = $this->determineAccessToken($headersOnly);
|
||||||
|
} catch (Exception $e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
$result = $this->storages['session']->validateAccessToken($accessToken);
|
// Set the access token
|
||||||
|
$tokenResult = $this->accessTokenStorage->getToken($accessToken);
|
||||||
|
if ($tokenResult === null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
if ( ! $result) {
|
$accessToken = new AccessToken;
|
||||||
throw new Exception\InvalidAccessTokenException('Access token is not valid');
|
$accessToken->setId($token);
|
||||||
|
$accessToken->setTTL($tokenResult['ttl']);
|
||||||
|
$accessToken->setTimestamp($tokenResult['created']);
|
||||||
|
|
||||||
|
$scopes = $this->accessTokenStorage->getTokenScopes($token);
|
||||||
|
foreach ($scopes as $scope => $details) {
|
||||||
|
$accessToken->associateScope($scope, $details);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->accessToken = $accessToken;
|
$this->accessToken = $accessToken;
|
||||||
$this->sessionId = $result['session_id'];
|
|
||||||
$this->clientId = $result['client_id'];
|
|
||||||
$this->ownerType = $result['owner_type'];
|
|
||||||
$this->ownerId = $result['owner_id'];
|
|
||||||
|
|
||||||
$sessionScopes = $this->storages['session']->getScopes($this->accessToken);
|
|
||||||
foreach ($sessionScopes as $scope) {
|
// Set the session
|
||||||
$this->sessionScopes[] = $scope['scope'];
|
$sessionResult = $this->sessionStorage->getSession($tokenResult['session_id']);
|
||||||
|
if ($sessionResult === null) {
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$session = new Session();
|
||||||
|
$session->setOwner($sessionResult['owner_type'], $sessionResult['owner_id']);
|
||||||
|
|
||||||
|
$this->session = $session;
|
||||||
|
|
||||||
|
// Set the client
|
||||||
|
$clientResult = $this->clientStorage->getClient($sessionResult['client_id']);
|
||||||
|
if ($clientResult === null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$client = new Client();
|
||||||
|
$client->setCredentials($clientResult['client_id'], $clientResult['client_secret']);
|
||||||
|
|
||||||
|
$this->client = $client;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the session scopes
|
* Get the session scopes
|
||||||
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function getScopes()
|
public function getScopes()
|
||||||
{
|
{
|
||||||
return $this->sessionScopes;
|
return $this->accessToken->getScopes();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks if the presented access token has the given scope(s).
|
* Checks if the presented access token has the given scope(s)
|
||||||
*
|
*
|
||||||
* @param array|string An array of scopes or a single scope as a string
|
* @param array|string An array of scopes or a single scope as a string
|
||||||
|
*
|
||||||
* @return bool Returns bool if all scopes are found, false if any fail
|
* @return bool Returns bool if all scopes are found, false if any fail
|
||||||
*/
|
*/
|
||||||
public function hasScope($scopes)
|
public function hasScope($scopes)
|
||||||
{
|
{
|
||||||
if (is_string($scopes)) {
|
if (is_string($scopes)) {
|
||||||
if (in_array($scopes, $this->sessionScopes)) {
|
return $this->accessToken->hasScope($scopes);
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
} elseif (is_array($scopes)) {
|
} elseif (is_array($scopes)) {
|
||||||
foreach ($scopes as $scope) {
|
foreach ($scopes as $scope) {
|
||||||
if ( ! in_array($scope, $this->sessionScopes)) {
|
if (!$this->accessToken->hasScope($scope)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads in the access token from the headers.
|
* Reads in the access token from the headers
|
||||||
*
|
*
|
||||||
* @param $headersOnly Limit Access Token to Authorization header only
|
* @param $headersOnly Limit Access Token to Authorization header only
|
||||||
|
*
|
||||||
* @throws Exception\MissingAccessTokenException Thrown if there is no access token presented
|
* @throws Exception\MissingAccessTokenException Thrown if there is no access token presented
|
||||||
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function determineAccessToken($headersOnly = false)
|
public function determineAccessToken($headersOnly = false)
|
||||||
{
|
{
|
||||||
if ($header = $this->getRequest()->header('Authorization')) {
|
if ($header = $this->getRequest()->headers->get('Authorization')) {
|
||||||
// Check for special case, because cURL sometimes does an
|
// Check for special case, because cURL sometimes does an
|
||||||
// internal second request and doubles the authorization header,
|
// internal second request and doubles the authorization header,
|
||||||
// which always resulted in an error.
|
// which always resulted in an error.
|
||||||
@ -261,8 +300,8 @@ class Resource
|
|||||||
}
|
}
|
||||||
$accessToken = ($accessToken === 'Bearer') ? '' : $accessToken;
|
$accessToken = ($accessToken === 'Bearer') ? '' : $accessToken;
|
||||||
} elseif ($headersOnly === false) {
|
} elseif ($headersOnly === false) {
|
||||||
$method = $this->getRequest()->server('REQUEST_METHOD');
|
$method = $this->getRequest()->server->get('REQUEST_METHOD');
|
||||||
$accessToken = $this->getRequest()->{$method}($this->tokenKey);
|
$accessToken = $this->getRequest()->request->get($this->tokenKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (empty($accessToken)) {
|
if (empty($accessToken)) {
|
||||||
@ -271,5 +310,4 @@ class Resource
|
|||||||
|
|
||||||
return $accessToken;
|
return $accessToken;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user