Skip to content

Commit 3fc71b8

Browse files
authored
Merge pull request #1094 from paulo-jay/RefreshTokenGrantFinalizeScopes
RefreshTokenGrant calls finalizeScopes method
2 parents c7c44c6 + 5657640 commit 3fc71b8

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

src/Grant/RefreshTokenGrant.php

+2
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ public function respondToAccessTokenRequest(
5959
}
6060
}
6161

62+
$scopes = $this->scopeRepository->finalizeScopes($scopes, $this->getIdentifier(), $client);
63+
6264
// Expire old tokens
6365
$this->accessTokenRepository->revokeAccessToken($oldRefreshToken['access_token_id']);
6466
$this->refreshTokenRepository->revokeRefreshToken($oldRefreshToken['refresh_token_id']);

tests/Grant/RefreshTokenGrantTest.php

+73
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public function testRespondToRequest()
5151
$scopeEntity->setIdentifier('foo');
5252
$scopeRepositoryMock = $this->getMockBuilder(ScopeRepositoryInterface::class)->getMock();
5353
$scopeRepositoryMock->method('getScopeEntityByIdentifier')->willReturn($scopeEntity);
54+
$scopeRepositoryMock->method('finalizeScopes')->willReturn([$scopeEntity]);
5455

5556
$accessTokenRepositoryMock = $this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock();
5657
$accessTokenRepositoryMock->method('getNewToken')->willReturn(new AccessTokenEntity());
@@ -107,6 +108,7 @@ public function testRespondToRequestNullRefreshToken()
107108

108109
$scopeRepositoryMock = $this->getMockBuilder(ScopeRepositoryInterface::class)->getMock();
109110
$scopeRepositoryMock->method('getScopeEntityByIdentifier')->willReturn($scopeEntity);
111+
$scopeRepositoryMock->method('finalizeScopes')->willReturn([$scopeEntity]);
110112

111113
$accessTokenRepositoryMock = $this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock();
112114
$accessTokenRepositoryMock->method('getNewToken')->willReturn(new AccessTokenEntity());
@@ -169,6 +171,7 @@ public function testRespondToReducedScopes()
169171
$scope->setIdentifier('foo');
170172
$scopeRepositoryMock = $this->getMockBuilder(ScopeRepositoryInterface::class)->getMock();
171173
$scopeRepositoryMock->method('getScopeEntityByIdentifier')->willReturn($scope);
174+
$scopeRepositoryMock->method('finalizeScopes')->willReturn([$scope]);
172175

173176
$grant = new RefreshTokenGrant($refreshTokenRepositoryMock);
174177
$grant->setClientRepository($clientRepositoryMock);
@@ -450,4 +453,74 @@ public function testRespondToRequestRevokedToken()
450453

451454
$grant->respondToAccessTokenRequest($serverRequest, $responseType, new DateInterval('PT5M'));
452455
}
456+
457+
public function testRespondToRequestFinalizeScopes()
458+
{
459+
$client = new ClientEntity();
460+
$client->setIdentifier('foo');
461+
$clientRepositoryMock = $this->getMockBuilder(ClientRepositoryInterface::class)->getMock();
462+
$clientRepositoryMock->method('getClientEntity')->willReturn($client);
463+
464+
$fooScopeEntity = new ScopeEntity();
465+
$fooScopeEntity->setIdentifier('foo');
466+
467+
$barScopeEntity = new ScopeEntity();
468+
$barScopeEntity->setIdentifier('bar');
469+
470+
$scopeRepositoryMock = $this->getMockBuilder(ScopeRepositoryInterface::class)->getMock();
471+
$scopeRepositoryMock->method('getScopeEntityByIdentifier')->willReturn($fooScopeEntity, $barScopeEntity);
472+
473+
$accessTokenRepositoryMock = $this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock();
474+
$accessTokenRepositoryMock->method('persistNewAccessToken')->willReturnSelf();
475+
476+
$refreshTokenRepositoryMock = $this->getMockBuilder(RefreshTokenRepositoryInterface::class)->getMock();
477+
$refreshTokenRepositoryMock->method('getNewRefreshToken')->willReturn(new RefreshTokenEntity());
478+
$refreshTokenRepositoryMock->method('persistNewRefreshToken')->willReturnSelf();
479+
480+
$grant = new RefreshTokenGrant($refreshTokenRepositoryMock);
481+
$grant->setClientRepository($clientRepositoryMock);
482+
$grant->setScopeRepository($scopeRepositoryMock);
483+
$grant->setAccessTokenRepository($accessTokenRepositoryMock);
484+
$grant->setEncryptionKey($this->cryptStub->getKey());
485+
$grant->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key'));
486+
487+
488+
$scopes = [$fooScopeEntity, $barScopeEntity];
489+
$finalizedScopes = [$fooScopeEntity];
490+
491+
$scopeRepositoryMock
492+
->expects($this->once())
493+
->method('finalizeScopes')
494+
->with($scopes, $grant->getIdentifier(), $client)
495+
->willReturn($finalizedScopes);
496+
497+
$accessTokenRepositoryMock
498+
->method('getNewToken')
499+
->with($client, $finalizedScopes)
500+
->willReturn(new AccessTokenEntity());
501+
502+
$oldRefreshToken = $this->cryptStub->doEncrypt(
503+
\json_encode(
504+
[
505+
'client_id' => 'foo',
506+
'refresh_token_id' => 'zyxwvu',
507+
'access_token_id' => 'abcdef',
508+
'scopes' => ['foo', 'bar'],
509+
'user_id' => 123,
510+
'expire_time' => \time() + 3600,
511+
]
512+
)
513+
);
514+
515+
$serverRequest = (new ServerRequest())->withParsedBody([
516+
'client_id' => 'foo',
517+
'client_secret' => 'bar',
518+
'refresh_token' => $oldRefreshToken,
519+
'scope' => ['foo', 'bar'],
520+
]);
521+
522+
$responseType = new StubResponseType();
523+
524+
$grant->respondToAccessTokenRequest($serverRequest, $responseType, new DateInterval('PT5M'));
525+
}
453526
}

0 commit comments

Comments
 (0)