From 6700b113a8604f03101bc7935841a80863334c70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20B=C5=82aszczyk?= Date: Fri, 23 Feb 2018 17:48:51 +0100 Subject: [PATCH] Add new event types: access_token_issued and refresh_token_issued. --- src/Grant/AuthCodeGrant.php | 4 ++++ src/Grant/ClientCredentialsGrant.php | 4 ++++ src/Grant/PasswordGrant.php | 4 ++++ src/Grant/RefreshTokenGrant.php | 4 ++++ src/RequestEvent.php | 3 +++ 5 files changed, 19 insertions(+) diff --git a/src/Grant/AuthCodeGrant.php b/src/Grant/AuthCodeGrant.php index d1669b2f..20d5041b 100644 --- a/src/Grant/AuthCodeGrant.php +++ b/src/Grant/AuthCodeGrant.php @@ -176,6 +176,10 @@ class AuthCodeGrant extends AbstractAuthorizeGrant $accessToken = $this->issueAccessToken($accessTokenTTL, $client, $authCodePayload->user_id, $scopes); $refreshToken = $this->issueRefreshToken($accessToken); + // Send events to emitter + $this->getEmitter()->emit(new RequestEvent(RequestEvent::ACCESS_TOKEN_ISSUED, $request)); + $this->getEmitter()->emit(new RequestEvent(RequestEvent::REFRESH_TOKEN_ISSUED, $request)); + // Inject tokens into response type $responseType->setAccessToken($accessToken); $responseType->setRefreshToken($refreshToken); diff --git a/src/Grant/ClientCredentialsGrant.php b/src/Grant/ClientCredentialsGrant.php index ed157aaf..026ce5e5 100644 --- a/src/Grant/ClientCredentialsGrant.php +++ b/src/Grant/ClientCredentialsGrant.php @@ -11,6 +11,7 @@ namespace League\OAuth2\Server\Grant; +use League\OAuth2\Server\RequestEvent; use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface; use Psr\Http\Message\ServerRequestInterface; @@ -37,6 +38,9 @@ class ClientCredentialsGrant extends AbstractGrant // Issue and persist access token $accessToken = $this->issueAccessToken($accessTokenTTL, $client, null, $finalizedScopes); + // Send event to emitter + $this->getEmitter()->emit(new RequestEvent(RequestEvent::ACCESS_TOKEN_ISSUED, $request)); + // Inject access token into response type $responseType->setAccessToken($accessToken); diff --git a/src/Grant/PasswordGrant.php b/src/Grant/PasswordGrant.php index cfd7e9fe..1d00998b 100644 --- a/src/Grant/PasswordGrant.php +++ b/src/Grant/PasswordGrant.php @@ -59,6 +59,10 @@ class PasswordGrant extends AbstractGrant $accessToken = $this->issueAccessToken($accessTokenTTL, $client, $user->getIdentifier(), $finalizedScopes); $refreshToken = $this->issueRefreshToken($accessToken); + // Send events to emitter + $this->getEmitter()->emit(new RequestEvent(RequestEvent::ACCESS_TOKEN_ISSUED, $request)); + $this->getEmitter()->emit(new RequestEvent(RequestEvent::REFRESH_TOKEN_ISSUED, $request)); + // Inject tokens into response $responseType->setAccessToken($accessToken); $responseType->setRefreshToken($refreshToken); diff --git a/src/Grant/RefreshTokenGrant.php b/src/Grant/RefreshTokenGrant.php index f8e022b4..519954be 100644 --- a/src/Grant/RefreshTokenGrant.php +++ b/src/Grant/RefreshTokenGrant.php @@ -65,6 +65,10 @@ class RefreshTokenGrant extends AbstractGrant $accessToken = $this->issueAccessToken($accessTokenTTL, $client, $oldRefreshToken['user_id'], $scopes); $refreshToken = $this->issueRefreshToken($accessToken); + // Send events to emitter + $this->getEmitter()->emit(new RequestEvent(RequestEvent::ACCESS_TOKEN_ISSUED, $request)); + $this->getEmitter()->emit(new RequestEvent(RequestEvent::REFRESH_TOKEN_ISSUED, $request)); + // Inject tokens into response $responseType->setAccessToken($accessToken); $responseType->setRefreshToken($refreshToken); diff --git a/src/RequestEvent.php b/src/RequestEvent.php index 1558e11f..b1ca3f6b 100644 --- a/src/RequestEvent.php +++ b/src/RequestEvent.php @@ -18,6 +18,9 @@ class RequestEvent extends Event const USER_AUTHENTICATION_FAILED = 'user.authentication.failed'; const REFRESH_TOKEN_CLIENT_FAILED = 'refresh_token.client.failed'; + const REFRESH_TOKEN_ISSUED = 'refresh_token.issued'; + const ACCESS_TOKEN_ISSUED = 'access_token.issued'; + /** * @var ServerRequestInterface */