2013-02-01 10:50:32 +00:00
|
|
|
<?php
|
|
|
|
|
2013-05-08 11:42:23 -07:00
|
|
|
namespace League\OAuth2\Server\Grant;
|
2013-05-08 11:06:09 -07:00
|
|
|
|
2015-04-05 21:57:29 +01:00
|
|
|
use DateInterval;
|
2016-04-09 15:25:45 +01:00
|
|
|
use League\OAuth2\Server\Entities\ClientEntityInterface;
|
|
|
|
use League\OAuth2\Server\Entities\UserEntityInterface;
|
2016-02-11 17:30:01 +00:00
|
|
|
use League\OAuth2\Server\Exception\OAuthServerException;
|
2016-02-12 10:01:15 +00:00
|
|
|
use League\OAuth2\Server\Repositories\AuthCodeRepositoryInterface;
|
2016-02-12 13:32:58 +00:00
|
|
|
use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface;
|
2016-03-23 12:54:17 +00:00
|
|
|
use League\OAuth2\Server\RequestEvent;
|
2016-04-10 10:07:08 +01:00
|
|
|
use League\OAuth2\Server\RequestTypes\AuthorizationRequest;
|
2016-03-17 14:37:21 +00:00
|
|
|
use League\OAuth2\Server\ResponseTypes\RedirectResponse;
|
2016-02-11 17:30:01 +00:00
|
|
|
use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface;
|
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
2013-02-01 10:50:32 +00:00
|
|
|
|
2016-03-09 12:32:01 +01:00
|
|
|
class AuthCodeGrant extends AbstractAuthorizeGrant
|
2014-04-06 19:14:46 +01:00
|
|
|
{
|
2016-02-12 10:01:15 +00:00
|
|
|
/**
|
|
|
|
* @var \DateInterval
|
|
|
|
*/
|
|
|
|
private $authCodeTTL;
|
2016-02-12 11:55:41 +00:00
|
|
|
|
2016-02-12 13:32:58 +00:00
|
|
|
/**
|
|
|
|
* @param \League\OAuth2\Server\Repositories\AuthCodeRepositoryInterface $authCodeRepository
|
|
|
|
* @param \League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface $refreshTokenRepository
|
|
|
|
* @param \DateInterval $authCodeTTL
|
2016-02-12 10:01:15 +00:00
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
AuthCodeRepositoryInterface $authCodeRepository,
|
2016-02-12 13:32:58 +00:00
|
|
|
RefreshTokenRepositoryInterface $refreshTokenRepository,
|
2016-04-10 14:22:26 +01:00
|
|
|
\DateInterval $authCodeTTL
|
2015-04-05 21:57:29 +01:00
|
|
|
) {
|
2016-02-18 12:07:23 +00:00
|
|
|
$this->setAuthCodeRepository($authCodeRepository);
|
|
|
|
$this->setRefreshTokenRepository($refreshTokenRepository);
|
2016-02-12 10:01:15 +00:00
|
|
|
$this->authCodeTTL = $authCodeTTL;
|
2016-02-12 14:28:24 +00:00
|
|
|
$this->refreshTokenTTL = new \DateInterval('P1M');
|
2013-05-07 15:20:32 -07:00
|
|
|
}
|
|
|
|
|
2013-03-31 13:37:02 +01:00
|
|
|
/**
|
2016-02-19 18:09:39 -05:00
|
|
|
* Respond to an access token request.
|
2013-03-31 13:37:02 +01:00
|
|
|
*
|
2016-02-11 17:30:01 +00:00
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request
|
|
|
|
* @param \League\OAuth2\Server\ResponseTypes\ResponseTypeInterface $responseType
|
|
|
|
* @param \DateInterval $accessTokenTTL
|
2014-12-10 13:10:35 +00:00
|
|
|
*
|
2016-02-12 13:32:58 +00:00
|
|
|
* @throws \League\OAuth2\Server\Exception\OAuthServerException
|
2016-02-19 18:09:39 -05:00
|
|
|
*
|
|
|
|
* @return \League\OAuth2\Server\ResponseTypes\ResponseTypeInterface
|
2013-03-31 13:37:02 +01:00
|
|
|
*/
|
2016-04-10 11:48:21 +01:00
|
|
|
public function respondToAccessTokenRequest(
|
2016-02-11 17:30:01 +00:00
|
|
|
ServerRequestInterface $request,
|
|
|
|
ResponseTypeInterface $responseType,
|
|
|
|
DateInterval $accessTokenTTL
|
|
|
|
) {
|
2016-02-12 13:01:25 +00:00
|
|
|
// Validate request
|
|
|
|
$client = $this->validateClient($request);
|
2016-02-12 13:32:58 +00:00
|
|
|
$encryptedAuthCode = $this->getRequestParameter('code', $request, null);
|
2016-02-12 13:01:25 +00:00
|
|
|
|
2016-02-12 13:32:58 +00:00
|
|
|
if ($encryptedAuthCode === null) {
|
2016-02-12 13:01:25 +00:00
|
|
|
throw OAuthServerException::invalidRequest('code');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate the authorization code
|
|
|
|
try {
|
2016-03-17 14:37:21 +00:00
|
|
|
$authCodePayload = json_decode($this->decrypt($encryptedAuthCode));
|
2016-02-12 13:01:25 +00:00
|
|
|
if (time() > $authCodePayload->expire_time) {
|
|
|
|
throw OAuthServerException::invalidRequest('code', 'Authorization code has expired');
|
|
|
|
}
|
2016-02-12 13:32:58 +00:00
|
|
|
|
2016-03-15 20:54:59 +01:00
|
|
|
if ($this->authCodeRepository->isAuthCodeRevoked($authCodePayload->auth_code_id) === true) {
|
2016-02-12 13:32:58 +00:00
|
|
|
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 18:13:39 +00:00
|
|
|
|
2016-04-10 11:48:21 +01:00
|
|
|
// 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 18:13:39 +00:00
|
|
|
if ($authCodePayload->redirect_uri !== $redirectUri) {
|
|
|
|
throw OAuthServerException::invalidRequest('redirect_uri', 'Invalid redirect URI');
|
|
|
|
}
|
2016-03-15 01:10:47 +01:00
|
|
|
|
|
|
|
$scopes = [];
|
|
|
|
foreach ($authCodePayload->scopes as $scopeId) {
|
2016-03-24 10:04:15 +00:00
|
|
|
$scope = $this->scopeRepository->getScopeEntityByIdentifier($scopeId);
|
2016-03-15 01:10:47 +01:00
|
|
|
|
2016-04-10 11:48:21 +01:00
|
|
|
if ($scope === false) {
|
2016-03-18 00:25:32 +01:00
|
|
|
// @codeCoverageIgnoreStart
|
2016-03-15 01:10:47 +01:00
|
|
|
throw OAuthServerException::invalidScope($scopeId);
|
2016-03-18 00:25:32 +01:00
|
|
|
// @codeCoverageIgnoreEnd
|
2016-03-15 01:10:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$scopes[] = $scope;
|
|
|
|
}
|
2016-04-10 18:07:18 +02:00
|
|
|
|
|
|
|
// Finalize the requested scopes
|
|
|
|
$scopes = $this->scopeRepository->finalizeScopes($scopes, $this->getIdentifier(), $client, $authCodePayload->user_id);
|
2016-02-12 13:01:25 +00:00
|
|
|
} catch (\LogicException $e) {
|
2016-02-21 16:40:01 +00:00
|
|
|
throw OAuthServerException::invalidRequest('code', 'Cannot decrypt the authorization code');
|
2016-02-12 13:01:25 +00:00
|
|
|
}
|
|
|
|
|
2016-02-12 13:32:58 +00:00
|
|
|
// Issue and persist access + refresh tokens
|
2016-03-15 01:10:47 +01:00
|
|
|
$accessToken = $this->issueAccessToken($accessTokenTTL, $client, $authCodePayload->user_id, $scopes);
|
2016-02-12 13:32:58 +00:00
|
|
|
$refreshToken = $this->issueRefreshToken($accessToken);
|
2016-02-12 13:01:25 +00:00
|
|
|
|
2016-02-12 13:32:58 +00:00
|
|
|
// Inject tokens into response type
|
2016-02-12 13:01:25 +00:00
|
|
|
$responseType->setAccessToken($accessToken);
|
2016-02-12 13:32:58 +00:00
|
|
|
$responseType->setRefreshToken($refreshToken);
|
2016-04-10 12:16:40 -04:00
|
|
|
|
2016-04-10 18:05:16 +02:00
|
|
|
// Revoke used auth code
|
|
|
|
$this->authCodeRepository->revokeAuthCode($authCodePayload->auth_code_id);
|
2014-04-06 19:14:46 +01:00
|
|
|
|
2016-02-12 13:01:25 +00:00
|
|
|
return $responseType;
|
2016-02-11 17:30:01 +00:00
|
|
|
}
|
2013-03-31 13:37:02 +01:00
|
|
|
|
2016-02-12 10:01:15 +00:00
|
|
|
/**
|
2016-02-19 18:09:39 -05:00
|
|
|
* Return the grant identifier that can be used in matching up requests.
|
2016-02-12 10:01:15 +00:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getIdentifier()
|
|
|
|
{
|
|
|
|
return 'authorization_code';
|
|
|
|
}
|
2016-04-10 10:07:08 +01:00
|
|
|
|
|
|
|
/**
|
2016-04-10 06:48:46 -04:00
|
|
|
* {@inheritdoc}
|
2016-04-10 10:07:08 +01:00
|
|
|
*/
|
|
|
|
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 06:48:46 -04:00
|
|
|
* {@inheritdoc}
|
2016-04-10 10:07:08 +01:00
|
|
|
*/
|
|
|
|
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,
|
|
|
|
$this->getIdentifier()
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($client instanceof ClientEntityInterface === false) {
|
|
|
|
$this->getEmitter()->emit(new RequestEvent('client.authentication.failed', $request));
|
|
|
|
throw OAuthServerException::invalidClient();
|
|
|
|
}
|
|
|
|
|
|
|
|
$redirectUri = $this->getQueryStringParameter('redirect_uri', $request);
|
|
|
|
if ($redirectUri !== null) {
|
|
|
|
if (
|
|
|
|
is_string($client->getRedirectUri())
|
|
|
|
&& (strcmp($client->getRedirectUri(), $redirectUri) !== 0)
|
|
|
|
) {
|
|
|
|
$this->getEmitter()->emit(new RequestEvent('client.authentication.failed', $request));
|
|
|
|
throw OAuthServerException::invalidClient();
|
|
|
|
} elseif (
|
|
|
|
is_array($client->getRedirectUri())
|
|
|
|
&& in_array($redirectUri, $client->getRedirectUri()) === false
|
|
|
|
) {
|
|
|
|
$this->getEmitter()->emit(new RequestEvent('client.authentication.failed', $request));
|
|
|
|
throw OAuthServerException::invalidClient();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$scopes = $this->validateScopes(
|
|
|
|
$this->getQueryStringParameter('scope', $request),
|
|
|
|
$client->getRedirectUri()
|
|
|
|
);
|
|
|
|
|
|
|
|
$stateParameter = $this->getQueryStringParameter('state', $request);
|
|
|
|
|
|
|
|
$authorizationRequest = new AuthorizationRequest();
|
|
|
|
$authorizationRequest->setGrantTypeId($this->getIdentifier());
|
|
|
|
$authorizationRequest->setClient($client);
|
|
|
|
$authorizationRequest->setRedirectUri($redirectUri);
|
|
|
|
$authorizationRequest->setState($stateParameter);
|
|
|
|
$authorizationRequest->setScopes($scopes);
|
|
|
|
|
|
|
|
return $authorizationRequest;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-04-10 06:48:46 -04:00
|
|
|
* {@inheritdoc}
|
2016-04-10 10:07:08 +01:00
|
|
|
*/
|
|
|
|
public function completeAuthorizationRequest(AuthorizationRequest $authorizationRequest)
|
|
|
|
{
|
|
|
|
if ($authorizationRequest->getUser() instanceof UserEntityInterface === false) {
|
|
|
|
throw new \LogicException('An instance of UserEntityInterface should be set on the AuthorizationRequest');
|
|
|
|
}
|
|
|
|
|
2016-04-10 11:48:21 +01:00
|
|
|
$finalRedirectUri = ($authorizationRequest->getRedirectUri() === null)
|
|
|
|
? is_array($authorizationRequest->getClient()->getRedirectUri())
|
|
|
|
? $authorizationRequest->getClient()->getRedirectUri()[0]
|
|
|
|
: $authorizationRequest->getClient()->getRedirectUri()
|
|
|
|
: $authorizationRequest->getRedirectUri();
|
|
|
|
|
2016-04-10 10:07:08 +01:00
|
|
|
// 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()
|
|
|
|
);
|
|
|
|
|
|
|
|
$redirectPayload['code'] = $this->encrypt(
|
|
|
|
json_encode(
|
|
|
|
[
|
|
|
|
'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'),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
);
|
2016-04-10 15:58:01 +01:00
|
|
|
$redirectPayload['state'] = $authorizationRequest->getState();
|
2016-04-10 10:07:08 +01:00
|
|
|
|
|
|
|
$response = new RedirectResponse();
|
|
|
|
$response->setRedirectUri(
|
|
|
|
$this->makeRedirectUri(
|
|
|
|
$finalRedirectUri,
|
|
|
|
$redirectPayload
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The user denied the client, redirect them back with an error
|
|
|
|
throw OAuthServerException::accessDenied(
|
|
|
|
'The user denied the request',
|
2016-04-10 11:48:21 +01:00
|
|
|
$finalRedirectUri
|
2016-04-10 10:07:08 +01:00
|
|
|
);
|
|
|
|
}
|
2013-09-07 17:59:44 +01:00
|
|
|
}
|