oauth2-server/src/ResourceServer.php

154 lines
4.6 KiB
PHP
Raw Normal View History

2012-12-28 15:12:16 -05:00
<?php
/**
* OAuth 2.0 Resource Server
*
2014-01-08 16:15:29 +00:00
* @package league/oauth2-server
* @author Alex Bilbie <hello@alexbilbie.com>
2014-03-09 19:34:23 +00:00
* @copyright Copyright (c) Alex Bilbie
* @license http://mit-license.org/
2014-03-09 20:05:38 +00:00
* @link https://github.com/thephpleague/oauth2-server
*/
2012-12-28 15:12:16 -05:00
namespace League\OAuth2\Server;
2012-12-28 15:12:16 -05:00
2014-11-08 18:26:12 +00:00
use League\OAuth2\Server\Entity\AccessTokenEntity;
use League\OAuth2\Server\Exception\AccessDeniedException;
use League\OAuth2\Server\Exception\InvalidRequestException;
2014-01-10 17:30:12 +00:00
use League\OAuth2\Server\Storage\AccessTokenInterface;
2014-11-08 18:26:12 +00:00
use League\OAuth2\Server\Storage\ClientInterface;
2014-01-10 17:30:12 +00:00
use League\OAuth2\Server\Storage\ScopeInterface;
2014-11-08 18:26:12 +00:00
use League\OAuth2\Server\Storage\SessionInterface;
use League\OAuth2\Server\TokenType\Bearer;
2012-12-28 15:12:16 -05:00
/**
* OAuth 2.0 Resource Server
*/
2014-02-24 14:43:26 +00:00
class ResourceServer extends AbstractServer
2012-12-28 15:12:16 -05:00
{
/**
* The access token
2014-12-10 13:10:35 +00:00
*
* @var \League\OAuth2\Server\Entity\AccessTokenEntity
*/
protected $accessToken;
2012-12-28 15:12:16 -05:00
/**
2013-12-16 23:47:03 +00:00
* The query string key which is used by clients to present the access token (default: access_token)
2014-12-10 13:10:35 +00:00
*
* @var string
*/
2013-12-16 23:47:03 +00:00
protected $tokenKey = 'access_token';
2012-12-28 15:12:16 -05:00
/**
2013-12-16 23:47:03 +00:00
* Initialise the resource server
2014-12-10 13:10:35 +00:00
*
* @param \League\OAuth2\Server\Storage\SessionInterface $sessionStorage
* @param \League\OAuth2\Server\Storage\AccessTokenInterface $accessTokenStorage
* @param \League\OAuth2\Server\Storage\ClientInterface $clientStorage
* @param \League\OAuth2\Server\Storage\ScopeInterface $scopeStorage
2014-12-10 13:10:35 +00:00
*
2013-12-16 23:47:03 +00:00
* @return self
2012-12-28 15:12:16 -05:00
*/
2013-12-16 23:47:03 +00:00
public function __construct(
SessionInterface $sessionStorage,
2014-01-10 17:30:12 +00:00
AccessTokenInterface $accessTokenStorage,
ClientInterface $clientStorage,
ScopeInterface $scopeStorage
2013-12-16 23:47:03 +00:00
) {
$this->setSessionStorage($sessionStorage);
$this->setAccessTokenStorage($accessTokenStorage);
$this->setClientStorage($clientStorage);
$this->setScopeStorage($scopeStorage);
2014-01-10 17:30:12 +00:00
// Set Bearer as the default token type
2014-11-08 18:26:12 +00:00
$this->setTokenType(new Bearer());
parent::__construct();
2013-12-16 23:47:03 +00:00
return $this;
}
2012-12-28 15:12:16 -05:00
/**
* Sets the query string key for the access token.
2014-12-10 13:10:35 +00:00
*
2014-11-12 18:10:29 +00:00
* @param string $key The new query string key
2014-12-10 13:10:35 +00:00
*
2013-12-16 23:47:03 +00:00
* @return self
*/
2014-07-11 18:27:03 +01:00
public function setIdKey($key)
2013-02-05 16:20:45 +00:00
{
$this->tokenKey = $key;
2014-05-03 10:53:57 +01:00
2013-11-25 23:58:42 +00:00
return $this;
2013-02-05 16:20:45 +00:00
}
/**
2013-12-16 23:47:03 +00:00
* Gets the access token
2014-12-10 13:10:35 +00:00
*
2014-11-09 09:45:20 +01:00
* @return \League\OAuth2\Server\Entity\AccessTokenEntity
*/
public function getAccessToken()
{
2014-11-09 09:45:20 +01:00
return $this->accessToken;
}
2014-05-02 17:21:53 +01:00
/**
* Checks if the access token is valid or not
2014-12-10 13:10:35 +00:00
*
* @param bool $headerOnly Limit Access Token to Authorization header
* @param \League\OAuth2\Server\Entity\AccessTokenEntity|null $accessToken Access Token
2014-11-12 18:10:29 +00:00
*
2014-05-02 17:21:53 +01:00
* @return bool
2014-11-12 18:10:29 +00:00
*
* @throws \League\OAuth2\Server\Exception\AccessDeniedException
2014-05-02 17:21:53 +01:00
*/
public function isValidRequest($headerOnly = true, $accessToken = null)
2014-05-02 17:21:53 +01:00
{
2014-05-03 11:08:33 +01:00
$accessTokenString = ($accessToken !== null)
? $accessToken
: $this->determineAccessToken($headerOnly);
2014-05-02 17:21:53 +01:00
// Set the access token
$this->accessToken = $this->getAccessTokenStorage()->get($accessTokenString);
2014-05-03 10:53:57 +01:00
2014-11-08 16:44:39 +00:00
// Ensure the access token exists
if (!$this->accessToken instanceof AccessTokenEntity) {
throw new AccessDeniedException();
2014-11-08 16:44:39 +00:00
}
// Check the access token hasn't expired
// Ensure the auth code hasn't expired
if ($this->accessToken->isExpired() === true) {
throw new AccessDeniedException();
}
return true;
2014-05-02 17:21:53 +01:00
}
/**
2013-12-16 23:47:03 +00:00
* Reads in the access token from the headers
2014-12-10 13:10:35 +00:00
*
* @param bool $headerOnly Limit Access Token to Authorization header
2014-12-10 13:10:35 +00:00
*
* @throws \League\OAuth2\Server\Exception\InvalidRequestException Thrown if there is no access token presented
2014-12-10 13:10:35 +00:00
*
* @return string
*/
public function determineAccessToken($headerOnly = false)
2012-12-28 15:12:16 -05:00
{
if ($this->getRequest()->headers->get('Authorization') !== null) {
$accessToken = $this->getTokenType()->determineAccessTokenInHeader($this->getRequest());
} elseif ($headerOnly === false) {
$accessToken = ($this->getRequest()->server->get('REQUEST_METHOD') === 'GET')
? $this->getRequest()->query->get($this->tokenKey)
: $this->getRequest()->request->get($this->tokenKey);
2012-12-28 15:12:16 -05:00
}
if (empty($accessToken)) {
throw new InvalidRequestException('access token');
2012-12-28 15:12:16 -05:00
}
return $accessToken;
2012-12-28 15:12:16 -05:00
}
}