Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include scope in response if given scopes don't match requested scopes #928

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/AuthorizationServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public function respondToAccessTokenRequest(ServerRequestInterface $request, Res
);

if ($tokenResponse instanceof ResponseTypeInterface) {
return $tokenResponse->generateHttpResponse($response);
return $tokenResponse->generateHttpResponse($response, $request);
}
}

Expand Down
19 changes: 18 additions & 1 deletion src/ResponseTypes/BearerTokenResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,41 @@
use League\OAuth2\Server\Entities\AccessTokenEntityInterface;
use League\OAuth2\Server\Entities\RefreshTokenEntityInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

class BearerTokenResponse extends AbstractResponseType
{
/**
* {@inheritdoc}
*/
public function generateHttpResponse(ResponseInterface $response)
public function generateHttpResponse(ResponseInterface $response, ServerRequestInterface $request = null)
{
$expireDateTime = $this->accessToken->getExpiryDateTime()->getTimestamp();

$jwtAccessToken = $this->accessToken->convertToJWT($this->privateKey);

if ($request) {
$requestParameters = (array) $request->getParsedBody();
}

$requestedScopes = $requestParameters['scope'] ?? [];

$responseParams = [
'token_type' => 'Bearer',
'expires_in' => $expireDateTime - (new \DateTime())->getTimestamp(),
'access_token' => (string) $jwtAccessToken,
];

$givenScopes = array_map(function ($scope) {
return $scope->getIdentifier();
}, $this->accessToken->getScopes());

if (!empty(array_diff($requestedScopes, $givenScopes))
|| !empty(array_diff($givenScopes, $requestedScopes))
) {
$responseParams['scope'] = implode(' ', $givenScopes);
}

if ($this->refreshToken instanceof RefreshTokenEntityInterface) {
$refreshToken = $this->encrypt(
json_encode(
Expand Down
3 changes: 2 additions & 1 deletion src/ResponseTypes/RedirectResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace League\OAuth2\Server\ResponseTypes;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

class RedirectResponse extends AbstractResponseType
{
Expand All @@ -33,7 +34,7 @@ public function setRedirectUri($redirectUri)
*
* @return ResponseInterface
*/
public function generateHttpResponse(ResponseInterface $response)
public function generateHttpResponse(ResponseInterface $response, ServerRequestInterface $request = null)
{
return $response->withStatus(302)->withHeader('Location', $this->redirectUri);
}
Expand Down
3 changes: 2 additions & 1 deletion src/ResponseTypes/ResponseTypeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use League\OAuth2\Server\Entities\AccessTokenEntityInterface;
use League\OAuth2\Server\Entities\RefreshTokenEntityInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

interface ResponseTypeInterface
{
Expand All @@ -33,7 +34,7 @@ public function setRefreshToken(RefreshTokenEntityInterface $refreshToken);
*
* @return ResponseInterface
*/
public function generateHttpResponse(ResponseInterface $response);
public function generateHttpResponse(ResponseInterface $response, ServerRequestInterface $request = null);

/**
* Set the encryption key
Expand Down
59 changes: 54 additions & 5 deletions tests/ResponseTypes/BearerResponseTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function testGenerateHttpResponse()
$responseType->setAccessToken($accessToken);
$responseType->setRefreshToken($refreshToken);

$response = $responseType->generateHttpResponse(new Response());
$response = $responseType->generateHttpResponse(new Response(), new ServerRequest());

$this->assertInstanceOf(ResponseInterface::class, $response);
$this->assertEquals(200, $response->getStatusCode());
Expand Down Expand Up @@ -86,7 +86,7 @@ public function testGenerateHttpResponseWithExtraParams()
$responseType->setAccessToken($accessToken);
$responseType->setRefreshToken($refreshToken);

$response = $responseType->generateHttpResponse(new Response());
$response = $responseType->generateHttpResponse(new Response(), new ServerRequest());

$this->assertInstanceOf(ResponseInterface::class, $response);
$this->assertEquals(200, $response->getStatusCode());
Expand All @@ -100,9 +100,58 @@ public function testGenerateHttpResponseWithExtraParams()
$this->assertObjectHasAttribute('expires_in', $json);
$this->assertObjectHasAttribute('access_token', $json);
$this->assertObjectHasAttribute('refresh_token', $json);
$this->assertObjectHasAttribute('scope', $json);

$this->assertObjectHasAttribute('foo', $json);
$this->assertAttributeEquals('bar', 'foo', $json);
$this->assertAttributeEquals('basic', 'scope', $json);
}

public function testGenerateHttpResponseWithInvalidScope()
{
$responseType = new BearerTokenResponse();
$responseType->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key'));
$responseType->setEncryptionKey(base64_encode(random_bytes(36)));

$client = new ClientEntity();
$client->setIdentifier('clientName');

$scope = new ScopeEntity();
$scope->setIdentifier('basic');

$accessToken = new AccessTokenEntity();
$accessToken->setIdentifier('abcdef');
$accessToken->setExpiryDateTime((new \DateTime())->add(new \DateInterval('PT1H')));
$accessToken->setClient($client);
$accessToken->addScope($scope);

$refreshToken = new RefreshTokenEntity();
$refreshToken->setIdentifier('abcdef');
$refreshToken->setAccessToken($accessToken);
$refreshToken->setExpiryDateTime((new \DateTime())->add(new \DateInterval('PT1H')));

$responseType->setAccessToken($accessToken);
$responseType->setRefreshToken($refreshToken);

$request = new ServerRequest(['scope' => 'invalid']);

$response = $responseType->generateHttpResponse(new Response(), $request);

$this->assertInstanceOf(ResponseInterface::class, $response);
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('no-cache', $response->getHeader('pragma')[0]);
$this->assertEquals('no-store', $response->getHeader('cache-control')[0]);
$this->assertEquals('application/json; charset=UTF-8', $response->getHeader('content-type')[0]);

$response->getBody()->rewind();
$json = json_decode($response->getBody()->getContents());
$this->assertAttributeEquals('Bearer', 'token_type', $json);
$this->assertObjectHasAttribute('expires_in', $json);
$this->assertObjectHasAttribute('access_token', $json);
$this->assertObjectHasAttribute('refresh_token', $json);
$this->assertObjectHasAttribute('scope', $json);

$this->assertAttributeEquals('basic', 'scope', $json);
}

public function testDetermineAccessTokenInHeaderValidToken()
Expand All @@ -128,7 +177,7 @@ public function testDetermineAccessTokenInHeaderValidToken()
$responseType->setAccessToken($accessToken);
$responseType->setRefreshToken($refreshToken);

$response = $responseType->generateHttpResponse(new Response());
$response = $responseType->generateHttpResponse(new Response(), new ServerRequest());
$json = json_decode((string) $response->getBody());

$accessTokenRepositoryMock = $this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock();
Expand Down Expand Up @@ -173,7 +222,7 @@ public function testDetermineAccessTokenInHeaderInvalidJWT()
$responseType->setAccessToken($accessToken);
$responseType->setRefreshToken($refreshToken);

$response = $responseType->generateHttpResponse(new Response());
$response = $responseType->generateHttpResponse(new Response(), new ServerRequest());
$json = json_decode((string) $response->getBody());

$authorizationValidator = new BearerTokenValidator($accessTokenRepositoryMock);
Expand Down Expand Up @@ -215,7 +264,7 @@ public function testDetermineAccessTokenInHeaderRevokedToken()
$responseType->setAccessToken($accessToken);
$responseType->setRefreshToken($refreshToken);

$response = $responseType->generateHttpResponse(new Response());
$response = $responseType->generateHttpResponse(new Response(), new ServerRequest());
$json = json_decode((string) $response->getBody());

$accessTokenRepositoryMock = $this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock();
Expand Down
2 changes: 1 addition & 1 deletion tests/Stubs/StubResponseType.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function validateAccessToken(ServerRequestInterface $request)
*
* @return ResponseInterface
*/
public function generateHttpResponse(ResponseInterface $response)
public function generateHttpResponse(ResponseInterface $response, ServerRequestInterface $request = null)
{
return new Response();
}
Expand Down