Skip to content

Commit

Permalink
Allow sub organization applications to consume tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
ShanChathusanda93 committed Jan 22, 2025
1 parent 28abfa2 commit bde18c2
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
import org.wso2.carbon.identity.oauth2.token.bindings.TokenBinding;
import org.wso2.carbon.identity.oauth2.util.OAuth2Util;
import org.wso2.carbon.identity.oauth2.validators.RefreshTokenValidator;
import org.wso2.carbon.identity.organization.management.service.exception.OrganizationManagementException;
import org.wso2.carbon.identity.organization.management.service.util.OrganizationManagementUtil;

import java.text.ParseException;
import java.util.Map;
Expand Down Expand Up @@ -174,7 +176,18 @@ protected AuthenticationResult doAuthenticate(MessageContext messageContext) {
String serviceProviderName = null;
String serviceProviderUUID = null;
try {
serviceProvider = OAuth2Util.getServiceProvider(oAuth2IntrospectionResponseDTO.getClientId());
/*
Tokens which are issued for the applications which are registered in sub organization,
contains the tenant domain for the authorized user as the sub organization. Based on that
we can get the application details by using both the client id and the tenant domain.
*/
if (OrganizationManagementUtil.isOrganization(oAuth2IntrospectionResponseDTO.getAuthorizedUser().
getTenantDomain())) {
serviceProvider = OAuth2Util.getServiceProvider(oAuth2IntrospectionResponseDTO.getClientId(),
oAuth2IntrospectionResponseDTO.getAuthorizedUser().getTenantDomain());

Check warning on line 187 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#L185-L187

Added lines #L185 - L187 were not covered by tests
} else {
serviceProvider = OAuth2Util.getServiceProvider(oAuth2IntrospectionResponseDTO.getClientId());

Check warning on line 189 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#L189

Added line #L189 was not covered by tests
}
if (serviceProvider != null) {
serviceProviderName = serviceProvider.getApplicationName();
serviceProviderUUID = serviceProvider.getApplicationResourceId();
Expand All @@ -189,17 +202,70 @@ protected AuthenticationResult doAuthenticate(MessageContext messageContext) {
log.debug("Error occurred while getting the Service Provider by Consumer key: "
+ oAuth2IntrospectionResponseDTO.getClientId(), e);
}
} catch (OrganizationManagementException e) {

Check warning on line 205 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#L205

Added line #L205 was not covered by tests
if (log.isDebugEnabled()) {
log.debug("Error occurred while checking the tenant domain: " +
oAuth2IntrospectionResponseDTO.getAuthorizedUser().getTenantDomain() +

Check warning on line 208 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#L207-L208

Added lines #L207 - L208 were not covered by tests
" is an organization.", e);
}
}

Check warning on line 211 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#L211

Added line #L211 was not covered by tests

/*
Set OAuthAppDO to the authentication context to be used when checking the user belongs to the
requested tenant. This needs to be executed in the sub organization level.
*/
OAuthAppDO oAuthAppDO = null;

Check warning on line 217 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#L217

Added line #L217 was not covered by tests
try {
if (OrganizationManagementUtil.isOrganization(oAuth2IntrospectionResponseDTO.getAuthorizedUser().
getTenantDomain())) {
oAuthAppDO = OAuth2Util.getAppInformationByClientId(
oAuth2IntrospectionResponseDTO.getClientId(),
oAuth2IntrospectionResponseDTO.getAuthorizedUser().getTenantDomain());

Check warning on line 223 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#L220-L223

Added lines #L220 - L223 were not covered by tests
}
} catch (IdentityOAuth2Exception | InvalidOAuthClientException e) {

Check warning on line 225 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#L225

Added line #L225 was not covered by tests
if (log.isDebugEnabled()) {
log.debug("Error occurred while getting the OAuth App by Consumer key: "
+ oAuth2IntrospectionResponseDTO.getClientId() + " and tenant domain: " +
oAuth2IntrospectionResponseDTO.getAuthorizedUser().getTenantDomain(), e);

Check warning on line 229 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#L227-L229

Added lines #L227 - L229 were not covered by tests
}
} catch (OrganizationManagementException e) {

Check warning on line 231 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#L231

Added line #L231 was not covered by tests
if (log.isDebugEnabled()) {
log.debug("Error occurred while checking the tenant domain: " +
oAuth2IntrospectionResponseDTO.getAuthorizedUser().getTenantDomain() +

Check warning on line 234 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#L233-L234

Added lines #L233 - L234 were not covered by tests
" is an organization.", e);
}
}

Check warning on line 237 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#L237

Added line #L237 was not covered by tests
if (oAuthAppDO != null) {
authenticationContext.addParameter(Constants.AUTH_CONTEXT_OAUTH_APP_PROPERTY, oAuthAppDO);

Check warning on line 239 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#L239

Added line #L239 was not covered by tests
}

String serviceProviderTenantDomain = null;
try {
serviceProviderTenantDomain =
OAuth2Util.getTenantDomainOfOauthApp(oAuth2IntrospectionResponseDTO.getClientId());
/*
Tokens which are issued for the applications which are registered in sub organization,
contains the tenant domain for the authorized user as the sub organization. Based on that
we can get the application tenant domain detail by using both the client id and the tenant domain.
*/
if (OrganizationManagementUtil.isOrganization(oAuth2IntrospectionResponseDTO.getAuthorizedUser().
getTenantDomain())) {
serviceProviderTenantDomain =
OAuth2Util.getTenantDomainOfOauthApp(oAuth2IntrospectionResponseDTO.getClientId(),
oAuth2IntrospectionResponseDTO.getAuthorizedUser().getTenantDomain());

Check warning on line 253 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#L250-L253

Added lines #L250 - L253 were not covered by tests
} else {
serviceProviderTenantDomain =
OAuth2Util.getTenantDomainOfOauthApp(oAuth2IntrospectionResponseDTO.getClientId());

Check warning on line 256 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#L255-L256

Added lines #L255 - L256 were not covered by tests
}
} catch (InvalidOAuthClientException | IdentityOAuth2Exception e) {
if (log.isDebugEnabled()) {
log.debug("Error occurred while getting the OAuth App tenantDomain by Consumer key: "
+ oAuth2IntrospectionResponseDTO.getClientId(), e);
}
} catch (OrganizationManagementException e) {

Check warning on line 263 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#L263

Added line #L263 was not covered by tests
if (log.isDebugEnabled()) {
log.debug("Error occurred while checking the tenant domain: " +
oAuth2IntrospectionResponseDTO.getAuthorizedUser().getTenantDomain() +

Check warning on line 266 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#L265-L266

Added lines #L265 - L266 were not covered by tests
" is an organization.", e);
}
}

if (serviceProviderName != null){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,14 @@ public static boolean isUserBelongsToRequestedTenant(AuthenticationContext authe
// Check request with organization qualified URL is allowed to access.
String organizationID = getOrganizationIdFromURLMapping(request);
if (user != null) {
return StringUtils.equals(organizationID, ((AuthenticatedUser) user).getAccessingOrganization());
if (StringUtils.equals(organizationID, ((AuthenticatedUser) user).getAccessingOrganization())) {
return true;
} else {
OAuthAppDO oAuthAppDO = (OAuthAppDO) authenticationContext.getParameter(
Constants.AUTH_CONTEXT_OAUTH_APP_PROPERTY);
tenantDomain = OAuth2Util.getTenantDomainOfOauthApp(oAuthAppDO);
return StringUtils.equals(((AuthenticatedUser) user).getAccessingOrganization(), tenantDomain);
}
}
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@
<identity.framework.version>7.3.13</identity.framework.version>
<carbon.identity.package.import.version.range>[5.17.8, 8.0.0)</carbon.identity.package.import.version.range>

<org.wso2.carbon.identity.oauth.version>7.0.65</org.wso2.carbon.identity.oauth.version>
<org.wso2.carbon.identity.oauth.version>7.0.213</org.wso2.carbon.identity.oauth.version>
<org.wso2.carbon.identity.oauth.import.version.range>[6.2.18, 8.0.0)
</org.wso2.carbon.identity.oauth.import.version.range>

Expand Down

0 comments on commit bde18c2

Please sign in to comment.