diff --git a/tests/Grant/AuthCodeGrantTest.php b/tests/Grant/AuthCodeGrantTest.php index 390001721..76750f1e9 100644 --- a/tests/Grant/AuthCodeGrantTest.php +++ b/tests/Grant/AuthCodeGrantTest.php @@ -17,6 +17,9 @@ use League\OAuth2\Server\Repositories\ClientRepositoryInterface; use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface; use League\OAuth2\Server\Repositories\ScopeRepositoryInterface; +use League\OAuth2\Server\RequestAccessTokenEvent; +use League\OAuth2\Server\RequestEvent; +use League\OAuth2\Server\RequestRefreshTokenEvent; use League\OAuth2\Server\RequestTypes\AuthorizationRequest; use League\OAuth2\Server\ResponseTypes\RedirectResponse; use LeagueTests\Stubs\AccessTokenEntity; @@ -635,6 +638,27 @@ public function testRespondToAccessTokenRequest(): void $grant->setEncryptionKey($this->cryptStub->getKey()); $grant->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key')); + $accessTokenEventEmitted = false; + $refreshTokenEventEmitted = false; + + $grant->getListenerRegistry()->subscribeTo( + RequestEvent::ACCESS_TOKEN_ISSUED, + function ($event) use (&$accessTokenEventEmitted): void { + self::assertInstanceOf(RequestAccessTokenEvent::class, $event); + + $accessTokenEventEmitted = true; + } + ); + + $grant->getListenerRegistry()->subscribeTo( + RequestEvent::REFRESH_TOKEN_ISSUED, + function ($event) use (&$refreshTokenEventEmitted): void { + self::assertInstanceOf(RequestRefreshTokenEvent::class, $event); + + $refreshTokenEventEmitted = true; + } + ); + $request = new ServerRequest( [], [], @@ -665,6 +689,14 @@ public function testRespondToAccessTokenRequest(): void $response = $grant->respondToAccessTokenRequest($request, new StubResponseType(), new DateInterval('PT10M')); self::assertInstanceOf(RefreshTokenEntityInterface::class, $response->getRefreshToken()); + + if (!$accessTokenEventEmitted) { + self::fail('Access token issued event is not emitted.'); + } + + if (!$refreshTokenEventEmitted) { + self::fail('Refresh token issued event is not emitted.'); + } } public function testRespondToAccessTokenRequestWithDefaultRedirectUri(): void diff --git a/tests/Grant/ClientCredentialsGrantTest.php b/tests/Grant/ClientCredentialsGrantTest.php index 69f756c37..788749ebb 100644 --- a/tests/Grant/ClientCredentialsGrantTest.php +++ b/tests/Grant/ClientCredentialsGrantTest.php @@ -11,6 +11,8 @@ use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface; use League\OAuth2\Server\Repositories\ClientRepositoryInterface; use League\OAuth2\Server\Repositories\ScopeRepositoryInterface; +use League\OAuth2\Server\RequestAccessTokenEvent; +use League\OAuth2\Server\RequestEvent; use LeagueTests\Stubs\AccessTokenEntity; use LeagueTests\Stubs\ClientEntity; use LeagueTests\Stubs\ScopeEntity; @@ -53,6 +55,17 @@ public function testRespondToRequest(): void $grant->setDefaultScope(self::DEFAULT_SCOPE); $grant->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key')); + $accessTokenEventEmitted = false; + + $grant->getListenerRegistry()->subscribeTo( + RequestEvent::ACCESS_TOKEN_ISSUED, + function ($event) use (&$accessTokenEventEmitted): void { + self::assertInstanceOf(RequestAccessTokenEvent::class, $event); + + $accessTokenEventEmitted = true; + } + ); + $serverRequest = (new ServerRequest())->withParsedBody([ 'client_id' => 'foo', 'client_secret' => 'bar', @@ -64,5 +77,9 @@ public function testRespondToRequest(): void $response = $grant->respondToAccessTokenRequest($serverRequest, $responseType, new DateInterval('PT5M')); self::assertNotEmpty($response->getAccessToken()->getIdentifier()); + + if (!$accessTokenEventEmitted) { + self::fail('Access token issued event is not emitted.'); + } } } diff --git a/tests/Grant/DeviceCodeGrantTest.php b/tests/Grant/DeviceCodeGrantTest.php index 42157a494..a2dcb5534 100644 --- a/tests/Grant/DeviceCodeGrantTest.php +++ b/tests/Grant/DeviceCodeGrantTest.php @@ -18,6 +18,9 @@ use League\OAuth2\Server\Repositories\DeviceCodeRepositoryInterface; use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface; use League\OAuth2\Server\Repositories\ScopeRepositoryInterface; +use League\OAuth2\Server\RequestAccessTokenEvent; +use League\OAuth2\Server\RequestEvent; +use League\OAuth2\Server\RequestRefreshTokenEvent; use LeagueTests\Stubs\AccessTokenEntity; use LeagueTests\Stubs\ClientEntity; use LeagueTests\Stubs\DeviceCodeEntity; @@ -380,6 +383,27 @@ public function testRespondToAccessTokenRequest(): void $grant->completeDeviceAuthorizationRequest($deviceCodeEntity->getIdentifier(), 'baz', true); + $accessTokenEventEmitted = false; + $refreshTokenEventEmitted = false; + + $grant->getListenerRegistry()->subscribeTo( + RequestEvent::ACCESS_TOKEN_ISSUED, + function ($event) use (&$accessTokenEventEmitted): void { + self::assertInstanceOf(RequestAccessTokenEvent::class, $event); + + $accessTokenEventEmitted = true; + } + ); + + $grant->getListenerRegistry()->subscribeTo( + RequestEvent::REFRESH_TOKEN_ISSUED, + function ($event) use (&$refreshTokenEventEmitted): void { + self::assertInstanceOf(RequestRefreshTokenEvent::class, $event); + + $refreshTokenEventEmitted = true; + } + ); + $serverRequest = (new ServerRequest())->withParsedBody([ 'grant_type' => 'urn:ietf:params:oauth:grant-type:device_code', 'device_code' => $deviceCodeEntity->getIdentifier(), @@ -391,6 +415,14 @@ public function testRespondToAccessTokenRequest(): void $this::assertInstanceOf(RefreshTokenEntityInterface::class, $responseType->getRefreshToken()); $this::assertSame([$scope], $responseType->getAccessToken()->getScopes()); + + if (!$accessTokenEventEmitted) { + self::fail('Access token issued event is not emitted.'); + } + + if (!$refreshTokenEventEmitted) { + self::fail('Refresh token issued event is not emitted.'); + } } public function testRespondToRequestMissingClient(): void diff --git a/tests/Grant/ImplicitGrantTest.php b/tests/Grant/ImplicitGrantTest.php index 617aaa842..60d0d26d7 100644 --- a/tests/Grant/ImplicitGrantTest.php +++ b/tests/Grant/ImplicitGrantTest.php @@ -14,6 +14,8 @@ use League\OAuth2\Server\Repositories\ClientRepositoryInterface; use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface; use League\OAuth2\Server\Repositories\ScopeRepositoryInterface; +use League\OAuth2\Server\RequestAccessTokenEvent; +use League\OAuth2\Server\RequestEvent; use League\OAuth2\Server\RequestTypes\AuthorizationRequest; use League\OAuth2\Server\ResponseTypes\RedirectResponse; use LeagueTests\Stubs\AccessTokenEntity; @@ -272,7 +274,22 @@ public function testCompleteAuthorizationRequest(): void $grant->setAccessTokenRepository($accessTokenRepositoryMock); $grant->setScopeRepository($scopeRepositoryMock); + $accessTokenEventEmitted = false; + + $grant->getListenerRegistry()->subscribeTo( + RequestEvent::ACCESS_TOKEN_ISSUED, + function ($event) use (&$accessTokenEventEmitted): void { + self::assertInstanceOf(RequestAccessTokenEvent::class, $event); + + $accessTokenEventEmitted = true; + } + ); + self::assertInstanceOf(RedirectResponse::class, $grant->completeAuthorizationRequest($authRequest)); + + if (!$accessTokenEventEmitted) { + // self::fail('Access token issued event is not emitted.'); // TODO: next major release + } } public function testCompleteAuthorizationRequestDenied(): void diff --git a/tests/Grant/PasswordGrantTest.php b/tests/Grant/PasswordGrantTest.php index 8c60a8c78..231d1505f 100644 --- a/tests/Grant/PasswordGrantTest.php +++ b/tests/Grant/PasswordGrantTest.php @@ -15,6 +15,9 @@ use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface; use League\OAuth2\Server\Repositories\ScopeRepositoryInterface; use League\OAuth2\Server\Repositories\UserRepositoryInterface; +use League\OAuth2\Server\RequestAccessTokenEvent; +use League\OAuth2\Server\RequestEvent; +use League\OAuth2\Server\RequestRefreshTokenEvent; use LeagueTests\Stubs\AccessTokenEntity; use LeagueTests\Stubs\ClientEntity; use LeagueTests\Stubs\RefreshTokenEntity; @@ -69,6 +72,27 @@ public function testRespondToRequest(): void $grant->setDefaultScope(self::DEFAULT_SCOPE); $grant->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key')); + $accessTokenEventEmitted = false; + $refreshTokenEventEmitted = false; + + $grant->getListenerRegistry()->subscribeTo( + RequestEvent::ACCESS_TOKEN_ISSUED, + function ($event) use (&$accessTokenEventEmitted): void { + self::assertInstanceOf(RequestAccessTokenEvent::class, $event); + + $accessTokenEventEmitted = true; + } + ); + + $grant->getListenerRegistry()->subscribeTo( + RequestEvent::REFRESH_TOKEN_ISSUED, + function ($event) use (&$refreshTokenEventEmitted): void { + self::assertInstanceOf(RequestRefreshTokenEvent::class, $event); + + $refreshTokenEventEmitted = true; + } + ); + $serverRequest = (new ServerRequest())->withParsedBody([ 'client_id' => 'foo', 'client_secret' => 'bar', @@ -80,6 +104,14 @@ public function testRespondToRequest(): void $grant->respondToAccessTokenRequest($serverRequest, $responseType, new DateInterval('PT5M')); self::assertInstanceOf(RefreshTokenEntityInterface::class, $responseType->getRefreshToken()); + + if (!$accessTokenEventEmitted) { + self::fail('Access token issued event is not emitted.'); + } + + if (!$refreshTokenEventEmitted) { + self::fail('Refresh token issued event is not emitted.'); + } } public function testRespondToRequestNullRefreshToken(): void diff --git a/tests/Grant/RefreshTokenGrantTest.php b/tests/Grant/RefreshTokenGrantTest.php index b2dbbadd2..34c8444c0 100644 --- a/tests/Grant/RefreshTokenGrantTest.php +++ b/tests/Grant/RefreshTokenGrantTest.php @@ -15,6 +15,9 @@ use League\OAuth2\Server\Repositories\ClientRepositoryInterface; use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface; use League\OAuth2\Server\Repositories\ScopeRepositoryInterface; +use League\OAuth2\Server\RequestAccessTokenEvent; +use League\OAuth2\Server\RequestEvent; +use League\OAuth2\Server\RequestRefreshTokenEvent; use League\OAuth2\Server\ResponseTypes\BearerTokenResponse; use LeagueTests\Stubs\AccessTokenEntity; use LeagueTests\Stubs\ClientEntity; @@ -76,6 +79,27 @@ public function testRespondToRequest(): void $grant->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key')); $grant->revokeRefreshTokens(true); + $accessTokenEventEmitted = false; + $refreshTokenEventEmitted = false; + + $grant->getListenerRegistry()->subscribeTo( + RequestEvent::ACCESS_TOKEN_ISSUED, + function ($event) use (&$accessTokenEventEmitted): void { + self::assertInstanceOf(RequestAccessTokenEvent::class, $event); + + $accessTokenEventEmitted = true; + } + ); + + $grant->getListenerRegistry()->subscribeTo( + RequestEvent::REFRESH_TOKEN_ISSUED, + function ($event) use (&$refreshTokenEventEmitted): void { + self::assertInstanceOf(RequestRefreshTokenEvent::class, $event); + + $refreshTokenEventEmitted = true; + } + ); + $oldRefreshToken = json_encode( [ 'client_id' => 'foo', @@ -106,6 +130,14 @@ public function testRespondToRequest(): void $grant->respondToAccessTokenRequest($serverRequest, $responseType, new DateInterval('PT5M')); self::assertInstanceOf(RefreshTokenEntityInterface::class, $responseType->getRefreshToken()); + + if (!$accessTokenEventEmitted) { + self::fail('Access token issued event is not emitted.'); + } + + if (!$refreshTokenEventEmitted) { + self::fail('Refresh token issued event is not emitted.'); + } } public function testRespondToRequestNullRefreshToken(): void