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-03-10 01:04:23 +05:30
|
|
|
* @copyright Copyright (c) Alex Bilbie
|
2013-12-03 00:12:54 +05:30
|
|
|
* @license http://mit-license.org/
|
2014-03-10 01:35:38 +05:30
|
|
|
* @link https://github.com/thephpleague/oauth2-server
|
2013-12-03 00:12:54 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
namespace League\OAuth2\Server\Grant;
|
|
|
|
|
2014-02-24 20:12:22 +05:30
|
|
|
use League\OAuth2\Server\AuthorizationServer;
|
2014-05-02 21:51:53 +05:30
|
|
|
use League\OAuth2\Server\Entity\ScopeEntity;
|
2014-04-25 15:54:48 +05:30
|
|
|
use League\OAuth2\Server\Exception;
|
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-10 18:00:13 +05:30
|
|
|
protected $responseType;
|
2014-01-08 21:45:29 +05:30
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback to authenticate a user's name and password
|
|
|
|
* @var function
|
|
|
|
*/
|
2014-01-10 18:00:13 +05:30
|
|
|
protected $callback;
|
2014-01-08 21:45:29 +05:30
|
|
|
|
|
|
|
/**
|
|
|
|
* AuthServer instance
|
|
|
|
* @var AuthServer
|
|
|
|
*/
|
2014-01-10 18:00:13 +05:30
|
|
|
protected $server;
|
2014-01-08 21:45:29 +05:30
|
|
|
|
|
|
|
/**
|
|
|
|
* Access token expires in override
|
|
|
|
* @var int
|
|
|
|
*/
|
2014-01-10 18:00:13 +05:30
|
|
|
protected $accessTokenTTL;
|
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
|
2014-05-03 15:23:43 +05:30
|
|
|
* @param string $identifier
|
2013-12-05 03:53:19 +05:30
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
public function setIdentifier($identifier)
|
|
|
|
{
|
|
|
|
$this->identifier = $identifier;
|
2014-05-03 15:23:43 +05:30
|
|
|
|
2013-12-05 03:53:19 +05:30
|
|
|
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
|
2014-05-03 15:23:43 +05:30
|
|
|
* @param int $accessTokenTTL
|
2013-12-03 00:12:54 +05:30
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
public function setAccessTokenTTL($accessTokenTTL)
|
|
|
|
{
|
|
|
|
$this->accessTokenTTL = $accessTokenTTL;
|
2014-05-03 15:23:43 +05:30
|
|
|
|
2013-12-03 00:12:54 +05:30
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-12-06 01:55:50 +05:30
|
|
|
/**
|
|
|
|
* Inject the authorization server into the grant
|
2014-05-03 15:23:43 +05:30
|
|
|
* @param AuthorizationServer $server The authorization server instance
|
|
|
|
* @return self
|
2013-12-06 01:55:50 +05:30
|
|
|
*/
|
2014-02-24 20:12:22 +05:30
|
|
|
public function setAuthorizationServer(AuthorizationServer $server)
|
2013-12-06 01:55:50 +05:30
|
|
|
{
|
2013-12-24 22:31:56 +05:30
|
|
|
$this->server = $server;
|
2014-05-03 15:23:43 +05:30
|
|
|
|
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
|
2014-05-03 15:23:43 +05:30
|
|
|
* @param string $scopeParam A string of scopes (e.g. "profile email birthday")
|
2014-01-08 21:45:29 +05:30
|
|
|
* @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]);
|
2014-05-03 15:38:33 +05:30
|
|
|
if ($scopesList[$i] === '') {
|
|
|
|
unset($scopesList[$i]); // Remove any junk scopes
|
|
|
|
}
|
2013-12-24 22:31:56 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
2014-05-03 15:38:33 +05:30
|
|
|
$this->server->scopeParamRequired() === true
|
|
|
|
&& $this->server->getDefaultScope() === null
|
|
|
|
&& count($scopesList) === 0
|
2013-12-24 22:31:56 +05:30
|
|
|
) {
|
2014-04-25 15:54:48 +05:30
|
|
|
throw new Exception\InvalidRequestException('scope');
|
2013-12-24 22:31:56 +05:30
|
|
|
} 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-10 18:00:13 +05:30
|
|
|
$scope = $this->server->getStorage('scope')->get(
|
2013-12-24 22:31:56 +05:30
|
|
|
$scopeItem,
|
|
|
|
$this->getIdentifier()
|
|
|
|
);
|
|
|
|
|
2014-05-02 21:51:53 +05:30
|
|
|
if (($scope instanceof ScopeEntity) === false) {
|
2014-04-25 15:54:48 +05:30
|
|
|
throw new Exception\InvalidScopeException($scopeItem);
|
2013-12-24 22:31:56 +05:30
|
|
|
}
|
|
|
|
|
2014-01-10 18:00:13 +05:30
|
|
|
$scopes[$scope->getId()] = $scope;
|
2013-12-24 22:31:56 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return $scopes;
|
|
|
|
}
|
|
|
|
|
2014-01-10 18:00:13 +05:30
|
|
|
/**
|
|
|
|
* Format the local scopes array
|
2014-05-02 21:51:53 +05:30
|
|
|
* @param array $unformated Array of Array of \League\OAuth2\Server\Entity\ScopeEntity
|
2014-01-10 18:00:13 +05:30
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function formatScopes($unformated = [])
|
|
|
|
{
|
|
|
|
$scopes = [];
|
|
|
|
foreach ($unformated as $scope) {
|
2014-05-02 21:51:53 +05:30
|
|
|
if ($scope instanceof ScopeEntity) {
|
2014-01-10 18:00:13 +05:30
|
|
|
$scopes[$scope->getId()] = $scope;
|
|
|
|
}
|
|
|
|
}
|
2014-05-03 15:23:43 +05:30
|
|
|
|
2014-01-10 18:00:13 +05:30
|
|
|
return $scopes;
|
|
|
|
}
|
|
|
|
|
2014-01-08 21:45:29 +05:30
|
|
|
/**
|
|
|
|
* Complete the grant flow
|
|
|
|
*
|
|
|
|
* Example response:
|
|
|
|
* <pre>
|
|
|
|
* array(
|
2014-05-03 15:23:43 +05:30
|
|
|
* 'access_token' => (string) , // The access token
|
|
|
|
* 'refresh_token' => (string) , // The refresh token (only set if the refresh token grant is enabled)
|
2014-01-08 21:45:29 +05:30
|
|
|
* 'token_type' => 'bearer', // Almost always "bearer" (exceptions: JWT, SAML)
|
2014-05-03 15:23:43 +05:30
|
|
|
* 'expires' => (int) , // The timestamp of when the access token will expire
|
|
|
|
* 'expires_in' => (int) // The number of seconds before the access token will expire
|
2014-01-08 21:45:29 +05:30
|
|
|
* )
|
|
|
|
* </pre>
|
|
|
|
*
|
2014-05-03 15:23:43 +05:30
|
|
|
* @return array An array of parameters to be passed back to the client
|
2014-01-08 21:45:29 +05:30
|
|
|
*/
|
|
|
|
abstract public function completeFlow();
|
2013-12-05 03:53:19 +05:30
|
|
|
}
|