|
18 | 18 | use LeagueTests\Stubs\RefreshTokenEntity;
|
19 | 19 | use LeagueTests\Stubs\ScopeEntity;
|
20 | 20 | use LeagueTests\Stubs\StubResponseType;
|
| 21 | +use PHPUnit\Framework\Assert; |
21 | 22 | use PHPUnit\Framework\TestCase;
|
22 | 23 |
|
23 | 24 | class RefreshTokenGrantTest extends TestCase
|
@@ -68,6 +69,7 @@ public function testRespondToRequest()
|
68 | 69 | $grant->setAccessTokenRepository($accessTokenRepositoryMock);
|
69 | 70 | $grant->setEncryptionKey($this->cryptStub->getKey());
|
70 | 71 | $grant->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key'));
|
| 72 | + $grant->revokeRefreshTokens(true); |
71 | 73 |
|
72 | 74 | $oldRefreshToken = $this->cryptStub->doEncrypt(
|
73 | 75 | \json_encode(
|
@@ -181,6 +183,7 @@ public function testRespondToReducedScopes()
|
181 | 183 | $grant->setScopeRepository($scopeRepositoryMock);
|
182 | 184 | $grant->setEncryptionKey($this->cryptStub->getKey());
|
183 | 185 | $grant->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key'));
|
| 186 | + $grant->revokeRefreshTokens(true); |
184 | 187 |
|
185 | 188 | $oldRefreshToken = $this->cryptStub->doEncrypt(
|
186 | 189 | \json_encode(
|
@@ -467,4 +470,118 @@ public function testRespondToRequestRevokedToken()
|
467 | 470 |
|
468 | 471 | $grant->respondToAccessTokenRequest($serverRequest, $responseType, new DateInterval('PT5M'));
|
469 | 472 | }
|
| 473 | + |
| 474 | + public function testRevokedRefreshToken() |
| 475 | + { |
| 476 | + $refreshTokenId = 'foo'; |
| 477 | + |
| 478 | + $client = new ClientEntity(); |
| 479 | + $client->setIdentifier('foo'); |
| 480 | + $client->setRedirectUri('http://foo/bar'); |
| 481 | + |
| 482 | + $clientRepositoryMock = $this->getMockBuilder(ClientRepositoryInterface::class)->getMock(); |
| 483 | + $clientRepositoryMock->method('getClientEntity')->willReturn($client); |
| 484 | + |
| 485 | + $scopeEntity = new ScopeEntity(); |
| 486 | + $scopeEntity->setIdentifier('foo'); |
| 487 | + |
| 488 | + $scopeRepositoryMock = $this->getMockBuilder(ScopeRepositoryInterface::class)->getMock(); |
| 489 | + $scopeRepositoryMock->method('getScopeEntityByIdentifier')->willReturn($scopeEntity); |
| 490 | + |
| 491 | + $accessTokenRepositoryMock = $this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock(); |
| 492 | + $accessTokenRepositoryMock->method('getNewToken')->willReturn(new AccessTokenEntity()); |
| 493 | + $accessTokenRepositoryMock->expects($this->once())->method('persistNewAccessToken')->willReturnSelf(); |
| 494 | + |
| 495 | + $refreshTokenRepositoryMock = $this->getMockBuilder(RefreshTokenRepositoryInterface::class)->getMock(); |
| 496 | + $refreshTokenRepositoryMock->method('isRefreshTokenRevoked') |
| 497 | + ->will($this->onConsecutiveCalls(false, true)); |
| 498 | + $refreshTokenRepositoryMock->expects($this->once())->method('revokeRefreshToken')->with($this->equalTo($refreshTokenId)); |
| 499 | + |
| 500 | + $oldRefreshToken = $this->cryptStub->doEncrypt( |
| 501 | + \json_encode( |
| 502 | + [ |
| 503 | + 'client_id' => 'foo', |
| 504 | + 'refresh_token_id' => $refreshTokenId, |
| 505 | + 'access_token_id' => 'abcdef', |
| 506 | + 'scopes' => ['foo'], |
| 507 | + 'user_id' => 123, |
| 508 | + 'expire_time' => \time() + 3600, |
| 509 | + ] |
| 510 | + ) |
| 511 | + ); |
| 512 | + |
| 513 | + $serverRequest = (new ServerRequest())->withParsedBody([ |
| 514 | + 'client_id' => 'foo', |
| 515 | + 'client_secret' => 'bar', |
| 516 | + 'refresh_token' => $oldRefreshToken, |
| 517 | + 'scope' => ['foo'], |
| 518 | + ]); |
| 519 | + |
| 520 | + $grant = new RefreshTokenGrant($refreshTokenRepositoryMock); |
| 521 | + $grant->setClientRepository($clientRepositoryMock); |
| 522 | + $grant->setScopeRepository($scopeRepositoryMock); |
| 523 | + $grant->setAccessTokenRepository($accessTokenRepositoryMock); |
| 524 | + $grant->setEncryptionKey($this->cryptStub->getKey()); |
| 525 | + $grant->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key')); |
| 526 | + $grant->revokeRefreshTokens(true); |
| 527 | + $grant->respondToAccessTokenRequest($serverRequest, new StubResponseType(), new DateInterval('PT5M')); |
| 528 | + |
| 529 | + Assert::assertTrue($refreshTokenRepositoryMock->isRefreshTokenRevoked($refreshTokenId)); |
| 530 | + } |
| 531 | + |
| 532 | + public function testUnrevokedRefreshToken() |
| 533 | + { |
| 534 | + $refreshTokenId = 'foo'; |
| 535 | + |
| 536 | + $client = new ClientEntity(); |
| 537 | + $client->setIdentifier('foo'); |
| 538 | + $client->setRedirectUri('http://foo/bar'); |
| 539 | + |
| 540 | + $clientRepositoryMock = $this->getMockBuilder(ClientRepositoryInterface::class)->getMock(); |
| 541 | + $clientRepositoryMock->method('getClientEntity')->willReturn($client); |
| 542 | + |
| 543 | + $scopeEntity = new ScopeEntity(); |
| 544 | + $scopeEntity->setIdentifier('foo'); |
| 545 | + |
| 546 | + $scopeRepositoryMock = $this->getMockBuilder(ScopeRepositoryInterface::class)->getMock(); |
| 547 | + $scopeRepositoryMock->method('getScopeEntityByIdentifier')->willReturn($scopeEntity); |
| 548 | + |
| 549 | + $accessTokenRepositoryMock = $this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock(); |
| 550 | + $accessTokenRepositoryMock->method('getNewToken')->willReturn(new AccessTokenEntity()); |
| 551 | + $accessTokenRepositoryMock->expects($this->once())->method('persistNewAccessToken')->willReturnSelf(); |
| 552 | + |
| 553 | + $refreshTokenRepositoryMock = $this->getMockBuilder(RefreshTokenRepositoryInterface::class)->getMock(); |
| 554 | + $refreshTokenRepositoryMock->method('isRefreshTokenRevoked')->willReturn(false); |
| 555 | + $refreshTokenRepositoryMock->expects($this->never())->method('revokeRefreshToken'); |
| 556 | + |
| 557 | + $oldRefreshToken = $this->cryptStub->doEncrypt( |
| 558 | + \json_encode( |
| 559 | + [ |
| 560 | + 'client_id' => 'foo', |
| 561 | + 'refresh_token_id' => $refreshTokenId, |
| 562 | + 'access_token_id' => 'abcdef', |
| 563 | + 'scopes' => ['foo'], |
| 564 | + 'user_id' => 123, |
| 565 | + 'expire_time' => \time() + 3600, |
| 566 | + ] |
| 567 | + ) |
| 568 | + ); |
| 569 | + |
| 570 | + $serverRequest = (new ServerRequest())->withParsedBody([ |
| 571 | + 'client_id' => 'foo', |
| 572 | + 'client_secret' => 'bar', |
| 573 | + 'refresh_token' => $oldRefreshToken, |
| 574 | + 'scope' => ['foo'], |
| 575 | + ]); |
| 576 | + |
| 577 | + $grant = new RefreshTokenGrant($refreshTokenRepositoryMock); |
| 578 | + $grant->setClientRepository($clientRepositoryMock); |
| 579 | + $grant->setScopeRepository($scopeRepositoryMock); |
| 580 | + $grant->setAccessTokenRepository($accessTokenRepositoryMock); |
| 581 | + $grant->setEncryptionKey($this->cryptStub->getKey()); |
| 582 | + $grant->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key')); |
| 583 | + $grant->respondToAccessTokenRequest($serverRequest, new StubResponseType(), new DateInterval('PT5M')); |
| 584 | + |
| 585 | + Assert::assertFalse($refreshTokenRepositoryMock->isRefreshTokenRevoked($refreshTokenId)); |
| 586 | + } |
470 | 587 | }
|
0 commit comments