|
25 | 25 | import org.apache.commons.logging.Log;
|
26 | 26 | import org.apache.commons.logging.LogFactory;
|
27 | 27 | import org.apache.oltu.oauth2.common.exception.OAuthSystemException;
|
| 28 | +import org.wso2.carbon.CarbonConstants; |
28 | 29 | import org.wso2.carbon.base.MultitenantConstants;
|
29 | 30 | import org.wso2.carbon.identity.application.authentication.framework.exception.FrameworkException;
|
30 | 31 | import org.wso2.carbon.identity.application.authentication.framework.handler.approles.ApplicationRolesResolver;
|
@@ -566,15 +567,30 @@ private Map<String, Object> getUserClaimsInOIDCDialect(String spTenantDomain,
|
566 | 567 | // Improve runtime claim value storage in cache through https://github.com/wso2/product-is/issues/15056
|
567 | 568 | requestedClaimUris.removeIf(claim -> claim.startsWith("http://wso2.org/claims/runtime/"));
|
568 | 569 |
|
569 |
| - boolean requestedAppRoleClaim = false; |
| 570 | + boolean roleClaimRequested = false; |
| 571 | + String rolesClaimURI = IdentityUtil.getLocalGroupsClaimURI(); |
| 572 | + if (requestedClaimUris.contains(rolesClaimURI) && !CarbonConstants.ENABLE_LEGACY_AUTHZ_RUNTIME) { |
| 573 | + requestedClaimUris.remove(rolesClaimURI); |
| 574 | + roleClaimRequested = true; |
| 575 | + } |
| 576 | + boolean appRoleClaimRequested = false; |
570 | 577 | if (requestedClaimUris.contains(APP_ROLES_CLAIM)) {
|
571 | 578 | requestedClaimUris.remove(APP_ROLES_CLAIM);
|
572 |
| - requestedAppRoleClaim = true; |
| 579 | + appRoleClaimRequested = true; |
573 | 580 | }
|
574 | 581 | Map<String, String> userClaims = getUserClaimsInLocalDialect(fullQualifiedUsername, realm, requestedClaimUris);
|
575 |
| - if (requestedAppRoleClaim) { |
576 |
| - handleAppRoleClaimInLocalDialect(userClaims, authenticatedUser, serviceProvider.getApplicationResourceId()); |
| 582 | + |
| 583 | + if (roleClaimRequested || appRoleClaimRequested) { |
| 584 | + String[] appAssocatedRolesOfUser = getAppAssociatedRolesOfUser(authenticatedUser, |
| 585 | + serviceProvider.getApplicationResourceId()); |
| 586 | + if (roleClaimRequested) { |
| 587 | + setRoleClaimInLocalDialect(userClaims, appAssocatedRolesOfUser); |
| 588 | + } |
| 589 | + if (appRoleClaimRequested) { |
| 590 | + setAppRoleClaimInLocalDialect(userClaims, appAssocatedRolesOfUser); |
| 591 | + } |
577 | 592 | }
|
| 593 | + |
578 | 594 | if (isEmpty(userClaims)) {
|
579 | 595 | // User claims can be empty if user does not exist in user stores. Probably a federated user.
|
580 | 596 | if (log.isDebugEnabled()) {
|
@@ -651,25 +667,51 @@ private String resolveUserIdForOrganizationSsoUser(AuthenticatedUser authenticat
|
651 | 667 | }
|
652 | 668 |
|
653 | 669 | /**
|
654 |
| - * Adds the application roles claim for local user. |
| 670 | + * Get app associated roles of the user. |
655 | 671 | *
|
656 |
| - * @param userClaims User claims in local dialect. |
657 | 672 | * @param authenticatedUser Authenticated user.
|
658 |
| - * @param applicationId Application ID. |
659 |
| - * @throws ApplicationRolesException Error while getting application roles. |
| 673 | + * @param applicationId Application id. |
| 674 | + * @return App associated roles of the user. |
| 675 | + * @throws ApplicationRolesException If an error occurred while getting app associated roles. |
660 | 676 | */
|
661 |
| - private void handleAppRoleClaimInLocalDialect(Map<String, String> userClaims, AuthenticatedUser authenticatedUser, |
662 |
| - String applicationId) throws ApplicationRolesException { |
| 677 | + private String[] getAppAssociatedRolesOfUser(AuthenticatedUser authenticatedUser, String applicationId) throws |
| 678 | + ApplicationRolesException { |
663 | 679 |
|
664 | 680 | ApplicationRolesResolver appRolesResolver =
|
665 | 681 | OpenIDConnectServiceComponentHolder.getInstance().getHighestPriorityApplicationRolesResolver();
|
666 | 682 | if (appRolesResolver == null) {
|
667 | 683 | log.debug("No application roles resolver found. So not adding application roles claim to the id_token.");
|
668 |
| - return; |
| 684 | + return new String[0]; |
| 685 | + } |
| 686 | + return appRolesResolver.getRoles(authenticatedUser, applicationId); |
| 687 | + } |
| 688 | + |
| 689 | + /** |
| 690 | + * Set the roles claim for local user. |
| 691 | + * |
| 692 | + * @param userClaims User claims in local dialect. |
| 693 | + * @param appAssociatedRoles App associated roles of the user. |
| 694 | + */ |
| 695 | + private void setRoleClaimInLocalDialect(Map<String, String> userClaims, String[] appAssociatedRoles) { |
| 696 | + |
| 697 | + String rolesClaimURI = IdentityUtil.getLocalGroupsClaimURI(); |
| 698 | + if (ArrayUtils.isNotEmpty(appAssociatedRoles)) { |
| 699 | + userClaims.put(rolesClaimURI, |
| 700 | + String.join(FrameworkUtils.getMultiAttributeSeparator(), appAssociatedRoles)); |
669 | 701 | }
|
670 |
| - String[] appRoles = appRolesResolver.getRoles(authenticatedUser, applicationId); |
671 |
| - if (ArrayUtils.isNotEmpty(appRoles)) { |
672 |
| - userClaims.put(APP_ROLES_CLAIM, String.join(FrameworkUtils.getMultiAttributeSeparator(), appRoles)); |
| 702 | + } |
| 703 | + |
| 704 | + /** |
| 705 | + * Set the application roles claim for local user. |
| 706 | + * |
| 707 | + * @param userClaims User claims in local dialect. |
| 708 | + * @param appAssociatedRoles App associated roles of the user. |
| 709 | + */ |
| 710 | + private void setAppRoleClaimInLocalDialect(Map<String, String> userClaims, String[] appAssociatedRoles) { |
| 711 | + |
| 712 | + if (ArrayUtils.isNotEmpty(appAssociatedRoles)) { |
| 713 | + userClaims.put(APP_ROLES_CLAIM, |
| 714 | + String.join(FrameworkUtils.getMultiAttributeSeparator(), appAssociatedRoles)); |
673 | 715 | }
|
674 | 716 | }
|
675 | 717 |
|
|
0 commit comments