Skip to content

Commit bf0c881

Browse files
Add PostTokenIssueEvent.Enable config and handle NPEs.
1 parent c435fb5 commit bf0c881

File tree

2 files changed

+59
-12
lines changed

2 files changed

+59
-12
lines changed

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

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ public class AccessTokenIssuer {
145145
public static final String OAUTH_APP_DO = "OAuthAppDO";
146146
private static final String SERVICE_PROVIDERS_SUB_CLAIM = "ServiceProviders.UseUsernameAsSubClaim";
147147
private final AuthorizationDetailsValidator authorizationDetailsValidator;
148+
private static final String ENABLE_POST_TOKEN_ISSUE_EVENT = "PostTokenIssueEvent.Enable";
148149

149150
/**
150151
* Private constructor which will not allow to create objects of this class from outside
@@ -1408,24 +1409,67 @@ private static void triggerPostIssueTokenEvent(OAuth2AccessTokenReqDTO tokenReqD
14081409
OAuthTokenReqMessageContext tokReqMsgCtx)
14091410
throws IdentityOAuth2Exception, OrganizationManagementException {
14101411

1411-
long issuedTimeMillis = tokReqMsgCtx.getAccessTokenIssuedTime();
1412-
String issuedTime = DateTimeFormatter.ISO_INSTANT.format(Instant.ofEpochMilli(issuedTimeMillis));
1412+
if (!Boolean.parseBoolean(IdentityUtil.getProperty(ENABLE_POST_TOKEN_ISSUE_EVENT))) {
1413+
if (log.isDebugEnabled()) {
1414+
log.debug("Token event publishing is disabled. Hence skipping the post issue token event.");
1415+
}
1416+
return;
1417+
}
1418+
if (tokenReqDTO == null || tokenRespDTO == null || tokReqMsgCtx == null) {
1419+
if (log.isDebugEnabled()) {
1420+
log.debug("Token request DTO, token response DTO or token request message context is null. " +
1421+
"Skipping the post issue token event.");
1422+
}
1423+
return;
1424+
}
1425+
if (tokenRespDTO.isError()) {
1426+
if (log.isDebugEnabled()) {
1427+
log.debug("Token response DTO is in error state. Hence skipping the post issue token event.");
1428+
}
1429+
return;
1430+
}
14131431
String userType = StringUtils.EMPTY;
1432+
String organizationId = StringUtils.EMPTY;
1433+
String tenantDomain = StringUtils.EMPTY;
1434+
String clientId = StringUtils.EMPTY;
1435+
String accessingOrganizationId = StringUtils.EMPTY;
1436+
String tokenId = StringUtils.EMPTY;
1437+
String grantType = StringUtils.EMPTY;
1438+
String issuedTime;
1439+
1440+
issuedTime = DateTimeFormatter.ISO_INSTANT.format(
1441+
Instant.ofEpochMilli(tokReqMsgCtx.getAccessTokenIssuedTime()));
14141442
Object userTypeObject = tokReqMsgCtx.getProperty(OAuthConstants.UserType.USER_TYPE);
14151443
if (userTypeObject instanceof String) {
14161444
userType = (String) userTypeObject;
14171445
}
1418-
String organizationId = OAuthComponentServiceHolder.getInstance().getOrganizationManager()
1419-
.resolveOrganizationId(tokReqMsgCtx.getOauth2AccessTokenReqDTO().getTenantDomain());
1420-
String accessingOrganizationId = tokReqMsgCtx.getAuthorizedUser().getAccessingOrganization();
1446+
if (tokReqMsgCtx.getOauth2AccessTokenReqDTO() != null &&
1447+
tokReqMsgCtx.getOauth2AccessTokenReqDTO().getTenantDomain() != null) {
1448+
organizationId = OAuthComponentServiceHolder.getInstance().getOrganizationManager()
1449+
.resolveOrganizationId(tokReqMsgCtx.getOauth2AccessTokenReqDTO().getTenantDomain());
1450+
}
1451+
if (tokReqMsgCtx.getOauth2AccessTokenReqDTO().getTenantDomain() != null) {
1452+
tenantDomain = tokReqMsgCtx.getOauth2AccessTokenReqDTO().getTenantDomain();
1453+
}
1454+
if (tokReqMsgCtx.getOauth2AccessTokenReqDTO().getClientId() != null) {
1455+
clientId = tokReqMsgCtx.getOauth2AccessTokenReqDTO().getClientId();
1456+
}
1457+
if (tokReqMsgCtx.getAuthorizedUser() != null) {
1458+
accessingOrganizationId = tokReqMsgCtx.getAuthorizedUser().getAccessingOrganization();
1459+
}
1460+
if (tokenRespDTO.getTokenId() != null) {
1461+
tokenId = tokenRespDTO.getTokenId();
1462+
}
1463+
if (tokenReqDTO.getGrantType() != null) {
1464+
grantType = tokenReqDTO.getGrantType();
1465+
}
14211466
if (!existingTokenUsed(tokReqMsgCtx)) {
14221467
Map<String, Object> eventProperties = new HashMap<>();
1423-
eventProperties.put(OIDCConstants.Event.TOKEN_ID, tokenRespDTO.getTokenId());
1424-
eventProperties.put(OIDCConstants.Event.TENANT_DOMAIN,
1425-
tokReqMsgCtx.getOauth2AccessTokenReqDTO().getTenantDomain());
1468+
eventProperties.put(OIDCConstants.Event.TOKEN_ID, tokenId);
1469+
eventProperties.put(OIDCConstants.Event.TENANT_DOMAIN, tenantDomain);
14261470
eventProperties.put(OIDCConstants.Event.USER_TYPE, userType);
1427-
eventProperties.put(OIDCConstants.Event.CLIENT_ID, tokReqMsgCtx.getOauth2AccessTokenReqDTO().getClientId());
1428-
eventProperties.put(OIDCConstants.Event.GRANT_TYPE, tokenReqDTO.getGrantType());
1471+
eventProperties.put(OIDCConstants.Event.CLIENT_ID, clientId);
1472+
eventProperties.put(OIDCConstants.Event.GRANT_TYPE, grantType);
14291473
eventProperties.put(OIDCConstants.Event.ISSUED_TIME, issuedTime);
14301474
eventProperties.put(OIDCConstants.Event.ISSUER_ORGANIZATION_ID, organizationId);
14311475
eventProperties.put(OIDCConstants.Event.ACCESSING_ORGANIZATION_ID, accessingOrganizationId);

components/org.wso2.carbon.identity.oauth/src/test/java/org/wso2/carbon/identity/oauth2/token/AccessTokenIssuerTest.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.wso2.carbon.identity.common.testng.WithCarbonHome;
4040
import org.wso2.carbon.identity.core.util.IdentityConfigParser;
4141
import org.wso2.carbon.identity.core.util.IdentityTenantUtil;
42+
import org.wso2.carbon.identity.core.util.IdentityUtil;
4243
import org.wso2.carbon.identity.oauth.cache.AppInfoCache;
4344
import org.wso2.carbon.identity.oauth.cache.OAuthCache;
4445
import org.wso2.carbon.identity.oauth.common.OAuthConstants;
@@ -104,6 +105,7 @@ public class AccessTokenIssuerTest {
104105
private final String testTenantDomain = "carbon.super";
105106
private final String testClientId = "dExLASaD1Flb_fx7ZecfAA3n1HRka";
106107
private final String testOrganizationId = "exLASaD1Flb_fx7ZecfAA3n1HRkaf";
108+
private static final String ENABLE_POST_TOKEN_ISSUE_EVENT = "PostTokenIssueEvent.Enable";
107109

108110
@AfterClass
109111
public void cleanUp() throws Exception {
@@ -178,7 +180,8 @@ public void testTriggerPostIssueTokenEvent(OAuth2AccessTokenReqDTO dto) throws I
178180
MockedStatic<IdentityTenantUtil> identityTenantUtil = mockStatic(IdentityTenantUtil.class);
179181
MockedStatic<AuthzUtil> authzUtil = mockStatic(AuthzUtil.class);
180182
MockedStatic<OAuthCache> oAuthCache = mockStatic(OAuthCache.class);
181-
MockedStatic<OAuth2TokenUtil> oAuth2TokenUtil = mockStatic(OAuth2TokenUtil.class)
183+
MockedStatic<OAuth2TokenUtil> oAuth2TokenUtil = mockStatic(OAuth2TokenUtil.class);
184+
MockedStatic<IdentityUtil> identityUtil = mockStatic(IdentityUtil.class)
182185
) {
183186
OAuth2AccessTokenRespDTO tokenResp = mock(OAuth2AccessTokenRespDTO.class);
184187

@@ -256,7 +259,7 @@ public void testTriggerPostIssueTokenEvent(OAuth2AccessTokenReqDTO dto) throws I
256259
identityTenantUtil.when(() -> IdentityTenantUtil.getRealm(any(), any())).thenReturn(userRealm);
257260

258261
authzUtil.when(AuthzUtil::isLegacyAuthzRuntime).thenReturn(false);
259-
262+
identityUtil.when(() -> IdentityUtil.getProperty(ENABLE_POST_TOKEN_ISSUE_EVENT)).thenReturn("true");
260263
AccessTokenIssuer.getInstance().issue(dto);
261264

262265
oAuth2TokenUtil.verify(() -> OAuth2TokenUtil.postIssueToken(

0 commit comments

Comments
 (0)