oauth2-server/src/League/OAuth2/Server/Resource.php

276 lines
6.8 KiB
PHP
Raw Normal View History

2012-12-29 01:42:16 +05:30
<?php
/**
* OAuth 2.0 Resource Server
*
* @package php-loep/oauth2-server
* @author Alex Bilbie <hello@alexbilbie.com>
* @copyright Copyright (c) 2013 PHP League of Extraordinary Packages
* @license http://mit-license.org/
2013-05-09 00:00:53 +05:30
* @link http://github.com/php-loep/oauth2-server
*/
2012-12-29 01:42:16 +05:30
namespace League\OAuth2\Server;
2012-12-29 01:42:16 +05:30
use OutOfBoundsException;
use League\OAuth2\Server\Storage\SessionInterface;
use League\OAuth2\Server\Util\RequestInterface;
use League\OAuth2\Server\Util\Request;
2012-12-29 01:42:16 +05:30
/**
* OAuth 2.0 Resource Server
*/
class Resource
2012-12-29 01:42:16 +05:30
{
/**
* The access token
* @var string
*/
2012-12-29 01:42:16 +05:30
protected $accessToken = null;
/**
* The session ID
* @var string
*/
2012-12-29 01:42:16 +05:30
protected $sessionId = null;
/**
* The type of the owner of the access token
* @var string
*/
2012-12-29 01:42:16 +05:30
protected $ownerType = null;
/**
* The ID of the owner of the access token
* @var string
*/
2012-12-29 01:42:16 +05:30
protected $ownerId = null;
/**
* The scopes associated with the access token
* @var array
*/
2012-12-29 01:42:16 +05:30
protected $sessionScopes = array();
/**
* The client, scope and session storage classes
* @var array
*/
2012-12-29 01:42:16 +05:30
protected $storages = array();
/**
* The request object
* @var Util\RequestInterface
*/
2012-12-29 01:42:16 +05:30
protected $request = null;
/**
* The query string key which is used by clients to present the access token (default: access_token)
* @var string
*/
protected $tokenKey = 'access_token';
2012-12-29 01:42:16 +05:30
/**
* The client ID
* @var string
*/
protected $clientId = null;
2012-12-29 01:42:16 +05:30
/**
* Sets up the Resource
*
* @param SessionInterface The Session Storage Object
2012-12-29 01:42:16 +05:30
*/
2013-02-05 21:50:45 +05:30
public function __construct(SessionInterface $session)
2012-12-29 01:42:16 +05:30
{
$this->storages['session'] = $session;
}
2012-12-29 01:42:16 +05:30
/**
* Sets the Request Object
*
* @param RequestInterface The Request Object
*/
public function setRequest(RequestInterface $request)
{
2012-12-29 01:42:16 +05:30
$this->request = $request;
2013-11-26 05:28:42 +05:30
return $this;
2012-12-29 01:42:16 +05:30
}
/**
* Gets the Request object. It will create one from the globals if one is not set.
*
* @return Util\RequestInterface
*/
public function getRequest()
{
if ($this->request === null) {
2013-02-05 21:50:45 +05:30
// @codeCoverageIgnoreStart
$this->request = Request::buildFromGlobals();
}
2013-02-05 21:50:45 +05:30
// @codeCoverageIgnoreEnd
return $this->request;
}
/**
* Returns the query string key for the access token.
*
* @return string
*/
2013-02-05 21:50:45 +05:30
public function getTokenKey()
{
return $this->tokenKey;
}
/**
* Sets the query string key for the access token.
*
* @param $key The new query string key
*/
2013-02-05 21:50:45 +05:30
public function setTokenKey($key)
{
$this->tokenKey = $key;
2013-11-26 05:28:42 +05:30
return $this;
2013-02-05 21:50:45 +05:30
}
/**
* Gets the access token owner ID.
*
* @return string
*/
public function getOwnerId()
{
return $this->ownerId;
}
/**
* Gets the owner type.
*
* @return string
*/
public function getOwnerType()
{
2013-02-05 21:50:45 +05:30
return $this->ownerType;
}
/**
* Gets the access token.
*
* @return string
*/
public function getAccessToken()
{
return $this->accessToken;
}
2013-05-09 06:36:18 +05:30
/**
* Gets the client ID that created the session
* @return string
*/
public function getClientId()
{
return $this->clientId;
}
2012-12-29 01:42:16 +05:30
/**
* Checks if the access token is valid or not.
2012-12-29 01:42:16 +05:30
*
* @param $headersOnly Limit Access Token to Authorization header only
* @throws Exception\InvalidAccessTokenException Thrown if the presented access token is not valid
2012-12-29 01:42:16 +05:30
* @return bool
*/
public function isValid($headersOnly = false)
2012-12-29 01:42:16 +05:30
{
$accessToken = $this->determineAccessToken($headersOnly);
2012-12-29 01:42:16 +05:30
$result = $this->storages['session']->validateAccessToken($accessToken);
2012-12-29 01:42:16 +05:30
if ( ! $result) {
throw new Exception\InvalidAccessTokenException('Access token is not valid');
2012-12-29 01:42:16 +05:30
}
$this->accessToken = $accessToken;
2013-05-05 22:41:01 +05:30
$this->sessionId = $result['session_id'];
$this->clientId = $result['client_id'];
2012-12-29 01:42:16 +05:30
$this->ownerType = $result['owner_type'];
$this->ownerId = $result['owner_id'];
2013-05-05 22:48:37 +05:30
$sessionScopes = $this->storages['session']->getScopes($this->accessToken);
foreach ($sessionScopes as $scope) {
2013-05-28 00:03:07 +05:30
$this->sessionScopes[] = $scope['scope'];
2013-05-05 22:48:37 +05:30
}
2012-12-29 01:42:16 +05:30
return true;
}
2013-05-05 22:46:28 +05:30
/**
* Get the session scopes
* @return array
2013-05-05 22:46:28 +05:30
*/
public function getScopes()
{
return $this->sessionScopes;
}
2012-12-29 01:42:16 +05:30
/**
* Checks if the presented access token has the given scope(s).
2012-12-29 01:42:16 +05:30
*
* @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
2012-12-29 01:42:16 +05:30
*/
public function hasScope($scopes)
{
if (is_string($scopes)) {
if (in_array($scopes, $this->sessionScopes)) {
return true;
}
return false;
} elseif (is_array($scopes)) {
foreach ($scopes as $scope) {
if ( ! in_array($scope, $this->sessionScopes)) {
return false;
}
}
return true;
}
return false;
}
/**
* Reads in the access token from the headers.
*
* @param $headersOnly Limit Access Token to Authorization header only
* @throws Exception\MissingAccessTokenException Thrown if there is no access token presented
* @return string
*/
public function determineAccessToken($headersOnly = false)
2012-12-29 01:42:16 +05:30
{
if ($header = $this->getRequest()->header('Authorization')) {
// Check for special case, because cURL sometimes does an
// internal second request and doubles the authorization header,
// which always resulted in an error.
//
// 1st request: Authorization: Bearer XXX
// 2nd request: Authorization: Bearer XXX, Bearer XXX
if (strpos($header, ',') !== false) {
$headerPart = explode(',', $header);
$accessToken = trim(preg_replace('/^(?:\s+)?Bearer\s/', '', $headerPart[0]));
} else {
$accessToken = trim(preg_replace('/^(?:\s+)?Bearer\s/', '', $header));
}
$accessToken = ($accessToken === 'Bearer') ? '' : $accessToken;
} elseif ($headersOnly === false) {
$method = $this->getRequest()->server('REQUEST_METHOD');
$accessToken = $this->getRequest()->{$method}($this->tokenKey);
2012-12-29 01:42:16 +05:30
}
if (empty($accessToken)) {
2013-02-08 17:10:33 +05:30
throw new Exception\InvalidAccessTokenException('Access token is missing');
2012-12-29 01:42:16 +05:30
}
return $accessToken;
2012-12-29 01:42:16 +05:30
}
}