Skip to content

Commit

Permalink
Allow sub organization applications to issue and 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 15c716a
Show file tree
Hide file tree
Showing 3 changed files with 59 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,13 @@ protected AuthenticationResult doAuthenticate(MessageContext messageContext) {
String serviceProviderName = null;
String serviceProviderUUID = null;
try {
serviceProvider = OAuth2Util.getServiceProvider(oAuth2IntrospectionResponseDTO.getClientId());
if (OrganizationManagementUtil.isOrganization(oAuth2IntrospectionResponseDTO.getAuthorizedUser().
getTenantDomain())) {
serviceProvider = OAuth2Util.getServiceProvider(oAuth2IntrospectionResponseDTO.getClientId(),
oAuth2IntrospectionResponseDTO.getAuthorizedUser().getTenantDomain());

Check warning on line 182 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#L180-L182

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

Check warning on line 184 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#L184

Added line #L184 was not covered by tests
}
if (serviceProvider != null) {
serviceProviderName = serviceProvider.getApplicationName();
serviceProviderUUID = serviceProvider.getApplicationResourceId();
Expand All @@ -189,17 +197,56 @@ 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 200 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#L200

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

Check warning on line 203 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#L202-L203

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

Check warning on line 206 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#L206

Added line #L206 was not covered by tests

/*
Set OAuthAppDO to the authentication context to be used when checking the user belongs to the
requested tenant.
*/
OAuthAppDO oAuthAppDO = null;

Check warning on line 212 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#L212

Added line #L212 was not covered by tests
try {
oAuthAppDO = OAuth2Util.getAppInformationByClientId(
oAuth2IntrospectionResponseDTO.getClientId(),
oAuth2IntrospectionResponseDTO.getAuthorizedUser().getTenantDomain());
} catch (IdentityOAuth2Exception | InvalidOAuthClientException e) {

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#L214-L217

Added lines #L214 - L217 were 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 221 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#L219-L221

Added lines #L219 - L221 were not covered by tests
}
}

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#L223

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

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
}

String serviceProviderTenantDomain = null;
try {
serviceProviderTenantDomain =
OAuth2Util.getTenantDomainOfOauthApp(oAuth2IntrospectionResponseDTO.getClientId());
if (OrganizationManagementUtil.isOrganization(oAuth2IntrospectionResponseDTO.getAuthorizedUser().
getTenantDomain())) {
serviceProviderTenantDomain =
OAuth2Util.getTenantDomainOfOauthApp(oAuth2IntrospectionResponseDTO.getClientId(),
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#L231-L234

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

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#L236-L237

Added lines #L236 - L237 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 244 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#L244

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

Check warning on line 247 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#L246-L247

Added lines #L246 - L247 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 15c716a

Please sign in to comment.