2012-12-29 01:42:16 +05:30
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace OAuth2;
|
|
|
|
|
|
|
|
use OutOfBoundsException;
|
2013-01-05 03:51:24 +05:30
|
|
|
use OAuth2\Storage\SessionInterface;
|
|
|
|
use OAuth2\Storage\SessionScopeInterface;
|
2013-02-05 21:50:45 +05:30
|
|
|
use OAuth2\Util\RequestInterface;
|
2013-02-07 20:24:56 +05:30
|
|
|
use OAuth2\Util\Request;
|
2012-12-29 01:42:16 +05:30
|
|
|
|
2013-01-22 22:03:09 +05:30
|
|
|
class ResourceServer
|
2012-12-29 01:42:16 +05:30
|
|
|
{
|
|
|
|
protected $accessToken = null;
|
|
|
|
|
|
|
|
protected $sessionId = null;
|
|
|
|
|
|
|
|
protected $ownerType = null;
|
|
|
|
|
|
|
|
protected $ownerId = null;
|
|
|
|
|
|
|
|
protected $sessionScopes = array();
|
|
|
|
|
|
|
|
protected $storages = array();
|
|
|
|
|
|
|
|
protected $request = null;
|
|
|
|
|
|
|
|
protected $tokenKey = 'oauth_token';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets up the Resource
|
|
|
|
*
|
|
|
|
* @param SessionInterface The Session Storage Object
|
|
|
|
* @param SessionScopeInterface The Session Scope Storage Object
|
|
|
|
*/
|
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;
|
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
|
|
|
|
*
|
|
|
|
* @param RequestInterface The Request Object
|
|
|
|
*/
|
|
|
|
public function setRequest(RequestInterface $request)
|
|
|
|
{
|
2012-12-29 01:42:16 +05:30
|
|
|
$this->request = $request;
|
|
|
|
}
|
|
|
|
|
2013-01-05 03:51:24 +05:30
|
|
|
/**
|
|
|
|
* Gets the Request object. It will create one from the globals if one is not set.
|
|
|
|
*
|
|
|
|
* @return RequestInterface
|
|
|
|
*/
|
|
|
|
public function getRequest()
|
|
|
|
{
|
|
|
|
if ($this->request === null) {
|
2013-02-05 21:50:45 +05:30
|
|
|
// @codeCoverageIgnoreStart
|
2013-01-05 03:51:24 +05:30
|
|
|
$this->request = Request::buildFromGlobals();
|
|
|
|
}
|
2013-02-05 21:50:45 +05:30
|
|
|
// @codeCoverageIgnoreEnd
|
2013-01-05 03:51:24 +05:30
|
|
|
|
|
|
|
return $this->request;
|
|
|
|
}
|
|
|
|
|
2013-02-05 21:50:45 +05:30
|
|
|
public function getTokenKey()
|
|
|
|
{
|
|
|
|
return $this->tokenKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setTokenKey($key)
|
|
|
|
{
|
|
|
|
$this->tokenKey = $key;
|
|
|
|
}
|
|
|
|
|
2013-01-18 01:49:01 +05:30
|
|
|
/**
|
|
|
|
* Gets the Owner ID.
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
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;
|
2013-01-18 01:49:01 +05:30
|
|
|
}
|
|
|
|
|
2013-01-22 21:55:51 +05:30
|
|
|
/**
|
|
|
|
* Gets the Access Token.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getAccessToken()
|
|
|
|
{
|
|
|
|
return $this->accessToken;
|
|
|
|
}
|
|
|
|
|
2012-12-29 01:42:16 +05:30
|
|
|
/**
|
|
|
|
* Checks if the Access Token is valid or not.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isValid()
|
|
|
|
{
|
|
|
|
$access_token = $this->determineAccessToken();
|
|
|
|
|
|
|
|
$result = $this->storages['session']->validateAccessToken($access_token);
|
|
|
|
|
|
|
|
if ( ! $result) {
|
2013-02-08 17:15:51 +05:30
|
|
|
throw new Exception\InvalidAccessTokenException('Access token is not valid');
|
2012-12-29 01:42:16 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
$this->accessToken = $access_token;
|
|
|
|
$this->sessionId = $result['id'];
|
|
|
|
$this->ownerType = $result['owner_type'];
|
|
|
|
$this->ownerId = $result['owner_id'];
|
|
|
|
|
2013-02-05 21:50:45 +05:30
|
|
|
$this->sessionScopes = $this->storages['session']->getScopes($this->sessionId);
|
2012-12-29 01:42:16 +05:30
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the current session has the given scope(s).
|
|
|
|
*
|
|
|
|
* @param array
|
|
|
|
*/
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-01-05 03:51:24 +05:30
|
|
|
/**
|
|
|
|
* Reads in the Access Token from the headers.
|
|
|
|
*
|
|
|
|
* @return string
|
2013-01-18 01:49:01 +05:30
|
|
|
* @throws Exception\MissingAccessTokenException
|
2013-01-05 03:51:24 +05:30
|
|
|
*/
|
2012-12-29 01:42:16 +05:30
|
|
|
protected function determineAccessToken()
|
|
|
|
{
|
2013-01-05 03:51:24 +05:30
|
|
|
if ($header = $this->getRequest()->header('Authorization')) {
|
2013-02-07 21:46:09 +05:30
|
|
|
$access_token = base64_decode(trim(str_replace('Bearer', '', $header)));
|
2012-12-29 01:42:16 +05:30
|
|
|
} else {
|
2013-01-05 03:51:24 +05:30
|
|
|
$method = $this->getRequest()->server('REQUEST_METHOD');
|
|
|
|
$access_token = $this->getRequest()->{$method}($this->tokenKey);
|
2012-12-29 01:42:16 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($access_token)) {
|
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 $access_token;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|