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

Allow sub organization applications to consume tokens from the valve level #302

Merged
Show file tree
Hide file tree
Changes from all 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 @@ -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 @@ -147,8 +149,10 @@
authenticationResult.setAuthenticationStatus(AuthenticationStatus.SUCCESS);

User authorizedUser = oAuth2IntrospectionResponseDTO.getAuthorizedUser();
String authorizedUserTenantDomain = null;

Check warning on line 152 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#L152

Added line #L152 was not covered by tests
if (authorizedUser != null) {
authenticationContext.setUser(authorizedUser);
authorizedUserTenantDomain = authorizedUser.getTenantDomain();

Check warning on line 155 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#L155

Added line #L155 was not covered by tests
if (authorizedUser instanceof AuthenticatedUser) {
IdentityUtil.threadLocalProperties.get()
.put(Constants.IS_FEDERATED_USER,
Expand All @@ -174,7 +178,18 @@
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 (StringUtils.isNotEmpty(authorizedUserTenantDomain) && OrganizationManagementUtil.
isOrganization(authorizedUserTenantDomain)) {
serviceProvider = OAuth2Util.getServiceProvider(oAuth2IntrospectionResponseDTO.getClientId(),

Check warning on line 188 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#L188

Added line #L188 was not covered by tests
authorizedUserTenantDomain);
} else {
serviceProvider = OAuth2Util.getServiceProvider(oAuth2IntrospectionResponseDTO.getClientId());

Check warning on line 191 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#L191

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

Check warning on line 207 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

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

Check warning on line 209 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#L209

Added line #L209 was not covered by tests
authorizedUserTenantDomain + " is an organization.", e);
}
}

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

/*
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 218 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#L218

Added line #L218 was not covered by tests
try {
if (StringUtils.isNotEmpty(authorizedUserTenantDomain) && OrganizationManagementUtil.
isOrganization(authorizedUserTenantDomain)) {
oAuthAppDO = OAuth2Util.getAppInformationByClientId(
oAuth2IntrospectionResponseDTO.getClientId(), authorizedUserTenantDomain);

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

Added lines #L222 - 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: " +

Check warning on line 228 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-L228

Added lines #L227 - L228 were not covered by tests
authorizedUserTenantDomain, e);
}
} 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: " +

Check warning on line 233 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

Added line #L233 was not covered by tests
authorizedUserTenantDomain + " is an organization.", e);
}
}

Check warning on line 236 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

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

Check warning on line 238 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#L238

Added line #L238 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 (StringUtils.isNotEmpty(authorizedUserTenantDomain) && OrganizationManagementUtil.
isOrganization(authorizedUserTenantDomain)) {
serviceProviderTenantDomain =
OAuth2Util.getTenantDomainOfOauthApp(oAuth2IntrospectionResponseDTO.getClientId(),

Check warning on line 251 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-L251

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

Check warning on line 255 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#L254-L255

Added lines #L254 - L255 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 262 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#L262

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

Check warning on line 264 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#L264

Added line #L264 was not covered by tests
authorizedUserTenantDomain + " 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);
}
Comment on lines +97 to +104

Choose a reason for hiding this comment

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

At which point this code get executed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If you are talking about the else part, that will be executed when a request comes from /t/{tenant-domain}/o/{org-id} pattern.

}
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
Loading