2013-01-05 01:14:02 +05:30
|
|
|
<?php
|
2013-02-13 02:03:23 +05:30
|
|
|
/**
|
2013-02-20 18:10:42 +05:30
|
|
|
* OAuth 2.0 Authorization Server
|
2013-02-13 02:03:23 +05:30
|
|
|
*
|
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-03-10 01:04:23 +05:30
|
|
|
* @copyright Copyright (c) Alex Bilbie
|
2013-02-13 02:03:23 +05:30
|
|
|
* @license http://mit-license.org/
|
2014-03-10 01:35:38 +05:30
|
|
|
* @link https://github.com/thephpleague/oauth2-server
|
2013-02-13 02:03:23 +05:30
|
|
|
*/
|
2013-01-05 01:14:02 +05:30
|
|
|
|
2013-05-09 00:12:23 +05:30
|
|
|
namespace League\OAuth2\Server;
|
2013-01-05 01:14:02 +05:30
|
|
|
|
2013-12-24 22:32:58 +05:30
|
|
|
use League\OAuth2\Server\Grant\GrantTypeInterface;
|
2013-05-09 00:12:23 +05:30
|
|
|
use League\OAuth2\Server\Storage\ClientInterface;
|
2013-12-24 22:32:58 +05:30
|
|
|
use League\OAuth2\Server\Storage\AccessTokenInterface;
|
|
|
|
use League\OAuth2\Server\Storage\AuthCodeInterface;
|
|
|
|
use League\OAuth2\Server\Storage\RefreshTokenInterface;
|
|
|
|
use League\OAuth2\Server\Storage\SessionInterface;
|
2013-05-09 00:12:23 +05:30
|
|
|
use League\OAuth2\Server\Storage\ScopeInterface;
|
2013-12-24 22:32:58 +05:30
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
2013-01-29 19:46:47 +05:30
|
|
|
|
2013-02-13 02:03:23 +05:30
|
|
|
/**
|
2013-02-20 18:10:42 +05:30
|
|
|
* OAuth 2.0 authorization server class
|
2013-02-13 02:03:23 +05:30
|
|
|
*/
|
2014-02-24 20:12:22 +05:30
|
|
|
class AuthorizationServer extends AbstractServer
|
2013-01-05 01:14:02 +05:30
|
|
|
{
|
2013-01-29 19:48:13 +05:30
|
|
|
/**
|
|
|
|
* The delimeter between scopes specified in the scope query string parameter
|
2013-05-09 06:12:15 +05:30
|
|
|
* The OAuth 2 specification states it should be a space but most use a comma
|
2013-01-29 19:48:13 +05:30
|
|
|
* @var string
|
|
|
|
*/
|
2013-05-09 06:12:15 +05:30
|
|
|
protected $scopeDelimeter = ' ';
|
2013-01-05 01:14:02 +05:30
|
|
|
|
2013-02-13 02:03:23 +05:30
|
|
|
/**
|
|
|
|
* The TTL (time to live) of an access token in seconds (default: 3600)
|
|
|
|
* @var integer
|
|
|
|
*/
|
2013-05-08 23:05:13 +05:30
|
|
|
protected $accessTokenTTL = 3600;
|
2013-01-05 01:14:02 +05:30
|
|
|
|
2013-02-13 02:03:23 +05:30
|
|
|
/**
|
|
|
|
* The registered grant response types
|
|
|
|
* @var array
|
|
|
|
*/
|
2013-12-27 01:52:31 +05:30
|
|
|
protected $responseTypes = [];
|
2013-01-05 01:14:02 +05:30
|
|
|
|
2013-02-13 02:03:23 +05:30
|
|
|
/**
|
|
|
|
* The registered grant types
|
|
|
|
* @var array
|
|
|
|
*/
|
2013-12-27 01:52:31 +05:30
|
|
|
protected $grantTypes = [];
|
2013-01-05 01:14:02 +05:30
|
|
|
|
2013-03-04 18:40:00 +05:30
|
|
|
/**
|
|
|
|
* Require the "scope" parameter to be in checkAuthoriseParams()
|
|
|
|
* @var boolean
|
|
|
|
*/
|
2013-05-09 22:32:41 +05:30
|
|
|
protected $requireScopeParam = false;
|
2013-03-04 18:45:12 +05:30
|
|
|
|
2013-03-04 21:16:02 +05:30
|
|
|
/**
|
2013-05-09 22:45:36 +05:30
|
|
|
* Default scope(s) to be used if none is provided
|
|
|
|
* @var string|array
|
2013-03-04 21:16:02 +05:30
|
|
|
*/
|
2014-01-10 23:00:12 +05:30
|
|
|
protected $defaultScope;
|
2013-03-04 21:16:02 +05:30
|
|
|
|
2013-03-04 18:45:12 +05:30
|
|
|
/**
|
|
|
|
* Require the "state" parameter to be in checkAuthoriseParams()
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
protected $requireStateParam = false;
|
2013-03-04 18:40:00 +05:30
|
|
|
|
2013-02-13 02:03:23 +05:30
|
|
|
/**
|
2013-02-20 18:10:42 +05:30
|
|
|
* Create a new OAuth2 authorization server
|
2013-12-27 01:52:31 +05:30
|
|
|
* @return self
|
2013-02-13 02:03:23 +05:30
|
|
|
*/
|
2013-12-24 22:32:58 +05:30
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->storages = [];
|
2014-05-03 15:23:43 +05:30
|
|
|
|
2013-12-27 01:52:31 +05:30
|
|
|
return $this;
|
2013-12-24 22:32:58 +05:30
|
|
|
}
|
|
|
|
|
2013-12-27 01:52:31 +05:30
|
|
|
/**
|
|
|
|
* Set the client storage
|
2014-05-03 15:23:43 +05:30
|
|
|
* @param ClientInterface $storage
|
2013-12-27 01:52:31 +05:30
|
|
|
* @return self
|
|
|
|
*/
|
2014-01-08 21:45:29 +05:30
|
|
|
public function setClientStorage(ClientInterface $storage)
|
2013-12-24 22:32:58 +05:30
|
|
|
{
|
2014-01-08 21:45:29 +05:30
|
|
|
$storage->setServer($this);
|
|
|
|
$this->storages['client'] = $storage;
|
2014-05-03 15:23:43 +05:30
|
|
|
|
2013-12-27 01:52:31 +05:30
|
|
|
return $this;
|
2013-12-24 22:32:58 +05:30
|
|
|
}
|
|
|
|
|
2013-12-27 01:52:31 +05:30
|
|
|
/**
|
|
|
|
* Set the session storage
|
2014-05-03 15:23:43 +05:30
|
|
|
* @param SessionInterface $storage
|
2013-12-27 01:52:31 +05:30
|
|
|
* @return self
|
|
|
|
*/
|
2014-01-08 21:45:29 +05:30
|
|
|
public function setSessionStorage(SessionInterface $storage)
|
2013-01-29 19:46:47 +05:30
|
|
|
{
|
2014-01-08 21:45:29 +05:30
|
|
|
$storage->setServer($this);
|
|
|
|
$this->storages['session'] = $storage;
|
2014-05-03 15:23:43 +05:30
|
|
|
|
2013-12-27 01:52:31 +05:30
|
|
|
return $this;
|
2013-12-24 22:32:58 +05:30
|
|
|
}
|
|
|
|
|
2013-12-27 01:52:31 +05:30
|
|
|
/**
|
|
|
|
* Set the access token storage
|
2014-05-03 15:23:43 +05:30
|
|
|
* @param AccessTokenInterface $storage
|
2013-12-27 01:52:31 +05:30
|
|
|
* @return self
|
|
|
|
*/
|
2014-01-08 21:45:29 +05:30
|
|
|
public function setAccessTokenStorage(AccessTokenInterface $storage)
|
2013-12-24 22:32:58 +05:30
|
|
|
{
|
2014-01-08 21:45:29 +05:30
|
|
|
$storage->setServer($this);
|
|
|
|
$this->storages['access_token'] = $storage;
|
2014-05-03 15:23:43 +05:30
|
|
|
|
2013-12-27 01:52:31 +05:30
|
|
|
return $this;
|
2013-12-24 22:32:58 +05:30
|
|
|
}
|
|
|
|
|
2013-12-27 01:52:31 +05:30
|
|
|
/**
|
|
|
|
* Set the refresh token storage
|
2014-05-03 15:23:43 +05:30
|
|
|
* @param RefreshTokenInteface $storage
|
2013-12-27 01:52:31 +05:30
|
|
|
* @return self
|
|
|
|
*/
|
2014-01-08 21:45:29 +05:30
|
|
|
public function setRefreshTokenStorage(RefreshTokenInterface $storage)
|
2013-12-24 22:32:58 +05:30
|
|
|
{
|
2014-01-08 21:45:29 +05:30
|
|
|
$storage->setServer($this);
|
|
|
|
$this->storages['refresh_token'] = $storage;
|
2014-05-03 15:23:43 +05:30
|
|
|
|
2013-12-27 01:52:31 +05:30
|
|
|
return $this;
|
2013-12-24 22:32:58 +05:30
|
|
|
}
|
|
|
|
|
2013-12-27 01:52:31 +05:30
|
|
|
/**
|
|
|
|
* Set the auth code storage
|
2014-05-03 15:23:43 +05:30
|
|
|
* @param AuthCodeInterface $authCode
|
2013-12-27 01:52:31 +05:30
|
|
|
* @return self
|
|
|
|
*/
|
2014-03-10 01:32:22 +05:30
|
|
|
public function setAuthCodeStorage(AuthCodeInterface $storage)
|
2013-12-24 22:32:58 +05:30
|
|
|
{
|
2014-01-08 21:45:29 +05:30
|
|
|
$storage->setServer($this);
|
2014-03-10 01:32:22 +05:30
|
|
|
$this->storages['auth_code'] = $storage;
|
2014-05-03 15:23:43 +05:30
|
|
|
|
2013-12-27 01:52:31 +05:30
|
|
|
return $this;
|
2013-12-24 22:32:58 +05:30
|
|
|
}
|
|
|
|
|
2013-12-27 01:52:31 +05:30
|
|
|
/**
|
|
|
|
* Set the scope storage
|
2014-05-03 15:23:43 +05:30
|
|
|
* @param ScopeInterface $storage
|
2013-12-27 01:52:31 +05:30
|
|
|
* @return self
|
|
|
|
*/
|
2014-01-08 21:45:29 +05:30
|
|
|
public function setScopeStorage(ScopeInterface $storage)
|
2013-12-24 22:32:58 +05:30
|
|
|
{
|
2014-01-08 21:45:29 +05:30
|
|
|
$storage->setServer($this);
|
|
|
|
$this->storages['scope'] = $storage;
|
2014-05-03 15:23:43 +05:30
|
|
|
|
2013-12-27 01:52:31 +05:30
|
|
|
return $this;
|
2013-01-05 01:14:02 +05:30
|
|
|
}
|
|
|
|
|
2013-02-13 02:03:23 +05:30
|
|
|
/**
|
|
|
|
* Enable support for a grant
|
2014-05-03 15:23:43 +05:30
|
|
|
* @param GrantTypeInterface $grantType A grant class which conforms to Interface/GrantTypeInterface
|
|
|
|
* @param null|string $identifier An identifier for the grant (autodetected if not passed)
|
2013-12-24 22:32:58 +05:30
|
|
|
* @return self
|
2013-02-13 02:03:23 +05:30
|
|
|
*/
|
2013-01-29 19:47:56 +05:30
|
|
|
public function addGrantType(GrantTypeInterface $grantType, $identifier = null)
|
2013-01-05 01:14:02 +05:30
|
|
|
{
|
|
|
|
if (is_null($identifier)) {
|
2013-01-29 19:47:56 +05:30
|
|
|
$identifier = $grantType->getIdentifier();
|
|
|
|
}
|
2013-12-06 01:55:50 +05:30
|
|
|
|
|
|
|
// Inject server into grant
|
|
|
|
$grantType->setAuthorizationServer($this);
|
|
|
|
|
2013-03-06 22:29:18 +05:30
|
|
|
$this->grantTypes[$identifier] = $grantType;
|
2013-01-29 19:47:56 +05:30
|
|
|
|
2014-05-03 15:38:33 +05:30
|
|
|
if (!is_null($grantType->getResponseType())) {
|
2013-01-29 19:47:56 +05:30
|
|
|
$this->responseTypes[] = $grantType->getResponseType();
|
2013-01-05 01:14:02 +05:30
|
|
|
}
|
2013-12-24 22:32:58 +05:30
|
|
|
|
|
|
|
return $this;
|
2013-01-05 01:14:02 +05:30
|
|
|
}
|
|
|
|
|
2013-02-13 02:03:23 +05:30
|
|
|
/**
|
|
|
|
* Check if a grant type has been enabled
|
|
|
|
* @param string $identifier The grant type identifier
|
2013-12-24 22:32:58 +05:30
|
|
|
* @return boolean Returns "true" if enabled, "false" if not
|
2013-02-13 02:03:23 +05:30
|
|
|
*/
|
2013-03-06 22:29:18 +05:30
|
|
|
public function hasGrantType($identifier)
|
2013-02-01 20:11:10 +05:30
|
|
|
{
|
2013-03-06 22:29:18 +05:30
|
|
|
return (array_key_exists($identifier, $this->grantTypes));
|
2013-02-01 20:11:10 +05:30
|
|
|
}
|
|
|
|
|
2013-11-26 05:28:42 +05:30
|
|
|
/**
|
|
|
|
* Returns response types
|
|
|
|
* @return array
|
|
|
|
*/
|
2013-03-31 18:07:12 +05:30
|
|
|
public function getResponseTypes()
|
|
|
|
{
|
|
|
|
return $this->responseTypes;
|
|
|
|
}
|
|
|
|
|
2013-03-04 18:40:00 +05:30
|
|
|
/**
|
|
|
|
* Require the "scope" paremter in checkAuthoriseParams()
|
|
|
|
* @param boolean $require
|
2013-12-24 22:32:58 +05:30
|
|
|
* @return self
|
2013-03-04 18:40:00 +05:30
|
|
|
*/
|
2013-05-11 11:31:54 +05:30
|
|
|
public function requireScopeParam($require = true)
|
2013-03-04 18:45:12 +05:30
|
|
|
{
|
|
|
|
$this->requireScopeParam = $require;
|
2014-05-03 15:23:43 +05:30
|
|
|
|
2013-12-24 22:32:58 +05:30
|
|
|
return $this;
|
2013-03-04 18:45:12 +05:30
|
|
|
}
|
|
|
|
|
2013-03-21 21:52:29 +05:30
|
|
|
/**
|
|
|
|
* Is the scope parameter required?
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function scopeParamRequired()
|
|
|
|
{
|
|
|
|
return $this->requireScopeParam;
|
|
|
|
}
|
|
|
|
|
2013-03-04 21:16:02 +05:30
|
|
|
/**
|
2014-01-08 21:45:29 +05:30
|
|
|
* Default scope to be used if none is provided and requireScopeParam() is false
|
|
|
|
* @param string $default Name of the default scope
|
2013-12-24 22:32:58 +05:30
|
|
|
* @param self
|
2013-03-04 21:16:02 +05:30
|
|
|
*/
|
2013-03-21 21:52:16 +05:30
|
|
|
public function setDefaultScope($default = null)
|
2013-03-04 21:16:02 +05:30
|
|
|
{
|
|
|
|
$this->defaultScope = $default;
|
2014-05-03 15:23:43 +05:30
|
|
|
|
2013-11-26 05:28:42 +05:30
|
|
|
return $this;
|
2013-03-04 21:16:02 +05:30
|
|
|
}
|
|
|
|
|
2013-03-21 21:52:44 +05:30
|
|
|
/**
|
|
|
|
* Default scope to be used if none is provided and requireScopeParam is false
|
|
|
|
* @return string|null
|
|
|
|
*/
|
|
|
|
public function getDefaultScope()
|
|
|
|
{
|
|
|
|
return $this->defaultScope;
|
|
|
|
}
|
|
|
|
|
2013-03-31 18:27:24 +05:30
|
|
|
/**
|
|
|
|
* Require the "state" paremter in checkAuthoriseParams()
|
|
|
|
* @param boolean $require
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function stateParamRequired()
|
|
|
|
{
|
|
|
|
return $this->requireStateParam;
|
|
|
|
}
|
|
|
|
|
2013-03-04 18:45:12 +05:30
|
|
|
/**
|
|
|
|
* Require the "state" paremter in checkAuthoriseParams()
|
|
|
|
* @param boolean $require
|
|
|
|
* @return void
|
|
|
|
*/
|
2013-05-11 11:31:54 +05:30
|
|
|
public function requireStateParam($require = true)
|
2013-03-04 18:40:00 +05:30
|
|
|
{
|
2013-03-04 18:45:12 +05:30
|
|
|
$this->requireStateParam = $require;
|
2014-05-03 15:23:43 +05:30
|
|
|
|
2013-11-26 05:28:42 +05:30
|
|
|
return $this;
|
2013-03-04 18:40:00 +05:30
|
|
|
}
|
|
|
|
|
2013-02-13 02:03:23 +05:30
|
|
|
/**
|
|
|
|
* Get the scope delimeter
|
|
|
|
* @return string The scope delimiter (default: ",")
|
|
|
|
*/
|
2013-01-22 21:55:51 +05:30
|
|
|
public function getScopeDelimeter()
|
|
|
|
{
|
|
|
|
return $this->scopeDelimeter;
|
|
|
|
}
|
|
|
|
|
2013-02-13 02:03:23 +05:30
|
|
|
/**
|
|
|
|
* Set the scope delimiter
|
|
|
|
* @param string $scopeDelimeter
|
|
|
|
*/
|
2013-05-09 06:36:09 +05:30
|
|
|
public function setScopeDelimeter($scopeDelimeter = ' ')
|
2013-01-05 01:14:02 +05:30
|
|
|
{
|
2013-02-13 02:03:23 +05:30
|
|
|
$this->scopeDelimeter = $scopeDelimeter;
|
2014-05-03 15:23:43 +05:30
|
|
|
|
2013-11-26 05:28:42 +05:30
|
|
|
return $this;
|
2013-01-05 01:14:02 +05:30
|
|
|
}
|
|
|
|
|
2013-02-13 02:03:23 +05:30
|
|
|
/**
|
|
|
|
* Get the TTL for an access token
|
|
|
|
* @return int The TTL
|
|
|
|
*/
|
2013-05-08 23:05:13 +05:30
|
|
|
public function getAccessTokenTTL()
|
2013-01-22 21:55:51 +05:30
|
|
|
{
|
2013-05-08 23:05:13 +05:30
|
|
|
return $this->accessTokenTTL;
|
2013-01-22 21:55:51 +05:30
|
|
|
}
|
|
|
|
|
2013-02-13 02:03:23 +05:30
|
|
|
/**
|
|
|
|
* Set the TTL for an access token
|
2013-05-08 03:46:30 +05:30
|
|
|
* @param int $accessTokenTTL The new TTL
|
2013-02-13 02:03:23 +05:30
|
|
|
*/
|
2013-05-09 06:36:09 +05:30
|
|
|
public function setAccessTokenTTL($accessTokenTTL = 3600)
|
2013-01-05 01:14:02 +05:30
|
|
|
{
|
2013-05-08 03:46:30 +05:30
|
|
|
$this->accessTokenTTL = $accessTokenTTL;
|
2014-05-03 15:23:43 +05:30
|
|
|
|
2013-11-26 05:28:42 +05:30
|
|
|
return $this;
|
2013-01-05 01:14:02 +05:30
|
|
|
}
|
|
|
|
|
2013-01-22 21:55:51 +05:30
|
|
|
/**
|
|
|
|
* Issue an access token
|
2013-12-24 22:32:58 +05:30
|
|
|
* @return array Authorise request parameters
|
2013-01-22 21:55:51 +05:30
|
|
|
*/
|
2014-01-10 18:00:13 +05:30
|
|
|
public function issueAccessToken()
|
2013-01-22 21:55:51 +05:30
|
|
|
{
|
2013-12-24 22:32:58 +05:30
|
|
|
$grantType = $this->getRequest()->request->get('grant_type');
|
2013-02-14 01:06:10 +05:30
|
|
|
if (is_null($grantType)) {
|
2014-04-25 15:54:33 +05:30
|
|
|
throw new Exception\InvalidRequestException('grant_type');
|
2013-01-29 21:54:28 +05:30
|
|
|
}
|
2013-01-22 21:55:51 +05:30
|
|
|
|
2013-01-29 21:54:28 +05:30
|
|
|
// Ensure grant type is one that is recognised and is enabled
|
2014-05-03 15:38:33 +05:30
|
|
|
if (!in_array($grantType, array_keys($this->grantTypes))) {
|
2014-04-25 15:54:33 +05:30
|
|
|
throw new Exception\UnsupportedGrantTypeException($grantType);
|
2013-01-29 21:54:28 +05:30
|
|
|
}
|
2013-01-22 21:55:51 +05:30
|
|
|
|
2013-01-29 21:54:48 +05:30
|
|
|
// Complete the flow
|
2014-01-10 18:00:13 +05:30
|
|
|
return $this->getGrantType($grantType)->completeFlow();
|
2013-01-29 21:54:48 +05:30
|
|
|
}
|
2013-01-22 21:55:51 +05:30
|
|
|
|
2013-02-13 02:03:23 +05:30
|
|
|
/**
|
|
|
|
* Return a grant type class
|
2014-05-03 15:23:43 +05:30
|
|
|
* @param string $grantType The grant type identifer
|
2014-04-25 15:54:33 +05:30
|
|
|
* @return Grant\GrantTypeInterface
|
2013-02-13 02:03:23 +05:30
|
|
|
*/
|
2013-03-06 23:31:34 +05:30
|
|
|
public function getGrantType($grantType)
|
2013-01-29 21:54:48 +05:30
|
|
|
{
|
2013-05-07 02:27:46 +05:30
|
|
|
if (isset($this->grantTypes[$grantType])) {
|
|
|
|
return $this->grantTypes[$grantType];
|
|
|
|
}
|
|
|
|
|
2014-04-25 15:54:33 +05:30
|
|
|
throw new Exception\InvalidGrantException($grantType);
|
2013-02-13 23:55:10 +05:30
|
|
|
}
|
2013-01-05 01:14:02 +05:30
|
|
|
}
|