1818
1919package org .wso2 .carbon .identity .oauth .internal .util ;
2020
21+ import org .apache .commons .lang3 .StringUtils ;
2122import org .apache .commons .logging .Log ;
2223import org .apache .commons .logging .LogFactory ;
2324import org .wso2 .carbon .context .PrivilegedCarbonContext ;
2425import org .wso2 .carbon .identity .application .authentication .framework .exception .UserIdNotFoundException ;
2526import org .wso2 .carbon .identity .application .authentication .framework .model .AuthenticatedUser ;
2627import org .wso2 .carbon .identity .application .common .model .ServiceProvider ;
28+ import org .wso2 .carbon .identity .core .util .IdentityTenantUtil ;
2729import org .wso2 .carbon .identity .event .IdentityEventConstants ;
2830import org .wso2 .carbon .identity .event .IdentityEventException ;
2931import org .wso2 .carbon .identity .event .event .Event ;
32+ import org .wso2 .carbon .identity .oauth .common .OAuthConstants ;
3033import org .wso2 .carbon .identity .oauth .common .exception .InvalidOAuthClientException ;
3134import org .wso2 .carbon .identity .oauth .dao .OAuthAppDO ;
35+ import org .wso2 .carbon .identity .oauth .internal .OAuthComponentServiceHolder ;
3236import org .wso2 .carbon .identity .oauth2 .IdentityOAuth2Exception ;
3337import org .wso2 .carbon .identity .oauth2 .dto .OAuth2AccessTokenReqDTO ;
3438import org .wso2 .carbon .identity .oauth2 .internal .OAuth2ServiceComponentHolder ;
3539import org .wso2 .carbon .identity .oauth2 .model .AccessTokenDO ;
3640import org .wso2 .carbon .identity .oauth2 .token .OAuthTokenReqMessageContext ;
3741import org .wso2 .carbon .identity .oauth2 .token .OauthTokenIssuer ;
3842import org .wso2 .carbon .identity .oauth2 .util .OAuth2Util ;
43+ import org .wso2 .carbon .identity .openidconnect .OIDCConstants ;
3944import org .wso2 .carbon .identity .openidconnect .internal .OpenIDConnectServiceComponentHolder ;
45+ import org .wso2 .carbon .identity .organization .management .service .exception .OrganizationManagementException ;
4046
4147import java .util .Collections ;
4248import java .util .HashMap ;
4349import java .util .Map ;
4450import java .util .Set ;
4551
52+ import static org .wso2 .carbon .identity .openidconnect .OIDCConstants .Event .EXISTING_TOKEN_USED ;
53+
4654/**
4755 * Utility class for publishing OAuth related events.
4856 * This class provides methods to publish token revoke events with various parameters.
@@ -241,7 +249,7 @@ private static void publish(Map<String, Object> properties) {
241249 */
242250 public static void publishTokenIssueEvent (OAuthTokenReqMessageContext tokReqMsgCtx ,
243251 OAuth2AccessTokenReqDTO oAuth2AccessTokenReqDTO )
244- throws UserIdNotFoundException {
252+ throws UserIdNotFoundException , OrganizationManagementException , IdentityOAuth2Exception {
245253
246254 HashMap <String , Object > properties = new HashMap <>();
247255
@@ -259,6 +267,16 @@ public static void publishTokenIssueEvent(OAuthTokenReqMessageContext tokReqMsgC
259267 }
260268
261269 if (tokReqMsgCtx != null ) {
270+
271+ String issuerTenant = tokReqMsgCtx .getOauth2AccessTokenReqDTO ().getTenantDomain ();
272+ String issuerOrganizationId = OAuthComponentServiceHolder .getInstance ().getOrganizationManager ()
273+ .resolveOrganizationId (issuerTenant );
274+ String accessingOrganizationId = StringUtils .EMPTY ;
275+ if (tokReqMsgCtx .getAuthorizedUser () != null
276+ && tokReqMsgCtx .getAuthorizedUser ().getAccessingOrganization () != null ) {
277+ accessingOrganizationId = tokReqMsgCtx .getAuthorizedUser ().getAccessingOrganization ();
278+ }
279+
262280 if (tokReqMsgCtx .getAuthorizedUser () != null ) {
263281 properties .put (IdentityEventConstants .EventProperty .USER_ID ,
264282 tokReqMsgCtx .getAuthorizedUser ().getUserId ());
@@ -272,9 +290,24 @@ public static void publishTokenIssueEvent(OAuthTokenReqMessageContext tokReqMsgC
272290 tokReqMsgCtx .getAuthorizedUser ().getUserResidentOrganization ());
273291 }
274292
293+ properties .put (OIDCConstants .Event .USER_TYPE ,
294+ tokReqMsgCtx .getProperty (OAuthConstants .UserType .USER_TYPE ));
295+ properties .put (OIDCConstants .Event .CLIENT_ID ,
296+ tokReqMsgCtx .getOauth2AccessTokenReqDTO ().getClientId ());
297+ properties .put (OIDCConstants .Event .ISSUED_TIME ,
298+ String .valueOf (tokReqMsgCtx .getAccessTokenIssuedTime ()));
299+ properties .put (EXISTING_TOKEN_USED ,
300+ String .valueOf (existingTokenUsed (tokReqMsgCtx )));
301+ properties .put (OIDCConstants .Event .SERVICE_PROVIDER , OAuth2Util .getServiceProvider (
302+ tokReqMsgCtx .getOauth2AccessTokenReqDTO ().getClientId (), issuerTenant ).getApplicationName ());
303+ properties .put (OIDCConstants .Event .ISSUER_ORGANIZATION_ID , issuerOrganizationId );
304+ properties .put (OIDCConstants .Event .ACCESSING_ORGANIZATION_ID , accessingOrganizationId );
305+ properties .put (OIDCConstants .Event .TOKEN_ID , tokReqMsgCtx .getProperty (OIDCConstants .TOKEN_ID )); //review this
306+
275307 properties .put (IdentityEventConstants .EventProperty .IAT , tokReqMsgCtx .getAccessTokenIssuedTime ());
276308 properties .put (IdentityEventConstants .EventProperty .JTI , tokReqMsgCtx .getJWTID ());
277309 properties .put (IdentityEventConstants .EventProperty .GRANT_TYPE , oAuth2AccessTokenReqDTO .getGrantType ());
310+ properties .put (OIDCConstants .Event .APP_RESIDENT_TENANT_ID , IdentityTenantUtil .getLoginTenantId ());
278311
279312 if (tokReqMsgCtx .getProperty (APP_DAO ) != null &&
280313 tokReqMsgCtx .getProperty (APP_DAO ) instanceof OAuthAppDO ) {
@@ -298,4 +331,13 @@ public static void publishTokenIssueEvent(OAuthTokenReqMessageContext tokReqMsgC
298331 LOG .error ("Error occurred publishing event " + IdentityEventConstants .Event .POST_ISSUE_ACCESS_TOKEN_V2 , e );
299332 }
300333 }
334+
335+ private static Boolean existingTokenUsed (OAuthTokenReqMessageContext tokReqMsgCtx ) {
336+
337+ Boolean existingTokenUsed = (Boolean ) tokReqMsgCtx .getProperty (EXISTING_TOKEN_USED );
338+ if (existingTokenUsed == null ) {
339+ existingTokenUsed = false ;
340+ }
341+ return existingTokenUsed ;
342+ }
301343}
0 commit comments