Skip to content

Commit

Permalink
Fix updating identitycontext in oauth2accesstokenhandler
Browse files Browse the repository at this point in the history
  • Loading branch information
ashanthamara committed Jan 26, 2025
1 parent f21bdb0 commit dc01b25
Showing 1 changed file with 27 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ protected AuthenticationResult doAuthenticate(MessageContext messageContext) {
OAuth2IntrospectionResponseDTO oAuth2IntrospectionResponseDTO =
oAuth2TokenValidationService.buildIntrospectionResponse(requestDTO);

setAuthenticatedEntityToThreadLocal(oAuth2IntrospectionResponseDTO);
IdentityUtil.threadLocalProperties.get()
.put(Constants.AUTHENTICATION_TYPE, oAuth2IntrospectionResponseDTO.getAut());

if (!oAuth2IntrospectionResponseDTO.isActive() ||
RefreshTokenValidator.TOKEN_TYPE_NAME.equals(oAuth2IntrospectionResponseDTO.getTokenType())) {
Expand All @@ -152,6 +153,7 @@ protected AuthenticationResult doAuthenticate(MessageContext messageContext) {
handleImpersonatedAccessToken(authenticationContext, accessToken, oAuth2IntrospectionResponseDTO);

authenticationResult.setAuthenticationStatus(AuthenticationStatus.SUCCESS);
setActorToIdentityContext(oAuth2IntrospectionResponseDTO);

Check warning on line 156 in components/org.wso2.carbon.identity.auth.service/src/main/java/org/wso2/carbon/identity/auth/service/handler/impl/OAuth2AccessTokenHandler.java

View check run for this annotation

Codecov / codecov/patch

components/org.wso2.carbon.identity.auth.service/src/main/java/org/wso2/carbon/identity/auth/service/handler/impl/OAuth2AccessTokenHandler.java#L156

Added line #L156 was not covered by tests

User authorizedUser = oAuth2IntrospectionResponseDTO.getAuthorizedUser();
String authorizedUserTenantDomain = null;
Expand Down Expand Up @@ -290,26 +292,33 @@ protected AuthenticationResult doAuthenticate(MessageContext messageContext) {
return authenticationResult;
}

private void setAuthenticatedEntityToThreadLocal(OAuth2IntrospectionResponseDTO introspectionResponseDTO) {
private void setActorToIdentityContext(OAuth2IntrospectionResponseDTO introspectionResponseDTO) {

String authenticatedEntity = introspectionResponseDTO.getAut();

Check warning on line 297 in components/org.wso2.carbon.identity.auth.service/src/main/java/org/wso2/carbon/identity/auth/service/handler/impl/OAuth2AccessTokenHandler.java

View check run for this annotation

Codecov / codecov/patch

components/org.wso2.carbon.identity.auth.service/src/main/java/org/wso2/carbon/identity/auth/service/handler/impl/OAuth2AccessTokenHandler.java#L297

Added line #L297 was not covered by tests
IdentityUtil.threadLocalProperties.get().put(Constants.AUTHENTICATION_TYPE, authenticatedEntity);
if (authenticatedEntity.equals(AUT_APPLICATION)) {
ApplicationActor actor = new ApplicationActor.Builder()
.authenticationType(ApplicationActor.AuthType.OAUTH2)
.entityId(introspectionResponseDTO.getClientId())
.build();
IdentityContext.getThreadLocalIdentityContext().setActor(actor);
if (authenticatedEntity == null) {
return;

Check warning on line 299 in components/org.wso2.carbon.identity.auth.service/src/main/java/org/wso2/carbon/identity/auth/service/handler/impl/OAuth2AccessTokenHandler.java

View check run for this annotation

Codecov / codecov/patch

components/org.wso2.carbon.identity.auth.service/src/main/java/org/wso2/carbon/identity/auth/service/handler/impl/OAuth2AccessTokenHandler.java#L299

Added line #L299 was not covered by tests
}
if (authenticatedEntity.equals(AUT_APPLICATION_USER)) {
UserActor.Builder userBuilder = new UserActor.Builder()
.username(introspectionResponseDTO.getAuthorizedUser().getUserName());
try {
userBuilder.userId(introspectionResponseDTO.getAuthorizedUser().getUserId());
} catch (UserIdNotFoundException e) {
log.warn("No userId found for the authenticated user.", e);
}
IdentityContext.getThreadLocalIdentityContext().setActor(userBuilder.build());

switch (authenticatedEntity) {
case AUT_APPLICATION:
ApplicationActor actor = new ApplicationActor.Builder()
.authenticationType(ApplicationActor.AuthType.OAUTH2)
.entityId(introspectionResponseDTO.getClientId())
.build();
IdentityContext.getThreadLocalIdentityContext().setActor(actor);
break;

Check warning on line 309 in components/org.wso2.carbon.identity.auth.service/src/main/java/org/wso2/carbon/identity/auth/service/handler/impl/OAuth2AccessTokenHandler.java

View check run for this annotation

Codecov / codecov/patch

components/org.wso2.carbon.identity.auth.service/src/main/java/org/wso2/carbon/identity/auth/service/handler/impl/OAuth2AccessTokenHandler.java#L304-L309

Added lines #L304 - L309 were not covered by tests
case AUT_APPLICATION_USER:
UserActor.Builder userBuilder = new UserActor.Builder()
.username(introspectionResponseDTO.getAuthorizedUser().getUserName());

Check warning on line 312 in components/org.wso2.carbon.identity.auth.service/src/main/java/org/wso2/carbon/identity/auth/service/handler/impl/OAuth2AccessTokenHandler.java

View check run for this annotation

Codecov / codecov/patch

components/org.wso2.carbon.identity.auth.service/src/main/java/org/wso2/carbon/identity/auth/service/handler/impl/OAuth2AccessTokenHandler.java#L311-L312

Added lines #L311 - L312 were not covered by tests
try {
userBuilder.userId(introspectionResponseDTO.getAuthorizedUser().getUserId());
} catch (UserIdNotFoundException e) {
log.warn("No userId found for the authenticated user.", e);
}
IdentityContext.getThreadLocalIdentityContext().setActor(userBuilder.build());
break;

Check warning on line 319 in components/org.wso2.carbon.identity.auth.service/src/main/java/org/wso2/carbon/identity/auth/service/handler/impl/OAuth2AccessTokenHandler.java

View check run for this annotation

Codecov / codecov/patch

components/org.wso2.carbon.identity.auth.service/src/main/java/org/wso2/carbon/identity/auth/service/handler/impl/OAuth2AccessTokenHandler.java#L314-L319

Added lines #L314 - L319 were not covered by tests
default:
break;
}
}

Check warning on line 323 in components/org.wso2.carbon.identity.auth.service/src/main/java/org/wso2/carbon/identity/auth/service/handler/impl/OAuth2AccessTokenHandler.java

View check run for this annotation

Codecov / codecov/patch

components/org.wso2.carbon.identity.auth.service/src/main/java/org/wso2/carbon/identity/auth/service/handler/impl/OAuth2AccessTokenHandler.java#L323

Added line #L323 was not covered by tests

Expand Down

0 comments on commit dc01b25

Please sign in to comment.