2013-02-01 16:20:32 +05:30
|
|
|
<?php
|
2013-02-14 01:06:56 +05:30
|
|
|
/**
|
|
|
|
* OAuth 2.0 Auth code grant
|
|
|
|
*
|
2014-01-08 21:45:29 +05:30
|
|
|
* @package league/oauth2-server
|
2013-02-14 01:06:56 +05:30
|
|
|
* @author Alex Bilbie <hello@alexbilbie.com>
|
2014-03-10 01:04:23 +05:30
|
|
|
* @copyright Copyright (c) Alex Bilbie
|
2013-02-14 01:06:56 +05:30
|
|
|
* @license http://mit-license.org/
|
2014-03-10 01:35:38 +05:30
|
|
|
* @link https://github.com/thephpleague/oauth2-server
|
2013-02-14 01:06:56 +05:30
|
|
|
*/
|
2013-02-01 16:20:32 +05:30
|
|
|
|
2013-05-09 00:12:23 +05:30
|
|
|
namespace League\OAuth2\Server\Grant;
|
2013-05-08 23:36:09 +05:30
|
|
|
|
2013-05-09 00:12:23 +05:30
|
|
|
use League\OAuth2\Server\Request;
|
|
|
|
use League\OAuth2\Server\Exception;
|
2014-05-02 21:51:53 +05:30
|
|
|
use League\OAuth2\Server\Entity\ClientEntity;
|
|
|
|
use League\OAuth2\Server\Entity\RefreshTokenEntity;
|
|
|
|
use League\OAuth2\Server\Entity\SessionEntity;
|
|
|
|
use League\OAuth2\Server\Entity\AccessTokenEntity;
|
|
|
|
use League\OAuth2\Server\Entity\AuthCodeEntity;
|
2013-05-09 00:12:23 +05:30
|
|
|
use League\OAuth2\Server\Util\SecureKey;
|
2013-02-01 16:20:32 +05:30
|
|
|
|
2013-02-14 01:06:56 +05:30
|
|
|
/**
|
|
|
|
* Auth code grant class
|
|
|
|
*/
|
2014-05-02 21:54:55 +05:30
|
|
|
class AuthCodeGrant extends AbstractGrant
|
2014-04-06 23:44:46 +05:30
|
|
|
{
|
2013-02-14 01:06:56 +05:30
|
|
|
/**
|
|
|
|
* Grant identifier
|
|
|
|
* @var string
|
|
|
|
*/
|
2013-02-01 20:11:52 +05:30
|
|
|
protected $identifier = 'authorization_code';
|
2013-02-14 01:06:56 +05:30
|
|
|
|
|
|
|
/**
|
|
|
|
* Response type
|
|
|
|
* @var string
|
|
|
|
*/
|
2013-02-01 16:20:32 +05:30
|
|
|
protected $responseType = 'code';
|
|
|
|
|
2013-03-06 22:29:18 +05:30
|
|
|
/**
|
|
|
|
* AuthServer instance
|
|
|
|
* @var AuthServer
|
|
|
|
*/
|
2014-03-10 01:04:23 +05:30
|
|
|
protected $server = null;
|
2013-03-06 22:29:18 +05:30
|
|
|
|
2013-05-07 03:34:00 +05:30
|
|
|
/**
|
|
|
|
* Access token expires in override
|
|
|
|
* @var int
|
|
|
|
*/
|
2013-05-08 03:46:30 +05:30
|
|
|
protected $accessTokenTTL = null;
|
2013-05-07 03:34:00 +05:30
|
|
|
|
2013-05-08 03:50:32 +05:30
|
|
|
/**
|
|
|
|
* The TTL of the auth token
|
|
|
|
* @var integer
|
|
|
|
*/
|
|
|
|
protected $authTokenTTL = 600;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Override the default access token expire time
|
2014-05-03 15:23:43 +05:30
|
|
|
* @param int $authTokenTTL
|
2013-05-08 03:50:32 +05:30
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setAuthTokenTTL($authTokenTTL)
|
|
|
|
{
|
|
|
|
$this->authTokenTTL = $authTokenTTL;
|
|
|
|
}
|
|
|
|
|
2013-02-14 01:06:56 +05:30
|
|
|
/**
|
2013-03-31 18:07:02 +05:30
|
|
|
* Check authorise parameters
|
|
|
|
*
|
2014-03-10 01:04:23 +05:30
|
|
|
* @throws
|
|
|
|
* @return array Authorise request parameters
|
2013-03-31 18:07:02 +05:30
|
|
|
*/
|
2014-03-10 01:04:23 +05:30
|
|
|
public function checkAuthoriseParams()
|
2013-03-31 18:07:02 +05:30
|
|
|
{
|
2014-04-06 23:44:46 +05:30
|
|
|
// Get required params
|
2014-05-07 22:00:07 +05:30
|
|
|
$clientId = $this->server->getRequest()->query->get('client_id', null);
|
2014-03-10 01:04:23 +05:30
|
|
|
if (is_null($clientId)) {
|
2014-04-25 15:54:48 +05:30
|
|
|
throw new Exception\InvalidRequestException('client_id');
|
2013-03-31 18:07:02 +05:30
|
|
|
}
|
|
|
|
|
2014-05-07 22:00:07 +05:30
|
|
|
$redirectUri = $this->server->getRequest()->query->get('redirect_uri', null);
|
2014-03-10 01:04:23 +05:30
|
|
|
if (is_null($redirectUri)) {
|
2014-04-25 15:54:48 +05:30
|
|
|
throw new Exception\InvalidRequestException('redirect_uri');
|
2013-03-31 18:07:02 +05:30
|
|
|
}
|
|
|
|
|
2014-05-07 22:00:07 +05:30
|
|
|
$state = $this->server->getRequest()->query->get('state', null);
|
2014-03-10 01:04:23 +05:30
|
|
|
if ($this->server->stateParamRequired() === true && is_null($state)) {
|
2014-04-25 15:54:48 +05:30
|
|
|
throw new Exception\InvalidRequestException('state');
|
2013-03-31 18:07:02 +05:30
|
|
|
}
|
|
|
|
|
2014-05-07 22:00:07 +05:30
|
|
|
$responseType = $this->server->getRequest()->query->get('response_type', null);
|
2014-03-10 01:04:23 +05:30
|
|
|
if (is_null($responseType)) {
|
2014-04-25 15:54:48 +05:30
|
|
|
throw new Exception\InvalidRequestException('response_type');
|
2013-03-31 18:07:02 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure response type is one that is recognised
|
2014-05-02 21:51:53 +05:30
|
|
|
if (!in_array($responseType, $this->server->getResponseTypes())) {
|
2014-05-01 19:02:54 +05:30
|
|
|
throw new Exception\UnsupportedResponseTypeException($responseType);
|
2013-03-31 18:07:02 +05:30
|
|
|
}
|
|
|
|
|
2014-03-10 01:04:23 +05:30
|
|
|
// Validate client ID and redirect URI
|
|
|
|
$client = $this->server->getStorage('client')->get(
|
|
|
|
$clientId,
|
|
|
|
null,
|
|
|
|
$redirectUri,
|
|
|
|
$this->getIdentifier()
|
|
|
|
);
|
2013-03-31 18:07:02 +05:30
|
|
|
|
2014-05-02 21:51:53 +05:30
|
|
|
if (($client instanceof ClientEntity) === false) {
|
2014-04-25 15:54:48 +05:30
|
|
|
throw new Exception\InvalidClientException();
|
2013-03-31 18:07:02 +05:30
|
|
|
}
|
|
|
|
|
2014-03-10 01:04:23 +05:30
|
|
|
// Validate any scopes that are in the request
|
2014-05-07 22:00:07 +05:30
|
|
|
$scopeParam = $this->server->getRequest()->query->get('scope', '');
|
2014-03-10 01:04:23 +05:30
|
|
|
$scopes = $this->validateScopes($scopeParam);
|
|
|
|
|
|
|
|
return [
|
|
|
|
'client' => $client,
|
|
|
|
'redirect_uri' => $redirectUri,
|
|
|
|
'state' => $state,
|
|
|
|
'response_type' => $responseType,
|
|
|
|
'scopes' => $scopes
|
|
|
|
];
|
2013-03-31 18:07:02 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse a new authorise request
|
|
|
|
*
|
2014-05-03 15:23:43 +05:30
|
|
|
* @param string $type The session owner's type
|
|
|
|
* @param string $typeId The session owner's ID
|
|
|
|
* @param array $authParams The authorise request $_GET parameters
|
|
|
|
* @return string An authorisation code
|
2013-03-31 18:07:02 +05:30
|
|
|
*/
|
2014-04-06 23:44:46 +05:30
|
|
|
public function newAuthoriseRequest($type, $typeId, $authParams = [])
|
2013-03-31 18:07:02 +05:30
|
|
|
{
|
2013-04-29 04:30:04 +05:30
|
|
|
// Create a new session
|
2014-05-02 21:51:53 +05:30
|
|
|
$session = new SessionEntity($this->server);
|
2014-03-10 01:04:23 +05:30
|
|
|
$session->setOwner($type, $typeId);
|
|
|
|
$session->associateClient($authParams['client']);
|
2014-04-06 23:44:46 +05:30
|
|
|
$session->save();
|
2013-04-29 15:55:23 +05:30
|
|
|
|
2014-04-06 23:44:46 +05:30
|
|
|
// Create a new auth code
|
2014-05-02 21:51:53 +05:30
|
|
|
$authCode = new AuthCodeEntity($this->server);
|
2014-04-07 01:38:20 +05:30
|
|
|
$authCode->setToken(SecureKey::generate());
|
2014-04-06 23:44:46 +05:30
|
|
|
$authCode->setRedirectUri($authParams['redirect_uri']);
|
2013-04-29 15:55:23 +05:30
|
|
|
|
2013-05-11 05:23:21 +05:30
|
|
|
foreach ($authParams['scopes'] as $scope) {
|
2014-04-06 23:44:46 +05:30
|
|
|
$authCode->associateScope($scope);
|
2013-05-11 05:23:21 +05:30
|
|
|
}
|
2013-04-29 04:30:04 +05:30
|
|
|
|
2014-04-06 23:44:46 +05:30
|
|
|
$authCode->setSession($session);
|
|
|
|
$authCode->save();
|
|
|
|
|
|
|
|
return $authCode->generateRedirectUri($authParams['state']);
|
2013-03-31 18:07:02 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-02-14 01:06:56 +05:30
|
|
|
* Complete the auth code grant
|
|
|
|
* @param null|array $inputParams
|
|
|
|
* @return array
|
|
|
|
*/
|
2013-02-13 23:55:10 +05:30
|
|
|
public function completeFlow($inputParams = null)
|
2013-02-01 16:20:32 +05:30
|
|
|
{
|
2013-02-14 01:06:56 +05:30
|
|
|
// Get the required params
|
2014-04-06 23:44:46 +05:30
|
|
|
$clientId = $this->server->getRequest()->request->get('client_id', null);
|
|
|
|
if (is_null($clientId)) {
|
2014-04-25 15:54:48 +05:30
|
|
|
throw new Exception\InvalidRequestException('client_id');
|
2013-02-01 20:11:52 +05:30
|
|
|
}
|
2013-02-01 16:20:32 +05:30
|
|
|
|
2014-04-06 23:44:46 +05:30
|
|
|
$clientSecret = $this->server->getRequest()->request->get('client_secret', null);
|
|
|
|
if (is_null($clientSecret)) {
|
2014-04-25 15:54:48 +05:30
|
|
|
throw new Exception\InvalidRequestException('client_secret');
|
2013-02-01 20:11:52 +05:30
|
|
|
}
|
2013-02-01 16:20:32 +05:30
|
|
|
|
2014-04-06 23:44:46 +05:30
|
|
|
$redirectUri = $this->server->getRequest()->request->get('redirect_uri', null);
|
|
|
|
if (is_null($redirectUri)) {
|
2014-04-25 15:54:48 +05:30
|
|
|
throw new Exception\InvalidRequestException('redirect_uri');
|
2013-02-01 20:11:52 +05:30
|
|
|
}
|
2013-02-01 16:20:32 +05:30
|
|
|
|
2014-04-06 23:44:46 +05:30
|
|
|
// Validate client ID and client secret
|
|
|
|
$client = $this->server->getStorage('client')->get(
|
|
|
|
$clientId,
|
|
|
|
$clientSecret,
|
|
|
|
$redirectUri,
|
|
|
|
$this->getIdentifier()
|
|
|
|
);
|
2013-02-01 16:20:32 +05:30
|
|
|
|
2014-05-02 21:51:53 +05:30
|
|
|
if (($client instanceof ClientEntity) === false) {
|
2014-04-25 15:54:48 +05:30
|
|
|
throw new Exception\InvalidClientException();
|
2013-02-01 16:20:32 +05:30
|
|
|
}
|
|
|
|
|
2014-04-06 23:44:46 +05:30
|
|
|
// Validate the auth code
|
|
|
|
$authCode = $this->server->getRequest()->request->get('code', null);
|
|
|
|
if (is_null($authCode)) {
|
2014-04-25 15:54:48 +05:30
|
|
|
throw new Exception\InvalidRequestException('code');
|
2013-02-01 20:11:52 +05:30
|
|
|
}
|
2013-02-01 16:20:32 +05:30
|
|
|
|
2014-04-06 23:44:46 +05:30
|
|
|
$code = $this->server->getStorage('auth_code')->get($authCode);
|
2014-05-02 21:51:53 +05:30
|
|
|
if (($code instanceof AuthCodeEntity) === false) {
|
2014-04-25 15:54:48 +05:30
|
|
|
throw new Exception\InvalidRequestException('code');
|
2013-02-01 16:20:32 +05:30
|
|
|
}
|
|
|
|
|
2014-04-06 23:44:46 +05:30
|
|
|
// Check redirect URI presented matches redirect URI originally used in authorise request
|
|
|
|
if ($code->getRedirectUri() !== $redirectUri) {
|
2014-04-25 15:54:48 +05:30
|
|
|
throw new Exception\InvalidRequestException('redirect_uri');
|
2014-04-06 23:44:46 +05:30
|
|
|
}
|
2013-02-01 16:20:32 +05:30
|
|
|
|
2014-04-06 23:44:46 +05:30
|
|
|
$session = $code->getSession();
|
|
|
|
$authCodeScopes = $code->getScopes();
|
2013-04-29 04:31:34 +05:30
|
|
|
|
2014-04-06 23:44:46 +05:30
|
|
|
// Generate the access token
|
2014-05-02 21:51:53 +05:30
|
|
|
$accessToken = new AccessTokenEntity($this->server);
|
2014-04-07 01:38:20 +05:30
|
|
|
$accessToken->setToken(SecureKey::generate());
|
2014-04-06 23:44:46 +05:30
|
|
|
$accessToken->setExpireTime($this->server->getAccessTokenTTL() + time());
|
2013-04-29 04:32:00 +05:30
|
|
|
|
2014-04-06 23:44:46 +05:30
|
|
|
foreach ($authCodeScopes as $authCodeScope) {
|
|
|
|
$session->associateScope($authCodeScope);
|
2013-04-29 04:31:07 +05:30
|
|
|
}
|
2013-02-01 16:20:32 +05:30
|
|
|
|
2014-04-23 21:32:50 +05:30
|
|
|
$this->server->getTokenType()->set('access_token', $accessToken->getToken());
|
|
|
|
$this->server->getTokenType()->set('expires', $accessToken->getExpireTime());
|
|
|
|
$this->server->getTokenType()->set('expires_in', $this->server->getAccessTokenTTL());
|
2013-02-01 16:20:32 +05:30
|
|
|
|
2013-04-29 16:01:07 +05:30
|
|
|
// Associate a refresh token if set
|
2014-03-10 01:04:23 +05:30
|
|
|
if ($this->server->hasGrantType('refresh_token')) {
|
2014-05-02 21:51:53 +05:30
|
|
|
$refreshToken = new RefreshTokenEntity($this->server);
|
2014-04-07 01:38:20 +05:30
|
|
|
$refreshToken->setToken(SecureKey::generate());
|
2014-04-06 23:44:46 +05:30
|
|
|
$refreshToken->setExpireTime($this->server->getGrantType('refresh_token')->getRefreshTokenTTL() + time());
|
2014-04-23 21:32:50 +05:30
|
|
|
$this->server->getTokenType()->set('refresh_token', $refreshToken->getToken());
|
2014-04-06 23:44:46 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// Expire the auth code
|
|
|
|
$code->expire();
|
|
|
|
|
|
|
|
// Save all the things
|
|
|
|
$session->save();
|
|
|
|
$accessToken->setSession($session);
|
|
|
|
$accessToken->save();
|
|
|
|
|
|
|
|
if ($this->server->hasGrantType('refresh_token')) {
|
|
|
|
$refreshToken->setAccessToken($accessToken);
|
|
|
|
$refreshToken->save();
|
2013-02-01 16:20:32 +05:30
|
|
|
}
|
|
|
|
|
2014-04-23 21:32:50 +05:30
|
|
|
return $this->server->getTokenType()->generateResponse();
|
2013-02-01 16:20:32 +05:30
|
|
|
}
|
2013-09-07 22:29:44 +05:30
|
|
|
}
|