Skip to content

Commit 668d48a

Browse files
Add additional event properties to POST_ISSUE_ACCESS_TOKEN_V2 event.
1 parent d403cf3 commit 668d48a

File tree

5 files changed

+107
-5
lines changed

5 files changed

+107
-5
lines changed

components/org.wso2.carbon.identity.oauth/src/main/java/org/wso2/carbon/identity/oauth/internal/util/AccessTokenEventUtil.java

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,39 @@
1818

1919
package org.wso2.carbon.identity.oauth.internal.util;
2020

21+
import org.apache.commons.lang3.StringUtils;
2122
import org.apache.commons.logging.Log;
2223
import org.apache.commons.logging.LogFactory;
2324
import org.wso2.carbon.context.PrivilegedCarbonContext;
2425
import org.wso2.carbon.identity.application.authentication.framework.exception.UserIdNotFoundException;
2526
import org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser;
2627
import org.wso2.carbon.identity.application.common.model.ServiceProvider;
28+
import org.wso2.carbon.identity.core.util.IdentityTenantUtil;
2729
import org.wso2.carbon.identity.event.IdentityEventConstants;
2830
import org.wso2.carbon.identity.event.IdentityEventException;
2931
import org.wso2.carbon.identity.event.event.Event;
32+
import org.wso2.carbon.identity.oauth.common.OAuthConstants;
3033
import org.wso2.carbon.identity.oauth.common.exception.InvalidOAuthClientException;
3134
import org.wso2.carbon.identity.oauth.dao.OAuthAppDO;
35+
import org.wso2.carbon.identity.oauth.internal.OAuthComponentServiceHolder;
3236
import org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception;
3337
import org.wso2.carbon.identity.oauth2.dto.OAuth2AccessTokenReqDTO;
3438
import org.wso2.carbon.identity.oauth2.internal.OAuth2ServiceComponentHolder;
3539
import org.wso2.carbon.identity.oauth2.model.AccessTokenDO;
3640
import org.wso2.carbon.identity.oauth2.token.OAuthTokenReqMessageContext;
3741
import org.wso2.carbon.identity.oauth2.token.OauthTokenIssuer;
3842
import org.wso2.carbon.identity.oauth2.util.OAuth2Util;
43+
import org.wso2.carbon.identity.openidconnect.OIDCConstants;
3944
import org.wso2.carbon.identity.openidconnect.internal.OpenIDConnectServiceComponentHolder;
45+
import org.wso2.carbon.identity.organization.management.service.exception.OrganizationManagementException;
4046

4147
import java.util.Collections;
4248
import java.util.HashMap;
4349
import java.util.Map;
4450
import 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
}

components/org.wso2.carbon.identity.oauth/src/main/java/org/wso2/carbon/identity/oauth2/token/AccessTokenIssuer.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,11 @@ public OAuth2AccessTokenRespDTO issue(OAuth2AccessTokenReqDTO tokenReqDTO)
406406
}
407407

408408
if (tokenRespDTO != null && !tokenRespDTO.isError() && tokenRespDTO.getAccessToken() != null) {
409-
AccessTokenEventUtil.publishTokenIssueEvent(tokReqMsgCtx, tokenReqDTO);
409+
try {
410+
AccessTokenEventUtil.publishTokenIssueEvent(tokReqMsgCtx, tokenReqDTO);
411+
} catch (OrganizationManagementException | IdentityOAuth2Exception | UserIdNotFoundException e) {
412+
log.error("Error while publishing POST_ISSUE_ACCESS_TOKEN_V2 event. Event not published.", e);
413+
}
410414
}
411415

412416
return tokenRespDTO;

components/org.wso2.carbon.identity.oauth/src/main/java/org/wso2/carbon/identity/oauth2/token/handlers/grant/AbstractAuthorizationGrantHandler.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
import org.wso2.carbon.identity.oauth2.validators.OAuth2ScopeHandler;
7171
import org.wso2.carbon.identity.oauth2.validators.scope.ScopeValidator;
7272
import org.wso2.carbon.identity.openidconnect.OIDCClaimUtil;
73+
import org.wso2.carbon.identity.openidconnect.OIDCConstants;
7374
import org.wso2.carbon.identity.organization.management.service.exception.OrganizationManagementException;
7475
import org.wso2.carbon.utils.DiagnosticLog;
7576

@@ -251,6 +252,7 @@ private void setDetailsToMessageContext(OAuthTokenReqMessageContext tokReqMsgCtx
251252
}
252253

253254
tokReqMsgCtx.setRefreshTokenvalidityPeriod(existingToken.getRefreshTokenValidityPeriodInMillis());
255+
tokReqMsgCtx.addProperty(OIDCConstants.TOKEN_ID, existingToken.getTokenId());
254256
}
255257

