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
|
|
|
|
2015-04-06 02:27:29 +05:30
|
|
|
use League\Event\Emitter;
|
|
|
|
use League\Event\Event;
|
|
|
|
use League\OAuth2\Server\Entities\AccessTokenEntity;
|
|
|
|
use League\OAuth2\Server\Entities\Interfaces\AuthCodeEntityInterface;
|
|
|
|
use League\OAuth2\Server\Entities\Interfaces\ClientEntityInterface;
|
|
|
|
use League\OAuth2\Server\Exception\InvalidClientException;
|
|
|
|
use League\OAuth2\Server\Exception\InvalidRequestException;
|
|
|
|
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
|
|
|
|
use League\OAuth2\Server\Repositories\AuthCodeRepositoryInterface;
|
|
|
|
use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
|
|
|
|
use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface;
|
|
|
|
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
|
|
|
|
use League\OAuth2\Server\TokenTypes\TokenTypeInterface;
|
|
|
|
use League\OAuth2\Server\Utils\SecureKey;
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
use DateInterval;
|
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
|
|
|
{
|
2015-04-06 02:27:29 +05:30
|
|
|
/**
|
|
|
|
* @var \League\OAuth2\Server\Repositories\AuthCodeRepositoryInterface
|
|
|
|
*/
|
|
|
|
protected $authCodeRepository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param \League\Event\Emitter $emitter
|
|
|
|
* @param \League\OAuth2\Server\Repositories\ClientRepositoryInterface $clientRepository
|
|
|
|
* @param \League\OAuth2\Server\Repositories\ScopeRepositoryInterface $scopeRepository
|
|
|
|
* @param \League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface $accessTokenRepository
|
|
|
|
* @param \League\OAuth2\Server\Repositories\AuthCodeRepositoryInterface $authCodeRepository
|
|
|
|
* @param \League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface $refreshTokenRepository
|
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
Emitter $emitter,
|
|
|
|
ClientRepositoryInterface $clientRepository,
|
|
|
|
ScopeRepositoryInterface $scopeRepository,
|
|
|
|
AccessTokenRepositoryInterface $accessTokenRepository,
|
|
|
|
AuthCodeRepositoryInterface $authCodeRepository,
|
|
|
|
RefreshTokenRepositoryInterface $refreshTokenRepository = null
|
|
|
|
) {
|
|
|
|
$this->authCodeRepository = $authCodeRepository;
|
|
|
|
$this->refreshTokenRepository = $refreshTokenRepository;
|
|
|
|
parent::__construct($emitter, $clientRepository, $scopeRepository, $accessTokenRepository);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
2015-04-06 02:27: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
|
2015-04-06 02:27:29 +05:30
|
|
|
$clientId = $request->query->get('client_id', null);
|
2014-03-10 01:04:23 +05:30
|
|
|
if (is_null($clientId)) {
|
2015-04-06 02:27:29 +05:30
|
|
|
throw new InvalidRequestException('client_id');
|
2013-03-31 18:07:02 +05:30
|
|
|
}
|
|
|
|
|
2015-04-06 02:27:29 +05:30
|
|
|
$redirectUri = $request->query->get('redirect_uri', null);
|
2014-03-10 01:04:23 +05:30
|
|
|
if (is_null($redirectUri)) {
|
2015-04-06 02:27:29 +05:30
|
|
|
throw new 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) {
|
2015-04-06 02:27:29 +05:30
|
|
|
$this->server->getEventEmitter()->emit(new Event\ClientAuthenticationFailedEvent($request));
|
2014-08-06 14:23:47 +05:30
|
|
|
throw new Exception\InvalidClientException();
|
|
|
|
}
|
|
|
|
|
2015-04-06 02:27:29 +05:30
|
|
|
$state = $request->query->get('state', null);
|
2014-03-10 01:04:23 +05:30
|
|
|
if ($this->server->stateParamRequired() === true && is_null($state)) {
|
2015-04-06 02:27:29 +05:30
|
|
|
throw new InvalidRequestException('state', $redirectUri);
|
2013-03-31 18:07:02 +05:30
|
|
|
}
|
|
|
|
|
2015-04-06 02:27:29 +05:30
|
|
|
$responseType = $request->query->get('response_type', null);
|
2014-03-10 01:04:23 +05:30
|
|
|
if (is_null($responseType)) {
|
2015-04-06 02:27:29 +05:30
|
|
|
throw new 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
|
2015-04-06 02:27:29 +05:30
|
|
|
$scopeParam = $request->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
|
|
|
];
|
2015-04-06 02:27:29 +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
|
|
|
*/
|
2015-04-06 02:27: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']);
|
2015-04-06 02:27:29 +05:30
|
|
|
}*/
|
2013-03-31 18:07:02 +05:30
|
|
|
|
|
|
|
/**
|
2015-04-06 02:27:29 +05:30
|
|
|
* Return an access token
|
2014-12-10 18:40:35 +05:30
|
|
|
*
|
2015-04-06 02:27:29 +05:30
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request
|
|
|
|
* @param \League\OAuth2\Server\TokenTypes\TokenTypeInterface $tokenType
|
|
|
|
* @param \DateInterval $accessTokenTTL
|
|
|
|
* @param string $scopeDelimiter
|
2014-12-10 18:40:35 +05:30
|
|
|
*
|
2015-04-06 02:27:29 +05:30
|
|
|
* @return \League\OAuth2\Server\TokenTypes\TokenTypeInterface
|
|
|
|
* @throws \League\OAuth2\Server\Exception\InvalidClientException
|
|
|
|
* @throws \League\OAuth2\Server\Exception\InvalidGrantException
|
|
|
|
* @throws \League\OAuth2\Server\Exception\InvalidRequestException
|
2013-02-14 01:06:56 +05:30
|
|
|
*/
|
2015-04-06 02:27:29 +05:30
|
|
|
public function getAccessTokenAsType(
|
|
|
|
Request $request,
|
|
|
|
TokenTypeInterface $tokenType,
|
|
|
|
DateInterval $accessTokenTTL,
|
|
|
|
$scopeDelimiter = ' '
|
|
|
|
) {
|
2013-02-14 01:06:56 +05:30
|
|
|
// Get the required params
|
2015-04-06 02:27:29 +05:30
|
|
|
$clientId = $request->request->get('client_id', $request->getUser());
|
2014-04-06 23:44:46 +05:30
|
|
|
if (is_null($clientId)) {
|
2015-10-14 14:21:53 +05:30
|
|
|
throw new InvalidRequestException('client_id', '');
|
2013-02-01 20:11:52 +05:30
|
|
|
}
|
2013-02-01 16:20:32 +05:30
|
|
|
|
2015-04-06 02:27:29 +05:30
|
|
|
$clientSecret = $request->request->get('client_secret',
|
|
|
|
$request->getPassword());
|
2014-04-06 23:44:46 +05:30
|
|
|
if (is_null($clientSecret)) {
|
2015-04-06 02:27:29 +05:30
|
|
|
throw new InvalidRequestException('client_secret');
|
2013-02-01 20:11:52 +05:30
|
|
|
}
|
2013-02-01 16:20:32 +05:30
|
|
|
|
2015-04-06 02:27:29 +05:30
|
|
|
$redirectUri = $request->request->get('redirect_uri', null);
|
2014-04-06 23:44:46 +05:30
|
|
|
if (is_null($redirectUri)) {
|
2015-04-06 02:27:29 +05:30
|
|
|
throw new 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
|
2015-04-06 02:27:29 +05:30
|
|
|
$client = $this->clientRepository->get(
|
2014-04-06 23:44:46 +05:30
|
|
|
$clientId,
|
|
|
|
$clientSecret,
|
|
|
|
$redirectUri,
|
|
|
|
$this->getIdentifier()
|
|
|
|
);
|
2013-02-01 16:20:32 +05:30
|
|
|
|
2015-04-06 02:27:29 +05:30
|
|
|
if (($client instanceof ClientEntityInterface) === false) {
|
|
|
|
$this->emitter->emit(new Event('client.authentication.failed', $request));
|
|
|
|
throw new InvalidClientException();
|
2013-02-01 16:20:32 +05:30
|
|
|
}
|
|
|
|
|
2014-04-06 23:44:46 +05:30
|
|
|
// Validate the auth code
|
2015-04-06 02:27:29 +05:30
|
|
|
$authCode = $request->request->get('code', null);
|
2014-04-06 23:44:46 +05:30
|
|
|
if (is_null($authCode)) {
|
2015-04-06 02:27:29 +05:30
|
|
|
throw new InvalidRequestException('code');
|
2013-02-01 20:11:52 +05:30
|
|
|
}
|
2013-02-01 16:20:32 +05:30
|
|
|
|
2015-04-06 02:27:29 +05:30
|
|
|
$code = $this->authCodeRepository->get($authCode);
|
|
|
|
if (($code instanceof AuthCodeEntityInterface) === false) {
|
|
|
|
throw new 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) {
|
2015-04-06 02:27:29 +05:30
|
|
|
throw new InvalidRequestException('code');
|
2014-11-08 22:14:39 +05:30
|
|
|
}
|
|
|
|
|
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) {
|
2015-04-06 02:27:29 +05:30
|
|
|
throw new 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
|
|
|
// Generate the access token
|
2014-05-02 21:51:53 +05:30
|
|
|
$accessToken = new AccessTokenEntity($this->server);
|
2015-04-06 02:27:29 +05:30
|
|
|
$accessToken->setIdentifier(SecureKey::generate());
|
2015-04-06 12:43:41 +05:30
|
|
|
$expirationDateTime = (new \DateTime())->add($accessTokenTTL);
|
|
|
|
$accessToken->setExpiryDateTime($expirationDateTime);
|
2015-04-06 02:27:29 +05:30
|
|
|
$accessToken->setClient($client);
|
2013-02-01 16:20:32 +05:30
|
|
|
|
2015-04-06 02:27:29 +05:30
|
|
|
foreach ($code->getScopes() as $scope) {
|
|
|
|
$accessToken->addScope($scope);
|
2014-08-18 21:17:36 +05:30
|
|
|
}
|
|
|
|
|
2015-04-06 02:27:29 +05:30
|
|
|
$tokenType->setAccessToken($accessToken);
|
2013-02-01 16:20:32 +05:30
|
|
|
|
2013-04-29 16:01:07 +05:30
|
|
|
// Associate a refresh token if set
|
2015-04-06 02:27:29 +05:30
|
|
|
if ($this->refreshTokenRepository instanceof RefreshTokenRepositoryInterface) {
|
|
|
|
// $refreshToken = new RefreshTokenEntity($this->server);
|
|
|
|
// $refreshToken->setId(SecureKey::generate());
|
|
|
|
// $refreshToken->setExpireTime($this->server->getGrantType('refresh_token')->getRefreshTokenTTL() + time());
|
|
|
|
// $tokenType->setParam('refresh_token', $refreshToken->getId());
|
|
|
|
// $refreshToken->setAccessToken($accessToken);
|
2014-04-06 23:44:46 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// Expire the auth code
|
2015-04-06 02:27:29 +05:30
|
|
|
$this->authCodeRepository->delete($code);
|
2014-04-06 23:44:46 +05:30
|
|
|
|
2015-04-06 02:27:29 +05:30
|
|
|
// Save the access token
|
|
|
|
$this->accessTokenRepository->create($accessToken);
|
2013-02-01 16:20:32 +05:30
|
|
|
|
2015-04-06 02:27:29 +05:30
|
|
|
return $tokenType;
|
2013-02-01 16:20:32 +05:30
|
|
|
}
|
2013-09-07 22:29:44 +05:30
|
|
|
}
|