Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve claim provider according to the organization bound token improvements #240

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.scr.ds-annotations</artifactId>
</dependency>
<dependency>
<groupId>org.wso2.carbon.identity.framework</groupId>
<artifactId>org.wso2.carbon.identity.application.authentication.framework</artifactId>
</dependency>
<dependency>
<groupId>org.wso2.carbon.identity.inbound.auth.oauth2</groupId>
<artifactId>org.wso2.carbon.identity.oauth</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,69 +41,94 @@
*/
public class OrganizationClaimProvider implements ClaimProvider, JWTAccessTokenClaimProvider {

private static final String ORGANIZATION_ID_ATTRIBUTE = "org_id";
private static final String ORGANIZATION_NAME_ATTRIBUTE = "org_name";
private static final String AUTHORIZED_ORGANIZATION_ID_ATTRIBUTE = "org_id";
private static final String AUTHORIZED_ORGANIZATION_NAME_ATTRIBUTE = "org_name";
private static final String USER_RESIDENT_ORGANIZATION_NAME_ATTRIBUTE = "user_organization";

@Override
public Map<String, Object> getAdditionalClaims(OAuthAuthzReqMessageContext oAuthAuthzReqMessageContext,
OAuth2AuthorizeRespDTO oAuth2AuthorizeRespDTO)
throws IdentityOAuth2Exception {

String tenantDomain = oAuthAuthzReqMessageContext.getAuthorizationReqDTO().getLoggedInTenantDomain();
return getOrganizationInformation(tenantDomain);
String organizationId = resolveOrganizationId(tenantDomain);
return setOrganizationInformation(organizationId, organizationId);
}

@Override
public Map<String, Object> getAdditionalClaims(OAuthTokenReqMessageContext oAuthTokenReqMessageContext,
OAuth2AccessTokenRespDTO oAuth2AccessTokenRespDTO)
throws IdentityOAuth2Exception {

String tenantDomain = oAuthTokenReqMessageContext.getAuthorizedUser().getTenantDomain();
return getOrganizationInformation(tenantDomain);
}

private OrganizationManager getOrganizationManager() {

return OrganizationClaimProviderServiceComponentHolder.getInstance().getOrganizationManager();
String userResidentOrganization = oAuthTokenReqMessageContext.getAuthorizedUser().getUserResidentOrganization();
String accessingOrganization = oAuthTokenReqMessageContext.getAuthorizedUser().getAccessingOrganization();
if (StringUtils.isEmpty(accessingOrganization)) {
accessingOrganization =
resolveOrganizationId(oAuthTokenReqMessageContext.getAuthorizedUser().getTenantDomain());
}
return setOrganizationInformation(userResidentOrganization, accessingOrganization);
}

@Override
public Map<String, Object> getAdditionalClaims(OAuthAuthzReqMessageContext oAuthAuthzReqMessageContext)
throws IdentityOAuth2Exception {

String tenantDomain = oAuthAuthzReqMessageContext.getAuthorizationReqDTO().getLoggedInTenantDomain();
return getOrganizationInformation(tenantDomain);
String organizationId = resolveOrganizationId(tenantDomain);
return setOrganizationInformation(organizationId, organizationId);
}

@Override
public Map<String, Object> getAdditionalClaims(OAuthTokenReqMessageContext oAuthTokenReqMessageContext)
throws IdentityOAuth2Exception {

String tenantDomain = oAuthTokenReqMessageContext.getAuthorizedUser().getTenantDomain();
return getOrganizationInformation(tenantDomain);
String userResidentOrganization = oAuthTokenReqMessageContext.getAuthorizedUser().getUserResidentOrganization();
String authorizedOrganization = oAuthTokenReqMessageContext.getAuthorizedUser().getAccessingOrganization();
if (StringUtils.isEmpty(authorizedOrganization)) {
authorizedOrganization =
resolveOrganizationId(oAuthTokenReqMessageContext.getAuthorizedUser().getTenantDomain());
}
return setOrganizationInformation(userResidentOrganization, authorizedOrganization);
}

private Map<String, Object> getOrganizationInformation(String tenantDomain) throws IdentityOAuth2Exception {
private Map<String, Object> setOrganizationInformation(String userResidentOrganization,
String authorizedOrganization)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private Map<String, Object> setOrganizationInformation(String userResidentOrganization,
String authorizedOrganization)
private Map<String, Object> buildOrganizationInformation(String userResidentOrganizationId,
String authorizedOrganizationId)

throws IdentityOAuth2Exception {

Map<String, Object> additionalClaims = new HashMap<>();
if (!OrganizationClaimProviderServiceComponentHolder.getInstance().isOrganizationManagementEnable()) {
return additionalClaims;
}
try {
String organizationId = getOrganizationManager().resolveOrganizationId(tenantDomain);
if (StringUtils.isNotBlank(organizationId)) {
String organizationName = getOrganizationManager().getOrganizationNameById(organizationId);
additionalClaims.put(ORGANIZATION_ID_ATTRIBUTE, organizationId);
additionalClaims.put(ORGANIZATION_NAME_ATTRIBUTE, organizationName);
if (StringUtils.isNotBlank(authorizedOrganization)) {
String authorizedOrganizationName =
getOrganizationManager().getOrganizationNameById(authorizedOrganization);
additionalClaims.put(USER_RESIDENT_ORGANIZATION_NAME_ATTRIBUTE, userResidentOrganization);
additionalClaims.put(AUTHORIZED_ORGANIZATION_ID_ATTRIBUTE, authorizedOrganization);
additionalClaims.put(AUTHORIZED_ORGANIZATION_NAME_ATTRIBUTE, authorizedOrganizationName);
}
} catch (OrganizationManagementException e) {
throw new IdentityOAuth2Exception("Error while resolving organization name by ID.", e);
}
return additionalClaims;
}

private String resolveOrganizationId(String tenantDomain) throws IdentityOAuth2Exception {

try {
return getOrganizationManager().resolveOrganizationId(tenantDomain);
} catch (OrganizationManagementClientException e) {
if (ERROR_CODE_ORGANIZATION_NOT_FOUND_FOR_TENANT.getCode().equals(e.getErrorCode())) {
return additionalClaims;
return null;
}
throw new IdentityOAuth2Exception("Error while resolving organization id.", e);
} catch (OrganizationManagementException e) {
throw new IdentityOAuth2Exception("Error while resolving organization id.", e);
}
return additionalClaims;
}

private OrganizationManager getOrganizationManager() {

return OrganizationClaimProviderServiceComponentHolder.getInstance().getOrganizationManager();
}
}
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,11 @@
<artifactId>org.wso2.carbon.identity.configuration.mgt.core</artifactId>
<version>${carbon.identity.framework.version}</version>
</dependency>
<dependency>
<groupId>org.wso2.carbon.identity.framework</groupId>
<artifactId>org.wso2.carbon.identity.application.authentication.framework</artifactId>
<version>${carbon.identity.framework.version}</version>
</dependency>
<dependency>
<groupId>org.wso2.carbon.identity.inbound.auth.oauth2</groupId>
<artifactId>org.wso2.carbon.identity.oauth</artifactId>
Expand Down Expand Up @@ -494,7 +499,7 @@
<carbon.multitenancy.package.import.version.range>[4.7.0,5.0.0)
</carbon.multitenancy.package.import.version.range>

<carbon.identity.framework.version>5.25.369</carbon.identity.framework.version>
<carbon.identity.framework.version>5.25.396</carbon.identity.framework.version>
<carbon.identity.package.import.version.range>[5.20.0, 7.0.0)
</carbon.identity.package.import.version.range>

Expand Down
Loading