Skip to content

Commit

Permalink
Merge pull request #1436 from ssigwart/intUserId
Browse files Browse the repository at this point in the history
Fix refresh token int user ID
  • Loading branch information
Sephster authored Nov 14, 2024
2 parents e76e647 + c406da5 commit fe04ef2
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Grant/RefreshTokenGrant.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@ public function respondToAccessTokenRequest(
}

// Issue and persist new access token
$accessToken = $this->issueAccessToken($accessTokenTTL, $client, $oldRefreshToken['user_id'], $scopes);
$userId = $oldRefreshToken['user_id'];
if (is_int($userId)) {
$userId = (string) $userId;
}
$accessToken = $this->issueAccessToken($accessTokenTTL, $client, $userId, $scopes);
$this->getEmitter()->emit(new RequestAccessTokenEvent(RequestEvent::ACCESS_TOKEN_ISSUED, $request, $accessToken));
$responseType->setAccessToken($accessToken);

Expand Down
64 changes: 64 additions & 0 deletions tests/Grant/RefreshTokenGrantTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -754,4 +754,68 @@ public function testUnrevokedRefreshToken(): void
self::assertObjectHasProperty('refresh_token', $json);
self::assertNotSame($json->refresh_token, $encryptedOldRefreshToken);
}

public function testRespondToRequestWithIntUserId(): void
{
$client = new ClientEntity();
$client->setIdentifier('foo');
$client->setRedirectUri('http://foo/bar');

$clientRepositoryMock = $this->getMockBuilder(ClientRepositoryInterface::class)->getMock();
$clientRepositoryMock->method('getClientEntity')->willReturn($client);
$clientRepositoryMock->method('validateClient')->willReturn(true);

$scopeEntity = new ScopeEntity();
$scopeEntity->setIdentifier('foo');
$scopeRepositoryMock = $this->getMockBuilder(ScopeRepositoryInterface::class)->getMock();
$scopeRepositoryMock->method('getScopeEntityByIdentifier')->willReturn($scopeEntity);
$scopeRepositoryMock->method('finalizeScopes')->willReturn([$scopeEntity]);

$accessTokenRepositoryMock = $this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock();
$accessTokenRepositoryMock->method('getNewToken')->willReturn(new AccessTokenEntity());
$accessTokenRepositoryMock->expects(self::once())->method('persistNewAccessToken')->willReturnSelf();

$refreshTokenRepositoryMock = $this->getMockBuilder(RefreshTokenRepositoryInterface::class)->getMock();
$refreshTokenRepositoryMock->method('getNewRefreshToken')->willReturn(new RefreshTokenEntity());
$refreshTokenRepositoryMock->expects(self::once())->method('persistNewRefreshToken')->willReturnSelf();

$grant = new RefreshTokenGrant($refreshTokenRepositoryMock);
$grant->setClientRepository($clientRepositoryMock);
$grant->setScopeRepository($scopeRepositoryMock);
$grant->setAccessTokenRepository($accessTokenRepositoryMock);
$grant->setEncryptionKey($this->cryptStub->getKey());
$grant->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key'));
$grant->revokeRefreshTokens(true);

$oldRefreshToken = json_encode(
[
'client_id' => 'foo',
'refresh_token_id' => 'zyxwvu',
'access_token_id' => 'abcdef',
'scopes' => ['foo'],
'user_id' => 123,
'expire_time' => time() + 3600,
]
);

if ($oldRefreshToken === false) {
self::fail('json_encode failed');
}

$encryptedOldRefreshToken = $this->cryptStub->doEncrypt(
$oldRefreshToken
);

$serverRequest = (new ServerRequest())->withParsedBody([
'client_id' => 'foo',
'client_secret' => 'bar',
'refresh_token' => $encryptedOldRefreshToken,
'scopes' => ['foo'],
]);

$responseType = new StubResponseType();
$grant->respondToAccessTokenRequest($serverRequest, $responseType, new DateInterval('PT5M'));

self::assertInstanceOf(RefreshTokenEntityInterface::class, $responseType->getRefreshToken());
}
}

0 comments on commit fe04ef2

Please sign in to comment.