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
|
|
|
|
2014-11-08 23:56:12 +05:30
|
|
|
use League\OAuth2\Server\Entity\AccessTokenEntity;
|
|
|
|
use League\OAuth2\Server\Entity\AuthCodeEntity;
|
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;
|
2014-10-01 02:46:26 +05:30
|
|
|
use League\OAuth2\Server\Event;
|
2014-11-08 23:56:12 +05:30
|
|
|
use League\OAuth2\Server\Exception;
|
|
|
|
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
|
2014-12-10 18:40:35 +05:30
|
|
|
*
|
2013-02-14 01:06:56 +05:30
|
|
|
* @var string
|
|
|
|
*/
|
2013-02-01 20:11:52 +05:30
|
|
|
protected $identifier = 'authorization_code';
|
2013-02-14 01:06:56 +05:30
|
|
|
|
|
|
|
/**
|
|
|
|
* Response type
|
2014-12-10 18:40:35 +05:30
|
|
|
*
|
2013-02-14 01:06:56 +05:30
|
|
|
* @var string
|
|
|
|
*/
|
2013-02-01 16:20:32 +05:30
|
|
|
protected $responseType = 'code';
|
|
|
|
|
2013-03-06 22:29:18 +05:30
|
|
|
/**
|
|
|
|
* AuthServer instance
|
2014-12-10 18:40:35 +05:30
|
|
|
*
|
2014-11-12 23:40:29 +05:30
|
|
|
* @var \League\OAuth2\Server\AuthorizationServer
|
2013-03-06 22:29:18 +05:30
|
|
|
*/
|
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
|
2014-12-10 18:40:35 +05:30
|
|
|
*
|
2013-05-07 03:34:00 +05:30
|
|
|
* @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
|
2014-12-10 18:40:35 +05:30
|
|
|
*
|
2013-05-08 03:50:32 +05:30
|
|
|
* @var integer
|
|
|
|
*/
|
|
|
|
protected $authTokenTTL = 600;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Override the default access token expire time
|
2014-12-10 18:40:35 +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
|
|
|
/**
|
2014-05-23 20:56:29 +05:30
|
|
|
* Check authorize parameters
|
2013-03-31 18:07:02 +05:30
|
|
|
*
|
2014-05-23 20:56:29 +05:30
|
|
|
* @return array Authorize request parameters
|
2014-11-12 23:40:29 +05:30
|
|
|
*
|
|
|
|
* @throws
|
2013-03-31 18:07:02 +05:30
|
|
|
*/
|
2014-05-23 20:56:29 +05:30
|
|
|
public function checkAuthorizeParams()
|
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-08-06 14:23:47 +05:30
|
|
|
// Validate client ID and redirect URI
|
2014-11-07 07:50:06 +05:30
|
|
|
$client = $this->server->getClientStorage()->get(
|
2014-08-06 14:23:47 +05:30
|
|
|
$clientId,
|
|
|
|
null,
|
|
|
|
$redirectUri,
|
|
|
|
$this->getIdentifier()
|
|
|
|
);
|
|
|
|
|
|
|
|
if (($client instanceof ClientEntity) === false) {
|
2014-10-01 02:46:26 +05:30
|
|
|
$this->server->getEventEmitter()->emit(new Event\ClientAuthenticationFailedEvent($this->server->getRequest()));
|
2014-08-06 14:23:47 +05:30
|
|
|
throw new Exception\InvalidClientException();
|
|
|
|
}
|
|
|
|
|
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-11-08 22:33:15 +05:30
|
|
|
throw new Exception\InvalidRequestException('state', $redirectUri);
|
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-11-08 22:33:15 +05:30
|
|
|
throw new Exception\InvalidRequestException('response_type', $redirectUri);
|
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-11-08 22:33:15 +05:30
|
|
|
throw new Exception\UnsupportedResponseTypeException($responseType, $redirectUri);
|
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-11-08 22:33:15 +05:30
|
|
|
$scopes = $this->validateScopes($scopeParam, $client, $redirectUri);
|
2014-03-10 01:04:23 +05:30
|
|
|
|
|
|
|
return [
|
2014-12-04 04:51:54 +05:30
|
|
|
'client' => $client,
|
|
|
|
'redirect_uri' => $redirectUri,
|
|
|
|
'state' => $state,
|
|
|
|
'response_type' => $responseType,
|
|
|
|
'scopes' => $scopes
|
2014-03-10 01:04:23 +05:30
|
|
|
];
|
2013-03-31 18:07:02 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-23 20:56:29 +05:30
|
|
|
* Parse a new authorize request
|
2013-03-31 18:07:02 +05:30
|
|
|
*
|
2014-12-10 18:40:35 +05:30
|
|
|
* @param string $type The session owner's type
|
|
|
|
* @param string $typeId The session owner's ID
|
|
|
|
* @param array $authParams The authorize request $_GET parameters
|
|
|
|
*
|
2014-05-03 15:23:43 +05:30
|
|
|
* @return string An authorisation code
|
2013-03-31 18:07:02 +05:30
|
|
|
*/
|
2014-05-23 20:56:29 +05:30
|
|
|
public function newAuthorizeRequest($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-07-11 22:57:03 +05:30
|
|
|
$authCode->setId(SecureKey::generate());
|
2014-04-06 23:44:46 +05:30
|
|
|
$authCode->setRedirectUri($authParams['redirect_uri']);
|
2014-06-23 12:50:34 +05:30
|
|
|
$authCode->setExpireTime(time() + $this->authTokenTTL);
|
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
|
2014-12-10 18:40:35 +05:30
|
|
|
*
|
2014-11-07 07:18:23 +05:30
|
|
|
* @return array
|
2014-12-10 18:40:35 +05:30
|
|
|
*
|
2014-11-12 23:40:29 +05:30
|
|
|
* @throws
|
2013-02-14 01:06:56 +05:30
|
|
|
*/
|
2014-06-23 12:50:34 +05:30
|
|
|
public function completeFlow()
|
2013-02-01 16:20:32 +05:30
|
|
|
{
|
2013-02-14 01:06:56 +05:30
|
|
|
// Get the required params
|
2014-11-24 05:02:50 +05:30
|
|
|
$clientId = $this->server->getRequest()->request->get('client_id', $this->server->getRequest()->getUser());
|
2014-04-06 23:44:46 +05:30
|
|
|
if (is_null($clientId)) {
|
2014-11-24 05:02:50 +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-11-24 05:02:50 +05:30
|
|
|
$clientSecret = $this->server->getRequest()->request->get('client_secret',
|
|
|
|
$this->server->getRequest()->getPassword());
|
2014-04-06 23:44:46 +05:30
|
|
|
if (is_null($clientSecret)) {
|
2014-11-24 05:02:50 +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
|
2014-11-07 07:50:06 +05:30
|
|
|
$client = $this->server->getClientStorage()->get(
|
2014-04-06 23:44:46 +05:30
|
|
|
$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-10-01 02:46:26 +05:30
|
|
|
$this->server->getEventEmitter()->emit(new Event\ClientAuthenticationFailedEvent($this->server->getRequest()));
|
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-11-07 07:50:06 +05:30
|
|
|
$code = $this->server->getAuthCodeStorage()->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-11-08 22:14:39 +05:30
|
|
|
// Ensure the auth code hasn't expired
|
|
|
|
if ($code->isExpired() === true) {
|
|
|
|
throw new Exception\InvalidRequestException('code');
|
|
|
|
}
|
|
|
|
|
2014-05-23 20:56:29 +05:30
|
|
|
// Check redirect URI presented matches redirect URI originally used in authorize request
|
2014-04-06 23:44:46 +05:30
|
|
|
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();
|
2014-11-07 02:20:22 +05:30
|
|
|
$session->associateClient($client);
|
|
|
|
|
2014-04-06 23:44:46 +05:30
|
|
|
$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-07-11 22:57:03 +05:30
|
|
|
$accessToken->setId(SecureKey::generate());
|
2014-09-11 18:09:50 +05:30
|
|
|
$accessToken->setExpireTime($this->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-08-18 21:17:36 +05:30
|
|
|
foreach ($session->getScopes() as $scope) {
|
2014-11-08 23:56:12 +05:30
|
|
|
$accessToken->associateScope($scope);
|
2014-08-18 21:17:36 +05:30
|
|
|
}
|
|
|
|
|
2014-10-01 02:58:49 +05:30
|
|
|
$this->server->getTokenType()->setSession($session);
|
2014-10-01 02:58:38 +05:30
|
|
|
$this->server->getTokenType()->setParam('access_token', $accessToken->getId());
|
|
|
|
$this->server->getTokenType()->setParam('expires_in', $this->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-07-11 22:57:03 +05:30
|
|
|
$refreshToken->setId(SecureKey::generate());
|
2014-04-06 23:44:46 +05:30
|
|
|
$refreshToken->setExpireTime($this->server->getGrantType('refresh_token')->getRefreshTokenTTL() + time());
|
2014-10-01 02:58:38 +05:30
|
|
|
$this->server->getTokenType()->setParam('refresh_token', $refreshToken->getId());
|
2014-04-06 23:44:46 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// Expire the auth code
|
|
|
|
$code->expire();
|
|
|
|
|
|
|
|
// Save all the things
|
|
|
|
$accessToken->setSession($session);
|
|
|
|
$accessToken->save();
|
|
|
|
|
2014-11-13 17:50:59 +05:30
|
|
|
if (isset($refreshToken) && $this->server->hasGrantType('refresh_token')) {
|
2014-04-06 23:44:46 +05:30
|
|
|
$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
|
|
|
}
|