2013-01-04 14:44:02 -05:00
|
|
|
<?php
|
2013-02-12 20:33:23 +00:00
|
|
|
/**
|
2013-02-20 12:40:42 +00:00
|
|
|
* OAuth 2.0 Authorization Server
|
2013-02-12 20:33:23 +00:00
|
|
|
*
|
2014-01-08 16:15:29 +00:00
|
|
|
* @package league/oauth2-server
|
2013-02-12 20:33:23 +00:00
|
|
|
* @author Alex Bilbie <hello@alexbilbie.com>
|
2014-03-09 19:34:23 +00:00
|
|
|
* @copyright Copyright (c) Alex Bilbie
|
2013-02-12 20:33:23 +00:00
|
|
|
* @license http://mit-license.org/
|
2014-03-09 20:05:38 +00:00
|
|
|
* @link https://github.com/thephpleague/oauth2-server
|
2013-02-12 20:33:23 +00:00
|
|
|
*/
|
2013-01-04 14:44:02 -05:00
|
|
|
|
2013-05-08 11:42:23 -07:00
|
|
|
namespace League\OAuth2\Server;
|
2013-01-04 14:44:02 -05:00
|
|
|
|
2013-12-24 17:02:58 +00:00
|
|
|
use League\OAuth2\Server\Grant\GrantTypeInterface;
|
2014-05-07 17:09:34 +01:00
|
|
|
use League\OAuth2\Server\TokenType\Bearer;
|
2013-01-29 14:16:47 +00:00
|
|
|
|
2013-02-12 20:33:23 +00:00
|
|
|
/**
|
2013-02-20 12:40:42 +00:00
|
|
|
* OAuth 2.0 authorization server class
|
2013-02-12 20:33:23 +00:00
|
|
|
*/
|
2014-02-24 14:42:22 +00:00
|
|
|
class AuthorizationServer extends AbstractServer
|
2013-01-04 14:44:02 -05:00
|
|
|
{
|
2013-01-29 14:18:13 +00:00
|
|
|
/**
|
|
|
|
* The delimeter between scopes specified in the scope query string parameter
|
2013-05-08 17:42:15 -07:00
|
|
|
* The OAuth 2 specification states it should be a space but most use a comma
|
2013-01-29 14:18:13 +00:00
|
|
|
* @var string
|
|
|
|
*/
|
2014-11-20 23:52:29 +00:00
|
|
|
protected $scopeDelimiter = ' ';
|
2013-01-04 14:44:02 -05:00
|
|
|
|
2013-02-12 20:33:23 +00:00
|
|
|
/**
|
|
|
|
* The TTL (time to live) of an access token in seconds (default: 3600)
|
|
|
|
* @var integer
|
|
|
|
*/
|
2013-05-08 10:35:13 -07:00
|
|
|
protected $accessTokenTTL = 3600;
|
2013-01-04 14:44:02 -05:00
|
|
|
|
2013-02-12 20:33:23 +00:00
|
|
|
/**
|
|
|
|
* The registered grant response types
|
|
|
|
* @var array
|
|
|
|
*/
|
2013-12-26 20:22:31 +00:00
|
|
|
protected $responseTypes = [];
|
2013-01-04 14:44:02 -05:00
|
|
|
|
2013-02-12 20:33:23 +00:00
|
|
|
/**
|
|
|
|
* The registered grant types
|
|
|
|
* @var array
|
|
|
|
*/
|
2013-12-26 20:22:31 +00:00
|
|
|
protected $grantTypes = [];
|
2013-01-04 14:44:02 -05:00
|
|
|
|
2013-03-04 13:10:00 +00:00
|
|
|
/**
|
|
|
|
* Require the "scope" parameter to be in checkAuthoriseParams()
|
|
|
|
* @var boolean
|
|
|
|
*/
|
2013-05-09 10:02:41 -07:00
|
|
|
protected $requireScopeParam = false;
|
2013-03-04 13:15:12 +00:00
|
|
|
|
2013-03-04 17:46:02 +02:00
|
|
|
/**
|
2013-05-09 10:15:36 -07:00
|
|
|
* Default scope(s) to be used if none is provided
|
|
|
|
* @var string|array
|
2013-03-04 17:46:02 +02:00
|
|
|
*/
|
2014-01-10 17:30:12 +00:00
|
|
|
protected $defaultScope;
|
2013-03-04 17:46:02 +02:00
|
|
|
|
2013-03-04 13:15:12 +00:00
|
|
|
/**
|
|
|
|
* Require the "state" parameter to be in checkAuthoriseParams()
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
protected $requireStateParam = false;
|
2013-03-04 13:10:00 +00:00
|
|
|
|
2013-02-12 20:33:23 +00:00
|
|
|
/**
|
2013-02-20 12:40:42 +00:00
|
|
|
* Create a new OAuth2 authorization server
|
2013-12-26 20:22:31 +00:00
|
|
|
* @return self
|
2013-02-12 20:33:23 +00:00
|
|
|
*/
|
2013-12-24 17:02:58 +00:00
|
|
|
public function __construct()
|
|
|
|
{
|
2014-05-07 17:10:52 +01:00
|
|
|
// Set Bearer as the default token type
|
2014-11-08 18:26:12 +00:00
|
|
|
$this->setTokenType(new Bearer());
|
2014-05-07 17:09:34 +01:00
|
|
|
|
2014-07-11 15:13:28 +01:00
|
|
|
parent::__construct();
|
|
|
|
|
2013-12-26 20:22:31 +00:00
|
|
|
return $this;
|
2013-12-24 17:02:58 +00:00
|
|
|
}
|
|
|
|
|
2013-02-12 20:33:23 +00:00
|
|
|
/**
|
|
|
|
* Enable support for a grant
|
2014-05-03 10:53:43 +01:00
|
|
|
* @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 17:02:58 +00:00
|
|
|
* @return self
|
2013-02-12 20:33:23 +00:00
|
|
|
*/
|
2013-01-29 14:17:56 +00:00
|
|
|
public function addGrantType(GrantTypeInterface $grantType, $identifier = null)
|
2013-01-04 14:44:02 -05:00
|
|
|
{
|
|
|
|
if (is_null($identifier)) {
|
2013-01-29 14:17:56 +00:00
|
|
|
$identifier = $grantType->getIdentifier();
|
|
|
|
}
|
2013-12-05 20:25:50 +00:00
|
|
|
|
|
|
|
// Inject server into grant
|
|
|
|
$grantType->setAuthorizationServer($this);
|
|
|
|
|
2013-03-06 16:59:18 +00:00
|
|
|
$this->grantTypes[$identifier] = $grantType;
|
2013-01-29 14:17:56 +00:00
|
|
|
|
2014-05-03 11:08:33 +01:00
|
|
|
if (!is_null($grantType->getResponseType())) {
|
2013-01-29 14:17:56 +00:00
|
|
|
$this->responseTypes[] = $grantType->getResponseType();
|
2013-01-04 14:44:02 -05:00
|
|
|
}
|
2013-12-24 17:02:58 +00:00
|
|
|
|
|
|
|
return $this;
|
2013-01-04 14:44:02 -05:00
|
|
|
}
|
|
|
|
|
2013-02-12 20:33:23 +00:00
|
|
|
/**
|
|
|
|
* Check if a grant type has been enabled
|
2014-12-03 23:21:54 +00:00
|
|
|
* @param string $identifier The grant type identifier
|
2013-12-24 17:02:58 +00:00
|
|
|
* @return boolean Returns "true" if enabled, "false" if not
|
2013-02-12 20:33:23 +00:00
|
|
|
*/
|
2013-03-06 16:59:18 +00:00
|
|
|
public function hasGrantType($identifier)
|
2013-02-01 14:41:10 +00:00
|
|
|
{
|
2013-03-06 16:59:18 +00:00
|
|
|
return (array_key_exists($identifier, $this->grantTypes));
|
2013-02-01 14:41:10 +00:00
|
|
|
}
|
|
|
|
|
2013-11-25 23:58:42 +00:00
|
|
|
/**
|
|
|
|
* Returns response types
|
|
|
|
* @return array
|
|
|
|
*/
|
2013-03-31 13:37:12 +01:00
|
|
|
public function getResponseTypes()
|
|
|
|
{
|
|
|
|
return $this->responseTypes;
|
|
|
|
}
|
|
|
|
|
2013-03-04 13:10:00 +00:00
|
|
|
/**
|
2014-11-20 23:53:14 +00:00
|
|
|
* Require the "scope" parameter in checkAuthoriseParams()
|
2013-03-04 13:10:00 +00:00
|
|
|
* @param boolean $require
|
2013-12-24 17:02:58 +00:00
|
|
|
* @return self
|
2013-03-04 13:10:00 +00:00
|
|
|
*/
|
2013-05-10 23:01:54 -07:00
|
|
|
public function requireScopeParam($require = true)
|
2013-03-04 13:15:12 +00:00
|
|
|
{
|
|
|
|
$this->requireScopeParam = $require;
|
2014-05-03 10:53:43 +01:00
|
|
|
|
2013-12-24 17:02:58 +00:00
|
|
|
return $this;
|
2013-03-04 13:15:12 +00:00
|
|
|
}
|
|
|
|
|
2013-03-21 16:22:29 +00:00
|
|
|
/**
|
|
|
|
* Is the scope parameter required?
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function scopeParamRequired()
|
|
|
|
{
|
|
|
|
return $this->requireScopeParam;
|
|
|
|
}
|
|
|
|
|
2013-03-04 17:46:02 +02:00
|
|
|
/**
|
2014-01-08 16:15:29 +00:00
|
|
|
* Default scope to be used if none is provided and requireScopeParam() is false
|
|
|
|
* @param string $default Name of the default scope
|
2014-11-12 18:10:29 +00:00
|
|
|
* @return self
|
2013-03-04 17:46:02 +02:00
|
|
|
*/
|
2013-03-21 16:22:16 +00:00
|
|
|
public function setDefaultScope($default = null)
|
2013-03-04 17:46:02 +02:00
|
|
|
{
|
|
|
|
$this->defaultScope = $default;
|
2014-05-03 10:53:43 +01:00
|
|
|
|
2013-11-25 23:58:42 +00:00
|
|
|
return $this;
|
2013-03-04 17:46:02 +02:00
|
|
|
}
|
|
|
|
|
2013-03-21 16:22:44 +00:00
|
|
|
/**
|
|
|
|
* 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 13:57:24 +01:00
|
|
|
/**
|
|
|
|
* Require the "state" paremter in checkAuthoriseParams()
|
2014-11-12 18:10:29 +00:00
|
|
|
* @return bool
|
2013-03-31 13:57:24 +01:00
|
|
|
*/
|
|
|
|
public function stateParamRequired()
|
|
|
|
{
|
|
|
|
return $this->requireStateParam;
|
|
|
|
}
|
|
|
|
|
2013-03-04 13:15:12 +00:00
|
|
|
/**
|
|
|
|
* Require the "state" paremter in checkAuthoriseParams()
|
|
|
|
* @param boolean $require
|
2014-11-12 18:10:29 +00:00
|
|
|
* @return self
|
2013-03-04 13:15:12 +00:00
|
|
|
*/
|
2013-05-10 23:01:54 -07:00
|
|
|
public function requireStateParam($require = true)
|
2013-03-04 13:10:00 +00:00
|
|
|
{
|
2013-03-04 13:15:12 +00:00
|
|
|
$this->requireStateParam = $require;
|
2014-05-03 10:53:43 +01:00
|
|
|
|
2013-11-25 23:58:42 +00:00
|
|
|
return $this;
|
2013-03-04 13:10:00 +00:00
|
|
|
}
|
|
|
|
|
2013-02-12 20:33:23 +00:00
|
|
|
/**
|
2014-11-20 23:52:29 +00:00
|
|
|
* Get the scope delimiter
|
2013-02-12 20:33:23 +00:00
|
|
|
* @return string The scope delimiter (default: ",")
|
|
|
|
*/
|
2014-11-20 23:52:29 +00:00
|
|
|
public function getScopeDelimiter()
|
2013-01-22 11:25:51 -05:00
|
|
|
{
|
2014-11-20 23:52:29 +00:00
|
|
|
return $this->scopeDelimiter;
|
2013-01-22 11:25:51 -05:00
|
|
|
}
|
|
|
|
|
2013-02-12 20:33:23 +00:00
|
|
|
/**
|
|
|
|
* Set the scope delimiter
|
2014-11-20 23:52:29 +00:00
|
|
|
* @param string $scopeDelimiter
|
2014-11-12 18:10:29 +00:00
|
|
|
* @return self
|
2013-02-12 20:33:23 +00:00
|
|
|
*/
|
2014-11-20 23:52:29 +00:00
|
|
|
public function setScopeDelimiter($scopeDelimiter = ' ')
|
2013-01-04 14:44:02 -05:00
|
|
|
{
|
2014-11-20 23:52:29 +00:00
|
|
|
$this->scopeDelimiter = $scopeDelimiter;
|
2014-05-03 10:53:43 +01:00
|
|
|
|
2013-11-25 23:58:42 +00:00
|
|
|
return $this;
|
2013-01-04 14:44:02 -05:00
|
|
|
}
|
|
|
|
|
2013-02-12 20:33:23 +00:00
|
|
|
/**
|
|
|
|
* Get the TTL for an access token
|
|
|
|
* @return int The TTL
|
|
|
|
*/
|
2013-05-08 10:35:13 -07:00
|
|
|
public function getAccessTokenTTL()
|
2013-01-22 11:25:51 -05:00
|
|
|
{
|
2013-05-08 10:35:13 -07:00
|
|
|
return $this->accessTokenTTL;
|
2013-01-22 11:25:51 -05:00
|
|
|
}
|
|
|
|
|
2013-02-12 20:33:23 +00:00
|
|
|
/**
|
|
|
|
* Set the TTL for an access token
|
2013-05-07 15:16:30 -07:00
|
|
|
* @param int $accessTokenTTL The new TTL
|
2014-11-12 18:10:29 +00:00
|
|
|
* @return self
|
2013-02-12 20:33:23 +00:00
|
|
|
*/
|
2013-05-08 18:06:09 -07:00
|
|
|
public function setAccessTokenTTL($accessTokenTTL = 3600)
|
2013-01-04 14:44:02 -05:00
|
|
|
{
|
2013-05-07 15:16:30 -07:00
|
|
|
$this->accessTokenTTL = $accessTokenTTL;
|
2014-05-03 10:53:43 +01:00
|
|
|
|
2013-11-25 23:58:42 +00:00
|
|
|
return $this;
|
2013-01-04 14:44:02 -05:00
|
|
|
}
|
|
|
|
|
2013-01-22 11:25:51 -05:00
|
|
|
/**
|
|
|
|
* Issue an access token
|
2013-12-24 17:02:58 +00:00
|
|
|
* @return array Authorise request parameters
|
2014-11-12 18:10:29 +00:00
|
|
|
* @throws
|
2013-01-22 11:25:51 -05:00
|
|
|
*/
|
2014-01-10 12:30:13 +00:00
|
|
|
public function issueAccessToken()
|
2013-01-22 11:25:51 -05:00
|
|
|
{
|
2013-12-24 17:02:58 +00:00
|
|
|
$grantType = $this->getRequest()->request->get('grant_type');
|
2013-02-13 19:36:10 +00:00
|
|
|
if (is_null($grantType)) {
|
2014-04-25 11:24:33 +01:00
|
|
|
throw new Exception\InvalidRequestException('grant_type');
|
2013-01-29 16:24:28 +00:00
|
|
|
}
|
2013-01-22 11:25:51 -05:00
|
|
|
|
2013-01-29 16:24:28 +00:00
|
|
|
// Ensure grant type is one that is recognised and is enabled
|
2014-05-03 11:08:33 +01:00
|
|
|
if (!in_array($grantType, array_keys($this->grantTypes))) {
|
2014-04-25 11:24:33 +01:00
|
|
|
throw new Exception\UnsupportedGrantTypeException($grantType);
|
2013-01-29 16:24:28 +00:00
|
|
|
}
|
2013-01-22 11:25:51 -05:00
|
|
|
|
2013-01-29 16:24:48 +00:00
|
|
|
// Complete the flow
|
2014-01-10 12:30:13 +00:00
|
|
|
return $this->getGrantType($grantType)->completeFlow();
|
2013-01-29 16:24:48 +00:00
|
|
|
}
|
2013-01-22 11:25:51 -05:00
|
|
|
|
2013-02-12 20:33:23 +00:00
|
|
|
/**
|
|
|
|
* Return a grant type class
|
2014-12-03 23:21:54 +00:00
|
|
|
* @param string $grantType The grant type identifier
|
2014-04-25 11:24:33 +01:00
|
|
|
* @return Grant\GrantTypeInterface
|
2014-11-12 18:10:29 +00:00
|
|
|
* @throws
|
2013-02-12 20:33:23 +00:00
|
|
|
*/
|
2013-03-06 18:01:34 +00:00
|
|
|
public function getGrantType($grantType)
|
2013-01-29 16:24:48 +00:00
|
|
|
{
|
2013-05-06 13:57:46 -07:00
|
|
|
if (isset($this->grantTypes[$grantType])) {
|
|
|
|
return $this->grantTypes[$grantType];
|
|
|
|
}
|
|
|
|
|
2014-04-25 11:24:33 +01:00
|
|
|
throw new Exception\InvalidGrantException($grantType);
|
2013-02-13 18:25:10 +00:00
|
|
|
}
|
2013-01-04 14:44:02 -05:00
|
|
|
}
|