2013-02-01 16:20:32 +05:30
|
|
|
<?php
|
2016-04-17 17:36:05 +05:30
|
|
|
/**
|
|
|
|
* @author Alex Bilbie <hello@alexbilbie.com>
|
|
|
|
* @copyright Copyright (c) Alex Bilbie
|
|
|
|
* @license http://mit-license.org/
|
|
|
|
*
|
|
|
|
* @link https://github.com/thephpleague/oauth2-server
|
|
|
|
*/
|
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
|
|
|
|
2016-04-09 19:55:45 +05:30
|
|
|
use League\OAuth2\Server\Entities\ClientEntityInterface;
|
2016-04-17 17:20:56 +05:30
|
|
|
use League\OAuth2\Server\Entities\ScopeEntityInterface;
|
2016-04-09 19:55:45 +05:30
|
|
|
use League\OAuth2\Server\Entities\UserEntityInterface;
|
2016-02-11 23:00:01 +05:30
|
|
|
use League\OAuth2\Server\Exception\OAuthServerException;
|
2016-02-12 15:31:15 +05:30
|
|
|
use League\OAuth2\Server\Repositories\AuthCodeRepositoryInterface;
|
2016-02-12 19:02:58 +05:30
|
|
|
use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface;
|
2016-03-23 18:24:17 +05:30
|
|
|
use League\OAuth2\Server\RequestEvent;
|
2016-04-10 14:37:08 +05:30
|
|
|
use League\OAuth2\Server\RequestTypes\AuthorizationRequest;
|
2016-03-17 20:07:21 +05:30
|
|
|
use League\OAuth2\Server\ResponseTypes\RedirectResponse;
|
2016-02-11 23:00:01 +05:30
|
|
|
use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface;
|
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
2013-02-01 16:20:32 +05:30
|
|
|
|
2016-03-09 17:02:01 +05:30
|
|
|
class AuthCodeGrant extends AbstractAuthorizeGrant
|
2014-04-06 23:44:46 +05:30
|
|
|
{
|
2016-02-12 15:31:15 +05:30
|
|
|
/**
|
|
|
|
* @var \DateInterval
|
|
|
|
*/
|
|
|
|
private $authCodeTTL;
|
2016-02-12 17:25:41 +05:30
|
|
|
|
2016-05-06 19:53:16 +05:30
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private $enableCodeExchangeProof = false;
|
|
|
|
|
2016-02-12 19:02:58 +05:30
|
|
|
/**
|
2016-07-09 04:30:44 +05:30
|
|
|
* @param AuthCodeRepositoryInterface $authCodeRepository
|
|
|
|
* @param RefreshTokenRepositoryInterface $refreshTokenRepository
|
|
|
|
* @param \DateInterval $authCodeTTL
|
2016-02-12 15:31:15 +05:30
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
AuthCodeRepositoryInterface $authCodeRepository,
|
2016-02-12 19:02:58 +05:30
|
|
|
RefreshTokenRepositoryInterface $refreshTokenRepository,
|
2016-04-10 18:52:26 +05:30
|
|
|
\DateInterval $authCodeTTL
|
2015-04-06 02:27:29 +05:30
|
|
|
) {
|
2016-02-18 17:37:23 +05:30
|
|
|
$this->setAuthCodeRepository($authCodeRepository);
|
|
|
|
$this->setRefreshTokenRepository($refreshTokenRepository);
|
2016-02-12 15:31:15 +05:30
|
|
|
$this->authCodeTTL = $authCodeTTL;
|
2016-02-12 19:58:24 +05:30
|
|
|
$this->refreshTokenTTL = new \DateInterval('P1M');
|
2013-05-08 03:50:32 +05:30
|
|
|
}
|
|
|
|
|
2016-05-06 19:53:16 +05:30
|
|
|
public function enableCodeExchangeProof()
|
|
|
|
{
|
|
|
|
$this->enableCodeExchangeProof = true;
|
|
|
|
}
|
|
|
|
|
2013-03-31 18:07:02 +05:30
|
|
|
/**
|
2016-02-20 04:39:39 +05:30
|
|
|
* Respond to an access token request.
|
2013-03-31 18:07:02 +05:30
|
|
|
*
|
2016-07-09 04:30:44 +05:30
|
|
|
* @param ServerRequestInterface $request
|
|
|
|
* @param ResponseTypeInterface $responseType
|
|
|
|
* @param \DateInterval $accessTokenTTL
|
2014-12-10 18:40:35 +05:30
|
|
|
*
|
2016-07-09 04:30:44 +05:30
|
|
|
* @throws OAuthServerException
|
2016-02-20 04:39:39 +05:30
|
|
|
*
|
2016-07-09 04:30:44 +05:30
|
|
|
* @return ResponseTypeInterface
|
2013-03-31 18:07:02 +05:30
|
|
|
*/
|
2016-04-10 16:18:21 +05:30
|
|
|
public function respondToAccessTokenRequest(
|
2016-02-11 23:00:01 +05:30
|
|
|
ServerRequestInterface $request,
|
|
|
|
ResponseTypeInterface $responseType,
|
2016-07-09 04:30:44 +05:30
|
|
|
\DateInterval $accessTokenTTL
|
2016-02-11 23:00:01 +05:30
|
|
|
) {
|
2016-02-12 18:31:25 +05:30
|
|
|
// Validate request
|
|
|
|
$client = $this->validateClient($request);
|
2016-02-12 19:02:58 +05:30
|
|
|
$encryptedAuthCode = $this->getRequestParameter('code', $request, null);
|
2016-02-12 18:31:25 +05:30
|
|
|
|
2016-02-12 19:02:58 +05:30
|
|
|
if ($encryptedAuthCode === null) {
|
2016-02-12 18:31:25 +05:30
|
|
|
throw OAuthServerException::invalidRequest('code');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate the authorization code
|
|
|
|
try {
|
2016-03-17 20:07:21 +05:30
|
|
|
$authCodePayload = json_decode($this->decrypt($encryptedAuthCode));
|
2016-02-12 18:31:25 +05:30
|
|
|
if (time() > $authCodePayload->expire_time) {
|
|
|
|
throw OAuthServerException::invalidRequest('code', 'Authorization code has expired');
|
|
|
|
}
|
2016-02-12 19:02:58 +05:30
|
|
|
|
2016-03-16 01:24:59 +05:30
|
|
|
if ($this->authCodeRepository->isAuthCodeRevoked($authCodePayload->auth_code_id) === true) {
|
2016-02-12 19:02:58 +05:30
|
|
|
throw OAuthServerException::invalidRequest('code', 'Authorization code has been revoked');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($authCodePayload->client_id !== $client->getIdentifier()) {
|
|
|
|
throw OAuthServerException::invalidRequest('code', 'Authorization code was not issued to this client');
|
|
|
|
}
|
2016-02-21 23:43:39 +05:30
|
|
|
|
2016-04-10 16:18:21 +05:30
|
|
|
// The redirect URI is required in this request
|
|
|
|
$redirectUri = $this->getRequestParameter('redirect_uri', $request, null);
|
|
|
|
if (empty($authCodePayload->redirect_uri) === false && $redirectUri === null) {
|
|
|
|
throw OAuthServerException::invalidRequest('redirect_uri');
|
|
|
|
}
|
|
|
|
|
2016-02-21 23:43:39 +05:30
|
|
|
if ($authCodePayload->redirect_uri !== $redirectUri) {
|
|
|
|
throw OAuthServerException::invalidRequest('redirect_uri', 'Invalid redirect URI');
|
|
|
|
}
|
2016-03-15 05:40:47 +05:30
|
|
|
|
|
|
|
$scopes = [];
|
|
|
|
foreach ($authCodePayload->scopes as $scopeId) {
|
2016-03-24 15:34:15 +05:30
|
|
|
$scope = $this->scopeRepository->getScopeEntityByIdentifier($scopeId);
|
2016-03-15 05:40:47 +05:30
|
|
|
|
2016-07-09 15:39:21 +05:30
|
|
|
if ($scope instanceof ScopeEntityInterface === false) {
|
2016-03-18 04:55:32 +05:30
|
|
|
// @codeCoverageIgnoreStart
|
2016-03-15 05:40:47 +05:30
|
|
|
throw OAuthServerException::invalidScope($scopeId);
|
2016-03-18 04:55:32 +05:30
|
|
|
// @codeCoverageIgnoreEnd
|
2016-03-15 05:40:47 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
$scopes[] = $scope;
|
|
|
|
}
|
2016-04-10 21:37:18 +05:30
|
|
|
|
|
|
|
// Finalize the requested scopes
|
2016-04-18 16:42:06 +05:30
|
|
|
$scopes = $this->scopeRepository->finalizeScopes(
|
|
|
|
$scopes,
|
|
|
|
$this->getIdentifier(),
|
|
|
|
$client,
|
|
|
|
$authCodePayload->user_id
|
|
|
|
);
|
2016-02-12 18:31:25 +05:30
|
|
|
} catch (\LogicException $e) {
|
2016-02-21 22:10:01 +05:30
|
|
|
throw OAuthServerException::invalidRequest('code', 'Cannot decrypt the authorization code');
|
2016-02-12 18:31:25 +05:30
|
|
|
}
|
|
|
|
|
2016-05-06 19:53:16 +05:30
|
|
|
// Validate code challenge
|
|
|
|
if ($this->enableCodeExchangeProof === true) {
|
|
|
|
$codeVerifier = $this->getRequestParameter('code_verifier', $request, null);
|
|
|
|
if ($codeVerifier === null) {
|
|
|
|
throw OAuthServerException::invalidRequest('code_verifier');
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ($authCodePayload->code_challenge_method) {
|
|
|
|
case 'plain':
|
|
|
|
if (hash_equals($codeVerifier, $authCodePayload->code_challenge) === false) {
|
|
|
|
throw OAuthServerException::invalidGrant('Failed to verify `code_verifier`.');
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
case 'S256':
|
|
|
|
if (
|
|
|
|
hash_equals(
|
|
|
|
urlencode(base64_encode(hash('sha256', $codeVerifier))),
|
|
|
|
$authCodePayload->code_challenge
|
|
|
|
) === false
|
|
|
|
) {
|
|
|
|
throw OAuthServerException::invalidGrant('Failed to verify `code_verifier`.');
|
|
|
|
}
|
|
|
|
// @codeCoverageIgnoreStart
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw OAuthServerException::serverError(
|
|
|
|
sprintf(
|
|
|
|
'Unsupported code challenge method `%s`',
|
|
|
|
$authCodePayload->code_challenge_method
|
|
|
|
)
|
|
|
|
);
|
|
|
|
// @codeCoverageIgnoreEnd
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-12 19:02:58 +05:30
|
|
|
// Issue and persist access + refresh tokens
|
2016-03-15 05:40:47 +05:30
|
|
|
$accessToken = $this->issueAccessToken($accessTokenTTL, $client, $authCodePayload->user_id, $scopes);
|
2016-02-12 19:02:58 +05:30
|
|
|
$refreshToken = $this->issueRefreshToken($accessToken);
|
2016-02-12 18:31:25 +05:30
|
|
|
|
2016-02-12 19:02:58 +05:30
|
|
|
// Inject tokens into response type
|
2016-02-12 18:31:25 +05:30
|
|
|
$responseType->setAccessToken($accessToken);
|
2016-02-12 19:02:58 +05:30
|
|
|
$responseType->setRefreshToken($refreshToken);
|
2016-04-10 21:46:40 +05:30
|
|
|
|
2016-04-10 21:35:16 +05:30
|
|
|
// Revoke used auth code
|
|
|
|
$this->authCodeRepository->revokeAuthCode($authCodePayload->auth_code_id);
|
2014-04-06 23:44:46 +05:30
|
|
|
|
2016-02-12 18:31:25 +05:30
|
|
|
return $responseType;
|
2016-02-11 23:00:01 +05:30
|
|
|
}
|
2013-03-31 18:07:02 +05:30
|
|
|
|
2016-02-12 15:31:15 +05:30
|
|
|
/**
|
2016-02-20 04:39:39 +05:30
|
|
|
* Return the grant identifier that can be used in matching up requests.
|
2016-02-12 15:31:15 +05:30
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getIdentifier()
|
|
|
|
{
|
|
|
|
return 'authorization_code';
|
|
|
|
}
|
2016-04-10 14:37:08 +05:30
|
|
|
|
|
|
|
/**
|
2016-04-10 16:18:46 +05:30
|
|
|
* {@inheritdoc}
|
2016-04-10 14:37:08 +05:30
|
|
|
*/
|
|
|
|
public function canRespondToAuthorizationRequest(ServerRequestInterface $request)
|
|
|
|
{
|
|
|
|
return (
|
|
|
|
array_key_exists('response_type', $request->getQueryParams())
|
|
|
|
&& $request->getQueryParams()['response_type'] === 'code'
|
|
|
|
&& isset($request->getQueryParams()['client_id'])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-04-10 16:18:46 +05:30
|
|
|
* {@inheritdoc}
|
2016-04-10 14:37:08 +05:30
|
|
|
*/
|
|
|
|
public function validateAuthorizationRequest(ServerRequestInterface $request)
|
|
|
|
{
|
|
|
|
$clientId = $this->getQueryStringParameter(
|
|
|
|
'client_id',
|
|
|
|
$request,
|
|
|
|
$this->getServerParameter('PHP_AUTH_USER', $request)
|
|
|
|
);
|
|
|
|
if (is_null($clientId)) {
|
|
|
|
throw OAuthServerException::invalidRequest('client_id');
|
|
|
|
}
|
|
|
|
|
|
|
|
$client = $this->clientRepository->getClientEntity(
|
|
|
|
$clientId,
|
2016-04-18 13:02:49 +05:30
|
|
|
$this->getIdentifier(),
|
|
|
|
null,
|
|
|
|
false
|
2016-04-10 14:37:08 +05:30
|
|
|
);
|
|
|
|
|
2016-07-09 15:39:21 +05:30
|
|
|
if ($client instanceof ClientEntityInterface === false) {
|
2016-05-04 13:25:57 +05:30
|
|
|
$this->getEmitter()->emit(new RequestEvent(RequestEvent::CLIENT_AUTHENTICATION_FAILED, $request));
|
2016-04-10 14:37:08 +05:30
|
|
|
throw OAuthServerException::invalidClient();
|
|
|
|
}
|
|
|
|
|
|
|
|
$redirectUri = $this->getQueryStringParameter('redirect_uri', $request);
|
|
|
|
if ($redirectUri !== null) {
|
|
|
|
if (
|
|
|
|
is_string($client->getRedirectUri())
|
|
|
|
&& (strcmp($client->getRedirectUri(), $redirectUri) !== 0)
|
|
|
|
) {
|
2016-05-04 13:25:57 +05:30
|
|
|
$this->getEmitter()->emit(new RequestEvent(RequestEvent::CLIENT_AUTHENTICATION_FAILED, $request));
|
2016-04-10 14:37:08 +05:30
|
|
|
throw OAuthServerException::invalidClient();
|
|
|
|
} elseif (
|
|
|
|
is_array($client->getRedirectUri())
|
|
|
|
&& in_array($redirectUri, $client->getRedirectUri()) === false
|
|
|
|
) {
|
2016-05-04 13:25:57 +05:30
|
|
|
$this->getEmitter()->emit(new RequestEvent(RequestEvent::CLIENT_AUTHENTICATION_FAILED, $request));
|
2016-04-10 14:37:08 +05:30
|
|
|
throw OAuthServerException::invalidClient();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$scopes = $this->validateScopes(
|
2017-10-19 02:38:41 +05:30
|
|
|
$this->getQueryStringParameter('scope', $request, $this->defaultScope),
|
2016-04-18 16:42:36 +05:30
|
|
|
is_array($client->getRedirectUri())
|
|
|
|
? $client->getRedirectUri()[0]
|
|
|
|
: $client->getRedirectUri()
|
2016-04-10 14:37:08 +05:30
|
|
|
);
|
|
|
|
|
|
|
|
$stateParameter = $this->getQueryStringParameter('state', $request);
|
|
|
|
|
|
|
|
$authorizationRequest = new AuthorizationRequest();
|
|
|
|
$authorizationRequest->setGrantTypeId($this->getIdentifier());
|
|
|
|
$authorizationRequest->setClient($client);
|
|
|
|
$authorizationRequest->setRedirectUri($redirectUri);
|
|
|
|
$authorizationRequest->setState($stateParameter);
|
|
|
|
$authorizationRequest->setScopes($scopes);
|
|
|
|
|
2016-05-06 19:53:16 +05:30
|
|
|
if ($this->enableCodeExchangeProof === true) {
|
|
|
|
$codeChallenge = $this->getQueryStringParameter('code_challenge', $request);
|
|
|
|
if ($codeChallenge === null) {
|
|
|
|
throw OAuthServerException::invalidRequest('code_challenge');
|
|
|
|
}
|
|
|
|
|
2017-07-01 21:49:23 +05:30
|
|
|
if (preg_match('/^[A-Za-z0-9-._~]{43,128}$/', $codeChallenge) !== 1) {
|
2017-06-16 21:22:36 +05:30
|
|
|
throw OAuthServerException::invalidRequest(
|
|
|
|
'code_challenge',
|
|
|
|
'The code_challenge must be between 43 and 128 characters'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-05-06 19:53:16 +05:30
|
|
|
$codeChallengeMethod = $this->getQueryStringParameter('code_challenge_method', $request, 'plain');
|
|
|
|
if (in_array($codeChallengeMethod, ['plain', 'S256']) === false) {
|
|
|
|
throw OAuthServerException::invalidRequest(
|
|
|
|
'code_challenge_method',
|
|
|
|
'Code challenge method must be `plain` or `S256`'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$authorizationRequest->setCodeChallenge($codeChallenge);
|
|
|
|
$authorizationRequest->setCodeChallengeMethod($codeChallengeMethod);
|
|
|
|
}
|
|
|
|
|
2016-04-10 14:37:08 +05:30
|
|
|
return $authorizationRequest;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-04-10 16:18:46 +05:30
|
|
|
* {@inheritdoc}
|
2016-04-10 14:37:08 +05:30
|
|
|
*/
|
|
|
|
public function completeAuthorizationRequest(AuthorizationRequest $authorizationRequest)
|
|
|
|
{
|
2016-07-09 15:39:21 +05:30
|
|
|
if ($authorizationRequest->getUser() instanceof UserEntityInterface === false) {
|
2016-04-10 14:37:08 +05:30
|
|
|
throw new \LogicException('An instance of UserEntityInterface should be set on the AuthorizationRequest');
|
|
|
|
}
|
|
|
|
|
2016-04-10 16:18:21 +05:30
|
|
|
$finalRedirectUri = ($authorizationRequest->getRedirectUri() === null)
|
|
|
|
? is_array($authorizationRequest->getClient()->getRedirectUri())
|
|
|
|
? $authorizationRequest->getClient()->getRedirectUri()[0]
|
|
|
|
: $authorizationRequest->getClient()->getRedirectUri()
|
|
|
|
: $authorizationRequest->getRedirectUri();
|
|
|
|
|
2016-04-10 14:37:08 +05:30
|
|
|
// The user approved the client, redirect them back with an auth code
|
|
|
|
if ($authorizationRequest->isAuthorizationApproved() === true) {
|
|
|
|
$authCode = $this->issueAuthCode(
|
|
|
|
$this->authCodeTTL,
|
|
|
|
$authorizationRequest->getClient(),
|
|
|
|
$authorizationRequest->getUser()->getIdentifier(),
|
|
|
|
$authorizationRequest->getRedirectUri(),
|
|
|
|
$authorizationRequest->getScopes()
|
|
|
|
);
|
|
|
|
|
2017-07-01 20:00:21 +05:30
|
|
|
$payload = [
|
2017-08-02 21:25:11 +05:30
|
|
|
'client_id' => $authCode->getClient()->getIdentifier(),
|
|
|
|
'redirect_uri' => $authCode->getRedirectUri(),
|
|
|
|
'auth_code_id' => $authCode->getIdentifier(),
|
|
|
|
'scopes' => $authCode->getScopes(),
|
|
|
|
'user_id' => $authCode->getUserIdentifier(),
|
|
|
|
'expire_time' => (new \DateTime())->add($this->authCodeTTL)->format('U'),
|
|
|
|
'code_challenge' => $authorizationRequest->getCodeChallenge(),
|
|
|
|
'code_challenge_method' => $authorizationRequest->getCodeChallengeMethod(),
|
2017-07-01 20:00:21 +05:30
|
|
|
];
|
|
|
|
|
2016-04-10 14:37:08 +05:30
|
|
|
$response = new RedirectResponse();
|
|
|
|
$response->setRedirectUri(
|
|
|
|
$this->makeRedirectUri(
|
|
|
|
$finalRedirectUri,
|
2016-04-18 16:42:06 +05:30
|
|
|
[
|
|
|
|
'code' => $this->encrypt(
|
|
|
|
json_encode(
|
2017-07-01 22:52:51 +05:30
|
|
|
$payload
|
2016-04-18 16:42:06 +05:30
|
|
|
)
|
|
|
|
),
|
|
|
|
'state' => $authorizationRequest->getState(),
|
|
|
|
]
|
2016-04-10 14:37:08 +05:30
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The user denied the client, redirect them back with an error
|
|
|
|
throw OAuthServerException::accessDenied(
|
|
|
|
'The user denied the request',
|
2016-06-28 13:33:01 +05:30
|
|
|
$this->makeRedirectUri(
|
|
|
|
$finalRedirectUri,
|
|
|
|
[
|
|
|
|
'state' => $authorizationRequest->getState(),
|
|
|
|
]
|
|
|
|
)
|
2016-04-10 14:37:08 +05:30
|
|
|
);
|
|
|
|
}
|
2013-09-07 22:29:44 +05:30
|
|
|
}
|