2012-12-29 01:42:16 +05:30
|
|
|
<?php
|
2013-02-13 02:03:23 +05:30
|
|
|
/**
|
|
|
|
* OAuth 2.0 Resource Server
|
|
|
|
*
|
2014-01-08 21:45:29 +05:30
|
|
|
* @package league/oauth2-server
|
2013-02-13 02:03:23 +05:30
|
|
|
* @author Alex Bilbie <hello@alexbilbie.com>
|
2014-01-08 21:45:29 +05:30
|
|
|
* @copyright Copyright (c) PHP League of Extraordinary Packages
|
2013-02-13 02:03:23 +05:30
|
|
|
* @license http://mit-license.org/
|
2013-05-09 00:00:53 +05:30
|
|
|
* @link http://github.com/php-loep/oauth2-server
|
2013-02-13 02:03:23 +05:30
|
|
|
*/
|
2012-12-29 01:42:16 +05:30
|
|
|
|
2013-05-09 00:12:23 +05:30
|
|
|
namespace League\OAuth2\Server;
|
2012-12-29 01:42:16 +05:30
|
|
|
|
2013-05-09 00:12:23 +05:30
|
|
|
use League\OAuth2\Server\Storage\SessionInterface;
|
2013-12-17 05:17:03 +05:30
|
|
|
use League\OAuth2\Server\Storage\AccessTokenInterface;
|
|
|
|
use League\OAuth2\Server\Storage\ClientInterface;
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
2012-12-29 01:42:16 +05:30
|
|
|
|
2013-02-13 02:03:23 +05:30
|
|
|
/**
|
|
|
|
* OAuth 2.0 Resource Server
|
|
|
|
*/
|
2013-05-09 00:12:23 +05:30
|
|
|
class Resource
|
2012-12-29 01:42:16 +05:30
|
|
|
{
|
2013-02-13 02:03:23 +05:30
|
|
|
/**
|
|
|
|
* The access token
|
2013-12-17 05:17:03 +05:30
|
|
|
*
|
|
|
|
* @var League\OAuth2\Server\AccessToken
|
2013-02-13 02:03:23 +05:30
|
|
|
*/
|
2012-12-29 01:42:16 +05:30
|
|
|
protected $accessToken = null;
|
|
|
|
|
2013-02-13 02:03:23 +05:30
|
|
|
/**
|
2013-12-17 05:17:03 +05:30
|
|
|
* The session
|
|
|
|
*
|
|
|
|
* @var \League\OAuth2\Server\Session
|
2013-02-13 02:03:23 +05:30
|
|
|
*/
|
2013-12-17 05:17:03 +05:30
|
|
|
protected $session = null;
|
2012-12-29 01:42:16 +05:30
|
|
|
|
2013-02-13 02:03:23 +05:30
|
|
|
/**
|
2013-12-17 05:17:03 +05:30
|
|
|
* The request object
|
|
|
|
*
|
|
|
|
* @var Util\RequestInterface
|
2013-02-13 02:03:23 +05:30
|
|
|
*/
|
2013-12-17 05:17:03 +05:30
|
|
|
protected $request = null;
|
2012-12-29 01:42:16 +05:30
|
|
|
|
2013-02-13 02:03:23 +05:30
|
|
|
/**
|
2013-12-17 05:17:03 +05:30
|
|
|
* The query string key which is used by clients to present the access token (default: access_token)
|
|
|
|
*
|
2013-02-13 02:03:23 +05:30
|
|
|
* @var string
|
|
|
|
*/
|
2013-12-17 05:17:03 +05:30
|
|
|
protected $tokenKey = 'access_token';
|
2012-12-29 01:42:16 +05:30
|
|
|
|
2013-02-13 02:03:23 +05:30
|
|
|
/**
|
2013-12-17 05:17:03 +05:30
|
|
|
* The client ID
|
|
|
|
*
|
|
|
|
* @var League\OAuth2\Server\Client
|
2013-02-13 02:03:23 +05:30
|
|
|
*/
|
2013-12-17 05:17:03 +05:30
|
|
|
protected $client = null;
|
2012-12-29 01:42:16 +05:30
|
|
|
|
2013-02-13 02:03:23 +05:30
|
|
|
/**
|
2013-12-17 05:17:03 +05:30
|
|
|
* Session storage
|
|
|
|
*
|
|
|
|
* @var League\OAuth2\Server\Storage\SessionInterface
|
2013-02-13 02:03:23 +05:30
|
|
|
*/
|
2013-12-17 05:17:03 +05:30
|
|
|
protected $sessionStorage = null;
|
2012-12-29 01:42:16 +05:30
|
|
|
|
2013-02-13 02:03:23 +05:30
|
|
|
/**
|
2013-12-17 05:17:03 +05:30
|
|
|
* Access token storage
|
|
|
|
*
|
|
|
|
* @var League\OAuth2\Server\Storage\AccessTokenInterface
|
2013-02-13 02:03:23 +05:30
|
|
|
*/
|
2013-12-17 05:17:03 +05:30
|
|
|
protected $accessTokenStorage = null;
|
2012-12-29 01:42:16 +05:30
|
|
|
|
2013-05-09 02:29:17 +05:30
|
|
|
/**
|
2013-12-17 05:17:03 +05:30
|
|
|
* Client storage
|
|
|
|
*
|
|
|
|
* @var League\OAuth2\Server\Storage\ClientInterface
|
2013-05-09 02:29:17 +05:30
|
|
|
*/
|
2013-12-17 05:17:03 +05:30
|
|
|
protected $clientStorage = null;
|
2013-05-09 02:29:17 +05:30
|
|
|
|
2012-12-29 01:42:16 +05:30
|
|
|
/**
|
2013-12-17 05:17:03 +05:30
|
|
|
* Initialise the resource server
|
|
|
|
*
|
|
|
|
* @param SessionInterface $sessionStorage [description]
|
|
|
|
* @param AccessTokenInteface $accessTokenStorage [description]
|
|
|
|
* @param ClientInterface $clientStorage [description]
|
2012-12-29 01:42:16 +05:30
|
|
|
*
|
2013-12-17 05:17:03 +05:30
|
|
|
* @return self
|
2012-12-29 01:42:16 +05:30
|
|
|
*/
|
2013-12-17 05:17:03 +05:30
|
|
|
public function __construct(
|
|
|
|
SessionInterface $sessionStorage,
|
|
|
|
AccessTokenInteface $accessTokenStorage,
|
|
|
|
ClientInterface $clientStorage
|
|
|
|
) {
|
|
|
|
$this->sessionStorage = $sessionStorage;
|
|
|
|
$this->accessTokenStorage = $accessTokenStorage;
|
|
|
|
$this->clientStorage = $clientStorage;
|
|
|
|
return $this;
|
2013-01-05 03:51:24 +05:30
|
|
|
}
|
2012-12-29 01:42:16 +05:30
|
|
|
|
2013-01-05 03:51:24 +05:30
|
|
|
/**
|
|
|
|
* Sets the Request Object
|
|
|
|
*
|
2013-12-17 05:17:03 +05:30
|
|
|
* @param \Symfony\Component\HttpFoundation\Request The Request Object
|
|
|
|
*
|
|
|
|
* @return self
|
2013-01-05 03:51:24 +05:30
|
|
|
*/
|
2013-12-17 05:17:03 +05:30
|
|
|
public function setRequest(Request $request)
|
2013-01-05 03:51:24 +05:30
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2013-01-05 03:51:24 +05:30
|
|
|
/**
|
2013-12-17 05:17:03 +05:30
|
|
|
* Gets the Request object. It will create one from the globals if one is not set.
|
2013-01-05 03:51:24 +05:30
|
|
|
*
|
2013-12-24 22:32:49 +05:30
|
|
|
* @return \Symfony\Component\HttpFoundation\Request
|
2013-01-05 03:51:24 +05:30
|
|
|
*/
|
|
|
|
public function getRequest()
|
|
|
|
{
|
2013-12-24 22:32:49 +05:30
|
|
|
if ($this->request = null) {
|
|
|
|
return Symfony\Component\HttpFoundation\Request::createFromGlobals();
|
|
|
|
}
|
|
|
|
|
2013-01-05 03:51:24 +05:30
|
|
|
return $this->request;
|
|
|
|
}
|
|
|
|
|
2013-02-13 02:03:23 +05:30
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2013-02-13 02:03:23 +05:30
|
|
|
/**
|
|
|
|
* Sets the query string key for the access token.
|
|
|
|
*
|
|
|
|
* @param $key The new query string key
|
2013-12-17 05:17:03 +05:30
|
|
|
*
|
|
|
|
* @return self
|
2013-02-13 02:03:23 +05:30
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
|
2013-01-18 01:49:01 +05:30
|
|
|
/**
|
2013-12-17 05:17:03 +05:30
|
|
|
* Gets the access token owner ID
|
2013-01-18 01:49:01 +05:30
|
|
|
*
|
2013-02-13 02:03:23 +05:30
|
|
|
* @return string
|
2013-01-18 01:49:01 +05:30
|
|
|
*/
|
|
|
|
public function getOwnerId()
|
|
|
|
{
|
2013-12-17 05:17:03 +05:30
|
|
|
return $this->session->getOwnerId();
|
2013-01-18 01:49:01 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-12-17 05:17:03 +05:30
|
|
|
* Gets the owner type
|
2013-01-18 01:49:01 +05:30
|
|
|
*
|
2013-02-13 02:03:23 +05:30
|
|
|
* @return string
|
2013-01-18 01:49:01 +05:30
|
|
|
*/
|
|
|
|
public function getOwnerType()
|
|
|
|
{
|
2013-12-17 05:17:03 +05:30
|
|
|
return $this->session->getOwnerType();
|
2013-01-18 01:49:01 +05:30
|
|
|
}
|
|
|
|
|
2013-01-22 21:55:51 +05:30
|
|
|
/**
|
2013-12-17 05:17:03 +05:30
|
|
|
* Gets the access token
|
2013-01-22 21:55:51 +05:30
|
|
|
*
|
2013-02-13 02:03:23 +05:30
|
|
|
* @return string
|
2013-01-22 21:55:51 +05:30
|
|
|
*/
|
|
|
|
public function getAccessToken()
|
|
|
|
{
|
2013-12-17 05:17:03 +05:30
|
|
|
return $this->accessToken->getId();
|
2013-01-22 21:55:51 +05:30
|
|
|
}
|
|
|
|
|
2013-05-09 06:36:18 +05:30
|
|
|
/**
|
|
|
|
* Gets the client ID that created the session
|
2013-12-17 05:17:03 +05:30
|
|
|
*
|
2013-05-09 06:36:18 +05:30
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getClientId()
|
|
|
|
{
|
2013-12-17 05:17:03 +05:30
|
|
|
return $this->client->getId();
|
2013-05-09 06:36:18 +05:30
|
|
|
}
|
|
|
|
|
2012-12-29 01:42:16 +05:30
|
|
|
/**
|
2013-12-17 05:17:03 +05:30
|
|
|
* Checks if the access token is valid or not
|
2012-12-29 01:42:16 +05:30
|
|
|
*
|
2013-05-28 09:57:30 +05:30
|
|
|
* @param $headersOnly Limit Access Token to Authorization header only
|
2013-12-17 05:17:03 +05:30
|
|
|
*
|
2012-12-29 01:42:16 +05:30
|
|
|
* @return bool
|
|
|
|
*/
|
2013-05-28 09:57:30 +05:30
|
|
|
public function isValid($headersOnly = false)
|
2012-12-29 01:42:16 +05:30
|
|
|
{
|
2013-12-17 05:17:03 +05:30
|
|
|
try {
|
|
|
|
$accessToken = $this->determineAccessToken($headersOnly);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the access token
|
|
|
|
$tokenResult = $this->accessTokenStorage->getToken($accessToken);
|
|
|
|
if ($tokenResult === null) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-12-29 01:42:16 +05:30
|
|
|
|
2013-12-17 05:17:03 +05:30
|
|
|
$accessToken = new AccessToken;
|
|
|
|
$accessToken->setId($token);
|
|
|
|
$accessToken->setTTL($tokenResult['ttl']);
|
|
|
|
$accessToken->setTimestamp($tokenResult['created']);
|
2012-12-29 01:42:16 +05:30
|
|
|
|
2013-12-17 05:17:03 +05:30
|
|
|
$scopes = $this->accessTokenStorage->getTokenScopes($token);
|
|
|
|
foreach ($scopes as $scope => $details) {
|
|
|
|
$accessToken->associateScope($scope, $details);
|
2012-12-29 01:42:16 +05:30
|
|
|
}
|
|
|
|
|
2013-05-05 22:35:46 +05:30
|
|
|
$this->accessToken = $accessToken;
|
2013-12-17 05:17:03 +05:30
|
|
|
|
|
|
|
// Set the session
|
|
|
|
$sessionResult = $this->sessionStorage->getSession($tokenResult['session_id']);
|
|
|
|
if ($sessionResult === null) {
|
|
|
|
return false;
|
2013-05-05 22:48:37 +05:30
|
|
|
}
|
2012-12-29 01:42:16 +05:30
|
|
|
|
2013-12-17 05:17:03 +05:30
|
|
|
$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;
|
|
|
|
|
2012-12-29 01:42:16 +05:30
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-05-05 22:46:28 +05:30
|
|
|
/**
|
|
|
|
* Get the session scopes
|
2013-12-17 05:17:03 +05:30
|
|
|
*
|
2013-05-09 02:29:17 +05:30
|
|
|
* @return array
|
2013-05-05 22:46:28 +05:30
|
|
|
*/
|
|
|
|
public function getScopes()
|
|
|
|
{
|
2013-12-17 05:17:03 +05:30
|
|
|
return $this->accessToken->getScopes();
|
2013-05-05 22:46:28 +05:30
|
|
|
}
|
|
|
|
|
2012-12-29 01:42:16 +05:30
|
|
|
/**
|
2013-12-17 05:17:03 +05:30
|
|
|
* Checks if the presented access token has the given scope(s)
|
2014-01-08 21:45:29 +05:30
|
|
|
* @param array|string $scopes 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)) {
|
2013-12-17 05:17:03 +05:30
|
|
|
return $this->accessToken->hasScope($scopes);
|
2012-12-29 01:42:16 +05:30
|
|
|
} elseif (is_array($scopes)) {
|
|
|
|
foreach ($scopes as $scope) {
|
2013-12-17 05:17:03 +05:30
|
|
|
if (!$this->accessToken->hasScope($scope)) {
|
2012-12-29 01:42:16 +05:30
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-05 03:51:24 +05:30
|
|
|
/**
|
2013-12-17 05:17:03 +05:30
|
|
|
* Reads in the access token from the headers
|
2013-01-05 03:51:24 +05:30
|
|
|
*
|
2013-05-28 09:57:30 +05:30
|
|
|
* @param $headersOnly Limit Access Token to Authorization header only
|
2013-12-17 05:17:03 +05:30
|
|
|
*
|
2013-02-13 02:03:23 +05:30
|
|
|
* @throws Exception\MissingAccessTokenException Thrown if there is no access token presented
|
2013-12-17 05:17:03 +05:30
|
|
|
*
|
2013-01-05 03:51:24 +05:30
|
|
|
* @return string
|
|
|
|
*/
|
2013-06-06 09:29:29 +05:30
|
|
|
public function determineAccessToken($headersOnly = false)
|
2012-12-29 01:42:16 +05:30
|
|
|
{
|
2013-12-17 05:17:03 +05:30
|
|
|
if ($header = $this->getRequest()->headers->get('Authorization')) {
|
2013-05-10 23:30:01 +05:30
|
|
|
// 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) {
|
2013-05-11 01:27:06 +05:30
|
|
|
$headerPart = explode(',', $header);
|
2013-05-11 11:30:47 +05:30
|
|
|
$accessToken = trim(preg_replace('/^(?:\s+)?Bearer\s/', '', $headerPart[0]));
|
2013-05-10 23:30:01 +05:30
|
|
|
} else {
|
2013-05-11 11:30:47 +05:30
|
|
|
$accessToken = trim(preg_replace('/^(?:\s+)?Bearer\s/', '', $header));
|
2013-05-10 23:30:01 +05:30
|
|
|
}
|
2013-05-11 01:27:06 +05:30
|
|
|
$accessToken = ($accessToken === 'Bearer') ? '' : $accessToken;
|
2013-05-28 09:57:30 +05:30
|
|
|
} elseif ($headersOnly === false) {
|
2013-12-17 05:17:03 +05:30
|
|
|
$accessToken = $this->getRequest()->request->get($this->tokenKey);
|
2012-12-29 01:42:16 +05:30
|
|
|
}
|
|
|
|
|
2013-05-05 22:35:46 +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
|
|
|
}
|
|
|
|
|
2013-05-05 22:35:46 +05:30
|
|
|
return $accessToken;
|
2012-12-29 01:42:16 +05:30
|
|
|
}
|
|
|
|
}
|