2013-12-03 00:12:54 +05:30
|
|
|
<?php
|
|
|
|
/**
|
2014-01-08 21:45:29 +05:30
|
|
|
* OAuth 2.0 Abstract grant
|
2013-12-03 00:12:54 +05:30
|
|
|
*
|
2014-01-08 21:45:29 +05:30
|
|
|
* @package league/oauth2-server
|
2013-12-03 00:12:54 +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-12-03 00:12:54 +05:30
|
|
|
* @license http://mit-license.org/
|
|
|
|
* @link http://github.com/php-loep/oauth2-server
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace League\OAuth2\Server\Grant;
|
|
|
|
|
2013-12-06 01:55:50 +05:30
|
|
|
use League\OAuth2\Server\Authorization;
|
2014-01-08 21:45:29 +05:30
|
|
|
use League\OAuth2\Server\Entities\Scope;
|
2013-12-06 01:55:50 +05:30
|
|
|
|
2014-01-08 21:45:29 +05:30
|
|
|
/**
|
|
|
|
* Abstract grant class
|
|
|
|
*/
|
|
|
|
abstract class AbstractGrant implements GrantTypeInterface
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Grant identifier
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $identifier = '';
|
2013-12-03 00:12:54 +05:30
|
|
|
|
2013-12-06 01:55:50 +05:30
|
|
|
/**
|
2014-01-08 21:45:29 +05:30
|
|
|
* Response type
|
|
|
|
* @var string
|
2013-12-06 01:55:50 +05:30
|
|
|
*/
|
2014-01-08 21:45:29 +05:30
|
|
|
protected $responseType = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback to authenticate a user's name and password
|
|
|
|
* @var function
|
|
|
|
*/
|
|
|
|
protected $callback = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* AuthServer instance
|
|
|
|
* @var AuthServer
|
|
|
|
*/
|
|
|
|
protected $server = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Access token expires in override
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
protected $accessTokenTTL = null;
|
2013-12-06 01:55:50 +05:30
|
|
|
|
2013-12-03 00:12:54 +05:30
|
|
|
/**
|
|
|
|
* Return the identifier
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getIdentifier()
|
|
|
|
{
|
|
|
|
return $this->identifier;
|
|
|
|
}
|
|
|
|
|
2013-12-05 03:53:19 +05:30
|
|
|
/**
|
|
|
|
* Return the identifier
|
|
|
|
* @param string $identifier
|
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
public function setIdentifier($identifier)
|
|
|
|
{
|
|
|
|
$this->identifier = $identifier;
|
|
|
|
return $this;
|
|
|
|
}
|
2013-12-06 01:58:31 +05:30
|
|
|
|
2013-12-03 00:12:54 +05:30
|
|
|
/**
|
|
|
|
* Return the response type
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getResponseType()
|
|
|
|
{
|
|
|
|
return $this->responseType;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Override the default access token expire time
|
|
|
|
* @param int $accessTokenTTL
|
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
public function setAccessTokenTTL($accessTokenTTL)
|
|
|
|
{
|
|
|
|
$this->accessTokenTTL = $accessTokenTTL;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-12-06 01:55:50 +05:30
|
|
|
/**
|
|
|
|
* Inject the authorization server into the grant
|
2013-12-24 22:31:56 +05:30
|
|
|
* @param Authorization $server The authorization server instance
|
2013-12-06 01:55:50 +05:30
|
|
|
* @return self
|
|
|
|
*/
|
2013-12-24 22:31:56 +05:30
|
|
|
public function setAuthorizationServer(Authorization $server)
|
2013-12-06 01:55:50 +05:30
|
|
|
{
|
2013-12-24 22:31:56 +05:30
|
|
|
$this->server = $server;
|
2013-12-06 01:55:50 +05:30
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2014-01-08 21:45:29 +05:30
|
|
|
/**
|
|
|
|
* Given a list of scopes, validate them and return an arrary of Scope entities
|
|
|
|
* @param string $scopeParam A string of scopes (e.g. "profile email birthday")
|
|
|
|
* @return array
|
|
|
|
* @throws ClientException If scope is invalid, or no scopes passed when required
|
|
|
|
*/
|
2013-12-24 22:31:56 +05:30
|
|
|
public function validateScopes($scopeParam = '')
|
|
|
|
{
|
|
|
|
$scopesList = explode($this->server->getScopeDelimeter(), $scopeParam);
|
|
|
|
|
|
|
|
for ($i = 0; $i < count($scopesList); $i++) {
|
|
|
|
$scopesList[$i] = trim($scopesList[$i]);
|
|
|
|
if ($scopesList[$i] === '') unset($scopesList[$i]); // Remove any junk scopes
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
$this->server->scopeParamRequired() === true &&
|
|
|
|
$this->server->getDefaultScope() === null &&
|
|
|
|
count($scopesList) === 0
|
|
|
|
) {
|
|
|
|
throw new ClientException(sprintf($this->server->getExceptionMessage('invalid_request'), 'scope'), 0);
|
|
|
|
} elseif (count($scopesList) === 0 && $this->server->getDefaultScope() !== null) {
|
|
|
|
if (is_array($this->server->getDefaultScope())) {
|
|
|
|
$scopesList = $this->server->getDefaultScope();
|
|
|
|
} else {
|
|
|
|
$scopesList = [0 => $this->server->getDefaultScope()];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$scopes = [];
|
|
|
|
|
|
|
|
foreach ($scopesList as $scopeItem) {
|
2014-01-08 21:45:29 +05:30
|
|
|
$scope = $this->server->getStorage('scope')->getScope(
|
2013-12-24 22:31:56 +05:30
|
|
|
$scopeItem,
|
|
|
|
$this->getIdentifier()
|
|
|
|
);
|
|
|
|
|
2014-01-08 21:45:29 +05:30
|
|
|
if (($scope instanceof Scope) === false) {
|
2013-12-24 22:31:56 +05:30
|
|
|
throw new ClientException(sprintf($this->server->getExceptionMessage('invalid_scope'), $scopeItem), 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
$scopes[] = $scope;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $scopes;
|
|
|
|
}
|
|
|
|
|
2014-01-08 21:45:29 +05:30
|
|
|
/**
|
|
|
|
* Complete the grant flow
|
|
|
|
*
|
|
|
|
* Example response:
|
|
|
|
* <pre>
|
|
|
|
* array(
|
|
|
|
* 'access_token' => (string), // The access token
|
|
|
|
* 'refresh_token' => (string), // The refresh token (only set if the refresh token grant is enabled)
|
|
|
|
* 'token_type' => 'bearer', // Almost always "bearer" (exceptions: JWT, SAML)
|
|
|
|
* 'expires' => (int), // The timestamp of when the access token will expire
|
|
|
|
* 'expires_in' => (int) // The number of seconds before the access token will expire
|
|
|
|
* )
|
|
|
|
* </pre>
|
|
|
|
*
|
|
|
|
* @return array An array of parameters to be passed back to the client
|
|
|
|
*/
|
|
|
|
abstract public function completeFlow();
|
|
|
|
|
2013-12-05 03:53:19 +05:30
|
|
|
}
|