oauth2-server/src/Grant/AuthCodeGrant.php

219 lines
7.7 KiB
PHP
Raw Normal View History

2013-02-01 16:20:32 +05:30
<?php
namespace League\OAuth2\Server\Grant;
2013-05-08 23:36:09 +05:30
use DateInterval;
2016-02-11 23:00:01 +05:30
use League\OAuth2\Server\Exception\OAuthServerException;
use League\OAuth2\Server\Repositories\AuthCodeRepositoryInterface;
use League\OAuth2\Server\ResponseTypes\AuthorizeClientResponseTypeInterface;
use League\OAuth2\Server\ResponseTypes\LoginUserResponseTypeInterface;
2016-02-11 23:00:01 +05:30
use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface;
use League\OAuth2\Server\Utils\KeyCrypt;
2016-02-11 23:00:01 +05:30
use Psr\Http\Message\ServerRequestInterface;
use Zend\Diactoros\Response;
use Zend\Diactoros\Uri;
2013-02-01 16:20:32 +05:30
2014-05-02 21:54:55 +05:30
class AuthCodeGrant extends AbstractGrant
2014-04-06 23:44:46 +05:30
{
/**
* @var \League\OAuth2\Server\ResponseTypes\LoginUserResponseTypeInterface
*/
private $loginUserResponseType;
/**
* @var \DateInterval
*/
private $authCodeTTL;
/**
* @var \League\OAuth2\Server\Repositories\AuthCodeRepositoryInterface
*/
private $authCodeRepository;
/**
* @var \League\OAuth2\Server\ResponseTypes\AuthorizeClientResponseTypeInterface
*/
private $authorizeClientResponseType;
/**
* @param \League\OAuth2\Server\Repositories\AuthCodeRepositoryInterface $authCodeRepository
* @param \DateInterval $authCodeTTL
* @param \League\OAuth2\Server\ResponseTypes\LoginUserResponseTypeInterface $loginUserResponseType
* @param \League\OAuth2\Server\ResponseTypes\AuthorizeClientResponseTypeInterface $authorizeClientResponseType
*/
public function __construct(
AuthCodeRepositoryInterface $authCodeRepository,
\DateInterval $authCodeTTL,
LoginUserResponseTypeInterface $loginUserResponseType,
AuthorizeClientResponseTypeInterface $authorizeClientResponseType
) {
$this->authCodeRepository = $authCodeRepository;
$this->authCodeTTL = $authCodeTTL;
$this->loginUserResponseType = $loginUserResponseType;
$this->authorizeClientResponseType = $authorizeClientResponseType;
2013-05-08 03:50:32 +05:30
}
2013-02-14 01:06:56 +05:30
/**
2016-02-11 23:00:01 +05:30
* Respond to an authorization request
2014-11-12 23:40:29 +05:30
*
2016-02-11 23:00:01 +05:30
* @param \Psr\Http\Message\ServerRequestInterface $request
*
* @return \Psr\Http\Message\ResponseInterface
* @throws \League\OAuth2\Server\Exception\OAuthServerException
*/
2016-02-11 23:00:01 +05:30
protected function respondToAuthorizationRequest(
ServerRequestInterface $request
2016-02-11 23:00:01 +05:30
) {
$clientId = $this->getQueryStringParameter(
'client_id',
$request,
$this->getServerParameter('PHP_AUTH_USER', $request)
);
2014-03-10 01:04:23 +05:30
if (is_null($clientId)) {
2016-02-11 23:00:01 +05:30
throw OAuthServerException::invalidRequest('client_id', null, '`%s` parameter is missing');
}
$redirectUri = $this->getQueryStringParameter('redirect_uri', $request, null);
2014-03-10 01:04:23 +05:30
if (is_null($redirectUri)) {
2016-02-11 23:00:01 +05:30
throw OAuthServerException::invalidRequest('redirect_uri', null, '`%s` parameter is missing');
}
2016-02-11 23:00:01 +05:30
$client = $this->clientRepository->getClientEntity(
2014-08-06 14:23:47 +05:30
$clientId,
null,
$redirectUri,
$this->getIdentifier()
2014-08-06 14:23:47 +05:30
);
if (!$client instanceof ClientEntityInterface) {
$this->emitter->emit(new Event('client.authentication.failed', $request));
2016-02-11 23:00:01 +05:30
throw OAuthServerException::invalidClient();
}
$scopes = $this->validateScopes($request, $client, $redirectUri);
$queryString = http_build_query($request->getQueryParams());
// Check if the user has been validated
$userIdCookieParam = $this->getCookieParameter('oauth_user_id', $request, null);
if ($userIdCookieParam === null) {
return $this->loginUserResponseType->handle($client, $scopes, $queryString, $this->pathToPrivateKey);
} else {
try {
$userId = KeyCrypt::decrypt($userIdCookieParam, $this->pathToPublicKey);
} catch (\LogicException $e) {
throw OAuthServerException::serverError($e->getMessage());
}
}
// Check the user has approved the request
$userApprovedCookieParam = $this->getCookieParameter('oauth_user_approved_client', $request, null);
if ($userApprovedCookieParam === null) {
return $this->authorizeClientResponseType->handle($client, $scopes, $queryString, $this->pathToPrivateKey);
} else {
try {
$userApprovedClient = KeyCrypt::decrypt($userApprovedCookieParam, $this->pathToPublicKey);
} catch (\LogicException $e) {
throw OAuthServerException::serverError($e->getMessage());
}
}
$stateParameter = $this->getQueryStringParameter('state', $request);
$redirectUri = new Uri($redirectUri);
parse_str($redirectUri->getQuery(), $redirectPayload);
if ($stateParameter !== null) {
$redirectPayload['state'] = $stateParameter;
}
if ($userApprovedClient === 1) {
$authCode = $this->issueAuthCode(
$this->authCodeTTL,
$client,
$userId,
$redirectUri,
$scopes
);
$this->authCodeRepository->persistNewAuthCode($authCode);
$redirectPayload['code'] = $authCode->getIdentifier();
return new Response(
'php://memory',
302,
[
'Location' => $redirectUri->withQuery(http_build_query($redirectPayload)),
]
);
}
$exception = OAuthServerException::accessDenied('The user denied the request', (string) $redirectUri);
return $exception->generateHttpResponse();
2016-02-11 23:00:01 +05:30
}
/**
2016-02-11 23:00:01 +05:30
* Respond to an access token request
*
2016-02-11 23:00:01 +05:30
* @param \Psr\Http\Message\ServerRequestInterface $request
* @param \League\OAuth2\Server\ResponseTypes\ResponseTypeInterface $responseType
* @param \DateInterval $accessTokenTTL
2014-12-10 18:40:35 +05:30
*
2016-02-11 23:00:01 +05:30
* @return \League\OAuth2\Server\ResponseTypes\ResponseTypeInterface
*/
2016-02-11 23:00:01 +05:30
protected function respondToAccessTokenRequest(
ServerRequestInterface $request,
ResponseTypeInterface $responseType,
DateInterval $accessTokenTTL
) {
2014-04-06 23:44:46 +05:30
2016-02-11 23:00:01 +05:30
}
/**
2016-02-11 23:00:01 +05:30
* @inheritdoc
2013-02-14 01:06:56 +05:30
*/
2016-02-11 23:00:01 +05:30
public function canRespondToRequest(ServerRequestInterface $request)
{
return (
(
strtoupper($request->getMethod()) === 'GET'
&& isset($request->getQueryParams()['response_type'])
&& $request->getQueryParams()['response_type'] === 'code'
&& isset($request->getQueryParams()['client_id'])
) || (parent::canRespondToRequest($request))
2014-04-06 23:44:46 +05:30
);
2013-02-01 16:20:32 +05:30
}
/**
* Return the grant identifier that can be used in matching up requests
*
* @return string
*/
public function getIdentifier()
{
return 'authorization_code';
}
/**
* @inheritdoc
*/
public function respondToRequest(
ServerRequestInterface $request,
ResponseTypeInterface $responseType,
\DateInterval $accessTokenTTL
) {
if (
isset($request->getQueryParams()['response_type'])
&& $request->getQueryParams()['response_type'] === 'code'
&& isset($request->getQueryParams()['client_id'])
) {
return $this->respondToAuthorizationRequest($request);
} elseif (
isset($request->getParsedBody()['grant_type'])
&& $request->getParsedBody()['grant_type'] === 'authorization_code'
) {
return $this->respondToAccessTokenRequest($request, $responseType, $accessTokenTTL);
} else {
throw OAuthServerException::serverError('respondToRequest() should not have been called');
}
}
2013-09-07 22:29:44 +05:30
}