256258
@Override
@@ -501,6 +503,7 @@ private OAuth2AccessTokenRespDTO generateNewAccessToken(OAuthTokenReqMessageCont
501503

502504
// Update cache with newly added token.
503505
updateCacheIfEnabled(newTokenBean, OAuth2Util.buildScopeString(tokReqMsgCtx.getScope()), oauthTokenIssuer);
506+
tokReqMsgCtx.addProperty(OIDCConstants.TOKEN_ID, existingTokenBean.getTokenId());
504507
return createResponseWithTokenBean(newTokenBean, newTokenBean.getValidityPeriodInMillis(), scope);
505508
}
506509

components/org.wso2.carbon.identity.oauth/src/main/java/org/wso2/carbon/identity/openidconnect/OIDCConstants.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public class OIDCConstants {
2828
public static final String IDN_OIDC_REQ_OBJECT_CLAIMS = "STORE_IDN_OIDC_REQ_OBJECT_CLAIMS";
2929
public static final String HAS_NON_OIDC_CLAIMS = "hasNonOIDCClaims";
3030
public static final String ID_TOKEN_USER_CLAIMS_PROP_KEY = "IDTokenUserClaims";
31+
public static final String TOKEN_ID = "TOKEN_ID";
3132

3233
/**
3334
* This class is used to define constants related to OIDC event specific features.
@@ -51,6 +52,14 @@ public class Event {
5152
public static final String OLD_ACCESS_TOKEN = "OLD_ACCESS_TOKEN";
5253
public static final String POST_REFRESH_TOKEN = "POST_REFRESH_TOKEN";
5354
public static final String IS_REQUEST_OBJECT_FLOW = "IS_REQUEST_OBJECT_FLOW";
55+
public static final String CLIENT_ID = "CLIENT_ID";
56+
public static final String USER_TYPE = "USER_TYPE";
57+
public static final String APP_RESIDENT_TENANT_ID = "APP_RESIDENT_TENANT_ID";
58+
public static final String ISSUED_TIME = "ISSUED_TIME";
59+
public static final String ISSUER_ORGANIZATION_ID = "ISSUER_ORGANIZATION_ID";
60+
public static final String ACCESSING_ORGANIZATION_ID = "ACCESSING_ORGANIZATION_ID";
61+
public static final String EXISTING_TOKEN_USED = "EXISTING_TOKEN_USED";
62+
public static final String SERVICE_PROVIDER = "SERVICE_PROVIDER";
5463
}
5564
}
5665

components/org.wso2.carbon.identity.oauth/src/test/java/org/wso2/carbon/identity/oauth2/util/OAuthEventPublishingUtilTest.java

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,26 @@
2929
import org.wso2.carbon.context.PrivilegedCarbonContext;
3030
import org.wso2.carbon.identity.application.authentication.framework.exception.UserIdNotFoundException;
3131
import org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser;
32+
import org.wso2.carbon.identity.application.common.IdentityApplicationManagementException;
33+
import org.wso2.carbon.identity.application.common.model.LocalAndOutboundAuthenticationConfig;
34+
import org.wso2.carbon.identity.application.common.model.ServiceProvider;
3235
import org.wso2.carbon.identity.common.testng.WithCarbonHome;
36+
import org.wso2.carbon.identity.core.util.IdentityTenantUtil;
3337
import org.wso2.carbon.identity.event.IdentityEventConstants;
3438
import org.wso2.carbon.identity.event.IdentityEventException;
3539
import org.wso2.carbon.identity.event.event.Event;
3640
import org.wso2.carbon.identity.event.services.IdentityEventService;
41+
import org.wso2.carbon.identity.oauth.common.OAuthConstants;
3742
import org.wso2.carbon.identity.oauth.dao.OAuthAppDO;
43+
import org.wso2.carbon.identity.oauth.internal.OAuthComponentServiceHolder;
3844
import org.wso2.carbon.identity.oauth.internal.util.AccessTokenEventUtil;
45+
import org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception;
3946
import org.wso2.carbon.identity.oauth2.dto.OAuth2AccessTokenReqDTO;
4047
import org.wso2.carbon.identity.oauth2.internal.OAuth2ServiceComponentHolder;
4148
import org.wso2.carbon.identity.oauth2.token.OAuthTokenReqMessageContext;
4249
import org.wso2.carbon.identity.oauth2.token.OauthTokenIssuer;
50+
import org.wso2.carbon.identity.organization.management.service.OrganizationManager;
51+
import org.wso2.carbon.identity.organization.management.service.exception.OrganizationManagementException;
4352

4453
import java.util.Map;
4554

@@ -49,11 +58,14 @@
4958
import static org.mockito.Mockito.verify;
5059
import static org.mockito.Mockito.when;
5160
import static org.mockito.MockitoAnnotations.openMocks;
61+
import static org.wso2.carbon.identity.openidconnect.OIDCConstants.Event.EXISTING_TOKEN_USED;
5262

5363
@Listeners(MockitoTestNGListener.class)
5464
@WithCarbonHome
5565
public class OAuthEventPublishingUtilTest {
5666

67+
private int TEST_APP_RESIDENT_TENANT_ID = 11;
68+
5769
@Mock
5870
OAuthTokenReqMessageContext tokReqMsgCtx;
5971

@@ -72,12 +84,25 @@ public class OAuthEventPublishingUtilTest {
7284
@Mock
7385
IdentityEventService identityEventService;
7486

87+
@Mock
88+
LocalAndOutboundAuthenticationConfig localAndOutboundAuthenticationConfig;
89+
90+
@Mock
91+
ServiceProvider sp;
92+
93+
@Mock
94+
OAuthComponentServiceHolder oAuthComponentServiceHolder;
95+
96+
@Mock
97+
OrganizationManager organizationManager;
98+
7599
@BeforeMethod
76-
public void setUp() throws UserIdNotFoundException {
100+
public void setUp() throws UserIdNotFoundException, IdentityApplicationManagementException, OrganizationManagementException {
77101

78102
openMocks(this);
79103
when(oAuth2AccessTokenReqDTO.getClientId()).thenReturn("test-client-id");
80104
when(oAuth2AccessTokenReqDTO.getGrantType()).thenReturn("authorization_code");
105+
when(oAuth2AccessTokenReqDTO.getTenantDomain()).thenReturn("issuer-tenant-domain");
81106

82107
when(tokReqMsgCtx.getAuthorizedUser()).thenReturn(authorizedUser);
83108
when(authorizedUser.getUserId()).thenReturn("user-id");
@@ -93,24 +118,43 @@ public void setUp() throws UserIdNotFoundException {
93118
when(oAuthAppDO.getOauthConsumerKey()).thenReturn("test-client-id");
94119

95120
when(tokenIssuer.getAccessTokenType()).thenReturn("Opaque");
121+
122+
when(tokReqMsgCtx.getProperty(OAuthConstants.UserType.USER_TYPE)).thenReturn("APPLICATION_USER");
123+
when(tokReqMsgCtx.getOauth2AccessTokenReqDTO()).thenReturn(oAuth2AccessTokenReqDTO);
124+
when(tokReqMsgCtx.getProperty(EXISTING_TOKEN_USED)).thenReturn(Boolean.FALSE);
125+
126+
when(oAuthComponentServiceHolder.getOrganizationManager()).thenReturn(organizationManager);
127+
when(organizationManager.resolveOrganizationId(anyString())).thenReturn("test-org-id");
96128
}
97129

98130
@Test
99-
public void testPublishTokenIssueEvent() throws UserIdNotFoundException, IdentityEventException {
131+
public void testPublishTokenIssueEvent() throws UserIdNotFoundException, IdentityEventException,
132+
IdentityOAuth2Exception, OrganizationManagementException {
100133

101134
try (MockedStatic<OAuth2Util> mockedOAuth2Util = Mockito.mockStatic(OAuth2Util.class);
102135
MockedStatic<OAuth2ServiceComponentHolder> mockedServiceHolder = Mockito.mockStatic(
103136
OAuth2ServiceComponentHolder.class);
104137
MockedStatic<PrivilegedCarbonContext> mockedCarbonContext = Mockito.mockStatic(
105-
PrivilegedCarbonContext.class)) {
138+
PrivilegedCarbonContext.class);
139+
MockedStatic<OAuthComponentServiceHolder> mockedOAuthComponentServiceHolder = Mockito.mockStatic(
140+
OAuthComponentServiceHolder.class);
141+
MockedStatic<IdentityTenantUtil> mockedIdentityTenantUtil = Mockito.mockStatic(
142+
IdentityTenantUtil.class)
143+
) {
106144

107145
mockedOAuth2Util.when(() -> OAuth2Util.getOAuthTokenIssuerForOAuthApp(anyString()))
108146
.thenReturn(tokenIssuer);
147+
mockedOAuth2Util.when(() -> OAuth2Util.getServiceProvider(anyString(), anyString()))
148+
.thenReturn(sp);
109149
when(tokenIssuer.getAccessTokenType()).thenReturn("Opaque");
110150

111151
mockedServiceHolder.when(OAuth2ServiceComponentHolder::getIdentityEventService)
112152
.thenReturn(identityEventService);
113153

154+
mockedOAuthComponentServiceHolder.when(OAuthComponentServiceHolder::getInstance)
155+
.thenReturn(oAuthComponentServiceHolder);
156+
mockedIdentityTenantUtil.when(IdentityTenantUtil::getLoginTenantId).thenReturn(TEST_APP_RESIDENT_TENANT_ID);
157+
114158
PrivilegedCarbonContext carbonContext = mock(PrivilegedCarbonContext.class);
115159
mockedCarbonContext.when(PrivilegedCarbonContext::getThreadLocalCarbonContext)
116160
.thenReturn(carbonContext);

0 commit comments

Comments
 (0)