Skip to content

Commit

Permalink
Merge branch 'master' into php84
Browse files Browse the repository at this point in the history
  • Loading branch information
hafezdivandari authored Oct 14, 2024
2 parents 9b6045d + 1dc1ee8 commit dae4528
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 15 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]

## [9.0.1] - released 2024-10-14
### Fixed
- Auto-generated event emitter is now persisted. Previously, a new emitter was generated every time (PR #1428)
- Fixed bug where you could not omit a redirect uri even if one had not been specified during the auth request (PR #1428)
- Fixed bug where "state" parameter wasn't present on `invalid_scope` error response and wasn't on fragment part of `access_denied` redirect URI on Implicit grant (PR #1298)
- Fixed bug where disabling refresh token revocation via `revokeRefreshTokens(false)` unintentionally disables issuing new refresh token (PR #1449)

## [9.0.0] - released 2024-05-13
### Added
Expand Down Expand Up @@ -652,7 +655,8 @@ Version 5 is a complete code rewrite.

- First major release

[Unreleased]: https://github.com/thephpleague/oauth2-server/compare/9.0.0...HEAD
[Unreleased]: https://github.com/thephpleague/oauth2-server/compare/9.0.1...HEAD
[9.0.1]: https://github.com/thephpleague/oauth2-server/compare/9.0.0...9.0.1
[9.0.0]: https://github.com/thephpleague/oauth2-server/compare/9.0.0-RC1...9.0.0
[9.0.0-RC1]: https://github.com/thephpleague/oauth2-server/compare/8.5.4...9.0.0-RC1
[8.5.4]: https://github.com/thephpleague/oauth2-server/compare/8.5.3...8.5.4
Expand Down
10 changes: 4 additions & 6 deletions src/Grant/RefreshTokenGrant.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,11 @@ public function respondToAccessTokenRequest(
$responseType->setAccessToken($accessToken);

// Issue and persist new refresh token if given
if ($this->revokeRefreshTokens) {
$refreshToken = $this->issueRefreshToken($accessToken);
$refreshToken = $this->issueRefreshToken($accessToken);

if ($refreshToken !== null) {
$this->getEmitter()->emit(new RequestRefreshTokenEvent(RequestEvent::REFRESH_TOKEN_ISSUED, $request, $refreshToken));
$responseType->setRefreshToken($refreshToken);
}
if ($refreshToken !== null) {
$this->getEmitter()->emit(new RequestRefreshTokenEvent(RequestEvent::REFRESH_TOKEN_ISSUED, $request, $refreshToken));
$responseType->setRefreshToken($refreshToken);
}

return $responseType;
Expand Down
36 changes: 28 additions & 8 deletions tests/Grant/RefreshTokenGrantTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface;
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
use League\OAuth2\Server\ResponseTypes\BearerTokenResponse;
use LeagueTests\Stubs\AccessTokenEntity;
use LeagueTests\Stubs\ClientEntity;
use LeagueTests\Stubs\CryptTraitStub;
use LeagueTests\Stubs\RefreshTokenEntity;
use LeagueTests\Stubs\ScopeEntity;
use LeagueTests\Stubs\StubResponseType;
use Nyholm\Psr7\Response;
use Nyholm\Psr7\ServerRequest;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -592,10 +594,10 @@ public function testRespondToRequestFinalizeScopes(): void
);

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

$responseType = new StubResponseType();
Expand Down Expand Up @@ -628,7 +630,7 @@ public function testRevokedRefreshToken(): void

$refreshTokenRepositoryMock = $this->getMockBuilder(RefreshTokenRepositoryInterface::class)->getMock();
$refreshTokenRepositoryMock->method('isRefreshTokenRevoked')
->will(self::onConsecutiveCalls(false, true));
->will(self::onConsecutiveCalls(false, true));
$refreshTokenRepositoryMock->expects(self::once())->method('revokeRefreshToken')->with(self::equalTo($refreshTokenId));

$oldRefreshToken = json_encode(
Expand Down Expand Up @@ -688,11 +690,15 @@ public function testUnrevokedRefreshToken(): void
$scopeRepositoryMock->method('getScopeEntityByIdentifier')->willReturn($scopeEntity);
$scopeRepositoryMock->method('finalizeScopes')->willReturn([$scopeEntity]);

$accessTokenEntity = new AccessTokenEntity();
$accessTokenEntity->setClient($client);

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

$refreshTokenRepositoryMock = $this->getMockBuilder(RefreshTokenRepositoryInterface::class)->getMock();
$refreshTokenRepositoryMock->method('getNewRefreshToken')->willReturn(new RefreshTokenEntity());
$refreshTokenRepositoryMock->method('isRefreshTokenRevoked')->willReturn(false);
$refreshTokenRepositoryMock->expects(self::never())->method('revokeRefreshToken');

Expand Down Expand Up @@ -722,16 +728,30 @@ public function testUnrevokedRefreshToken(): void
'scope' => 'foo',
]);

$privateKey = new CryptKey('file://' . __DIR__ . '/../Stubs/private.key');

$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->setPrivateKey($privateKey);
$grant->revokeRefreshTokens(false);

$grant->respondToAccessTokenRequest($serverRequest, new StubResponseType(), new DateInterval('PT5M'));
$responseType = new BearerTokenResponse();
$responseType->setPrivateKey($privateKey);
$responseType->setEncryptionKey($this->cryptStub->getKey());

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

$json = json_decode((string) $response->getBody());

self::assertFalse($refreshTokenRepositoryMock->isRefreshTokenRevoked($refreshTokenId));
self::assertEquals('Bearer', $json->token_type);
self::assertObjectHasProperty('expires_in', $json);
self::assertObjectHasProperty('access_token', $json);
self::assertObjectHasProperty('refresh_token', $json);
self::assertNotSame($json->refresh_token, $encryptedOldRefreshToken);
}
}

0 comments on commit dae4528

Please sign in to comment.