Skip to content

Commit

Permalink
Fix resolving tenant domain issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
Thisara-Welmilla committed Oct 16, 2023
1 parent 3500e45 commit 30ee888
Show file tree
Hide file tree
Showing 13 changed files with 36 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.wso2.carbon.identity.core.model.ExpressionNode;
import org.wso2.carbon.identity.core.model.FilterTreeBuilder;
import org.wso2.carbon.identity.core.model.Node;
import org.wso2.carbon.identity.core.util.IdentityTenantUtil;
import org.wso2.carbon.identity.rest.api.user.application.v1.core.function.ApplicationBasicInfoToApiModel;
import org.wso2.carbon.identity.rest.api.user.application.v1.model.ApplicationListResponse;
import org.wso2.carbon.identity.rest.api.user.application.v1.model.ApplicationResponse;
Expand Down Expand Up @@ -71,7 +72,7 @@ public ApplicationResponse getApplication(String applicationId) {

try {

String tenantDomain = ContextLoader.getTenantDomainFromContext();
String tenantDomain = IdentityTenantUtil.resolveTenantDomain();
ApplicationBasicInfo applicationBasicInfo = ApplicationServiceHolder.getDiscoverableApplicationManager()
.getDiscoverableApplicationBasicInfoByResourceId(applicationId, tenantDomain);
if (applicationBasicInfo == null) {
Expand Down Expand Up @@ -104,7 +105,7 @@ public ApplicationListResponse getApplications(String attributes, Integer limit,

handleNotImplementedCapabilities(attributes, sortOrder, sortBy);

String tenantDomain = ContextLoader.getTenantDomainFromContext();
String tenantDomain = IdentityTenantUtil.resolveTenantDomain();
String filterFormatted = buildFilter(filter);
try {
List<ApplicationBasicInfo> applicationBasicInfos = ApplicationServiceHolder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import org.springframework.beans.factory.annotation.Autowired;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.identity.api.user.common.ContextLoader;
import org.wso2.carbon.identity.api.user.common.function.UniqueIdToUser;
import org.wso2.carbon.identity.application.common.model.User;
import org.wso2.carbon.identity.core.util.IdentityTenantUtil;
import org.wso2.carbon.identity.rest.api.user.association.v1.MeApiService;
import org.wso2.carbon.identity.rest.api.user.association.v1.core.UserAssociationService;
import org.wso2.carbon.identity.rest.api.user.association.v1.dto.AssociationUserRequestDTO;
Expand Down Expand Up @@ -86,7 +86,7 @@ private String getFullyQualifiedUsernameFromContext() {
private String getFullyQualifiedUserName(String userId) {

User user = new UniqueIdToUser().apply(UserAssociationServiceHolder.getRealmService(), userId,
ContextLoader.getTenantDomainFromContext());
IdentityTenantUtil.resolveTenantDomain());
return user.toFullQualifiedUsername();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package org.wso2.carbon.identity.rest.api.user.association.v1.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.wso2.carbon.identity.api.user.common.ContextLoader;
import org.wso2.carbon.identity.api.user.common.function.UniqueIdToUser;
import org.wso2.carbon.identity.application.common.model.User;
import org.wso2.carbon.identity.core.util.IdentityTenantUtil;
import org.wso2.carbon.identity.rest.api.user.association.v1.UserIdApiService;
import org.wso2.carbon.identity.rest.api.user.association.v1.core.UserAssociationService;
import org.wso2.carbon.identity.rest.api.user.association.v1.util.UserAssociationServiceHolder;
Expand Down Expand Up @@ -34,7 +34,7 @@ public Response userIdAssociationsGet(String userId) {
private String getUser(String userId) {

User user = new UniqueIdToUser().apply(UserAssociationServiceHolder.getRealmService(), userId,
ContextLoader.getTenantDomainFromContext());
IdentityTenantUtil.resolveTenantDomain());
return user.toFullQualifiedUsername();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@

import org.springframework.beans.factory.annotation.Autowired;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.identity.api.user.common.ContextLoader;
import org.wso2.carbon.identity.api.user.common.function.UniqueIdToUser;
import org.wso2.carbon.identity.application.common.model.User;
import org.wso2.carbon.identity.core.util.IdentityTenantUtil;
import org.wso2.carbon.identity.rest.api.user.authorized.apps.v1.UserIdApiService;
import org.wso2.carbon.identity.rest.api.user.authorized.apps.v1.core.AuthorizedAppsService;
import org.wso2.carbon.identity.rest.api.user.authorized.apps.v1.dto.AuthorizedAppDTO;
Expand Down Expand Up @@ -74,6 +74,6 @@ public Response listUserAuthorizedAppsByAppId(String userId, String applicationI

private User getUser(String userId) {

return new UniqueIdToUser().apply(realmService, userId, ContextLoader.getTenantDomainFromContext());
return new UniqueIdToUser().apply(realmService, userId, IdentityTenantUtil.resolveTenantDomain());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ public List<AuthorizedAppDTO> listUserAuthorizedApps(User user) {
*/
public void deleteIssuedTokensByAppId(String applicationId) {

String tenantDomain = ContextLoader.getTenantDomainFromContext();
String tenantDomain = IdentityTenantUtil.resolveTenantDomain();
ServiceProvider application = getServiceProvider(applicationId, tenantDomain);

// Extract the inbound authentication request config for the given inbound type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@

import org.springframework.beans.factory.annotation.Autowired;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.identity.api.user.common.ContextLoader;
import org.wso2.carbon.identity.api.user.common.function.UniqueIdToUser;
import org.wso2.carbon.identity.application.common.model.User;
import org.wso2.carbon.identity.core.util.IdentityTenantUtil;
import org.wso2.carbon.identity.rest.api.user.authorized.apps.v2.UserIdApiService;
import org.wso2.carbon.identity.rest.api.user.authorized.apps.v2.core.AuthorizedAppsService;
import org.wso2.carbon.identity.rest.api.user.authorized.apps.v2.dto.AuthorizedAppDTO;
Expand Down Expand Up @@ -75,6 +75,6 @@ public Response listUserAuthorizedAppsByAppId(String userId, String applicationI

private User getUser(String userId) {

return new UniqueIdToUser().apply(realmService, userId, ContextLoader.getTenantDomainFromContext());
return new UniqueIdToUser().apply(realmService, userId, IdentityTenantUtil.resolveTenantDomain());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
import org.wso2.carbon.identity.api.user.challenge.common.ChallengeQuestionServiceHolder;
import org.wso2.carbon.identity.api.user.challenge.common.Constant;
import org.wso2.carbon.identity.api.user.common.Constants;
import org.wso2.carbon.identity.api.user.common.ContextLoader;
import org.wso2.carbon.identity.api.user.common.error.APIError;
import org.wso2.carbon.identity.api.user.common.error.ErrorResponse;
import org.wso2.carbon.identity.application.common.model.User;
import org.wso2.carbon.identity.core.util.IdentityTenantUtil;
import org.wso2.carbon.identity.recovery.IdentityRecoveryClientException;
import org.wso2.carbon.identity.recovery.IdentityRecoveryException;
import org.wso2.carbon.identity.recovery.model.ChallengeQuestion;
Expand Down Expand Up @@ -65,7 +65,7 @@ public List<ChallengeSetDTO> getChallengesForUser(User user, Integer offset, Int

try {
return buildChallengesDTO(ChallengeQuestionServiceHolder.getChallengeQuestionManager()
.getAllChallengeQuestionsForUser(ContextLoader.getTenantDomainFromContext(), user), offset, limit);
.getAllChallengeQuestionsForUser(IdentityTenantUtil.resolveTenantDomain(), user), offset, limit);
} catch (IdentityRecoveryException e) {
throw handleIdentityRecoveryException(e,
Constant.ErrorMessage.ERROR_CODE_ERROR_RETRIEVING_CHALLENGES_FOR_USER);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@

import org.springframework.beans.factory.annotation.Autowired;
import org.wso2.carbon.identity.api.user.challenge.common.ChallengeQuestionServiceHolder;
import org.wso2.carbon.identity.api.user.common.ContextLoader;
import org.wso2.carbon.identity.api.user.common.function.UniqueIdToUser;
import org.wso2.carbon.identity.application.common.model.User;
import org.wso2.carbon.identity.core.util.IdentityTenantUtil;
import org.wso2.carbon.identity.rest.api.user.challenge.v1.UserIdApiService;
import org.wso2.carbon.identity.rest.api.user.challenge.v1.core.UserChallengeService;
import org.wso2.carbon.identity.rest.api.user.challenge.v1.dto.ChallengeAnswerDTO;
Expand Down Expand Up @@ -108,6 +108,6 @@ private URI getUserChallengeAnswersLocation(String userId) {
private User getUser(String userId) {

return new UniqueIdToUser().apply(ChallengeQuestionServiceHolder.getRealmService(), userId,
ContextLoader.getTenantDomainFromContext());
IdentityTenantUtil.resolveTenantDomain());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.base.MultitenantConstants;
import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.identity.api.user.common.error.APIError;
Expand All @@ -27,6 +28,7 @@
import org.wso2.carbon.identity.core.ServiceURLBuilder;
import org.wso2.carbon.identity.core.URLBuilderException;
import org.wso2.carbon.identity.core.util.IdentityTenantUtil;
import org.wso2.carbon.identity.core.util.IdentityUtil;
import org.wso2.carbon.user.api.UserRealm;
import org.wso2.carbon.user.api.UserStoreException;
import org.wso2.carbon.user.core.UserStoreConfigConstants;
Expand All @@ -38,6 +40,7 @@
import static org.wso2.carbon.identity.api.user.common.Constants.ErrorMessage.ERROR_CODE_INVALID_USERNAME;
import static org.wso2.carbon.identity.api.user.common.Constants.ErrorMessage.ERROR_CODE_SERVER_ERROR;
import static org.wso2.carbon.identity.api.user.common.Constants.TENANT_CONTEXT_PATH_COMPONENT;
import static org.wso2.carbon.identity.api.user.common.Constants.TENANT_NAME_FROM_CONTEXT;
import static org.wso2.carbon.identity.api.user.common.Constants.USER_API_PATH_COMPONENT;
import static org.wso2.carbon.identity.application.common.util.IdentityApplicationConstants.Error.UNEXPECTED_SERVER_ERROR;

Expand All @@ -54,7 +57,11 @@ public class ContextLoader {
*/
public static String getTenantDomainFromContext() {

return IdentityTenantUtil.resolveTenantDomain();
String tenantDomain = MultitenantConstants.SUPER_TENANT_DOMAIN_NAME;
if (IdentityUtil.threadLocalProperties.get().get(TENANT_NAME_FROM_CONTEXT) != null) {
tenantDomain = (String) IdentityUtil.threadLocalProperties.get().get(TENANT_NAME_FROM_CONTEXT);
}
return tenantDomain;
}

/**
Expand All @@ -72,7 +79,7 @@ public static String getUsernameFromContext() {
*/
public static User getUserFromContext() {

return getUser(getTenantDomainFromContext(), getUsernameFromContext());
return getUser(IdentityTenantUtil.resolveTenantDomain(), getUsernameFromContext());
}


Expand Down Expand Up @@ -159,7 +166,7 @@ public static org.wso2.carbon.identity.application.common.model.User getUser(org
public static URI buildURI(String endpoint) {

String tenantQualifiedRelativePath =
String.format(TENANT_CONTEXT_PATH_COMPONENT, getTenantDomainFromContext()) + USER_API_PATH_COMPONENT;
String.format(TENANT_CONTEXT_PATH_COMPONENT, IdentityTenantUtil.resolveTenantDomain()) + USER_API_PATH_COMPONENT;
String url = tenantQualifiedRelativePath + endpoint;
return URI.create(url);
}
Expand Down Expand Up @@ -221,7 +228,7 @@ private static String getContext(String endpoint) {
if (IdentityTenantUtil.isTenantQualifiedUrlsEnabled()) {
context = USER_API_PATH_COMPONENT + endpoint;
} else {
context = String.format(TENANT_CONTEXT_PATH_COMPONENT, getTenantDomainFromContext()) +
context = String.format(TENANT_CONTEXT_PATH_COMPONENT, IdentityTenantUtil.resolveTenantDomain()) +
USER_API_PATH_COMPONENT + endpoint;
}
return context;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ private String includeData(Constants.ErrorMessage error, String data) {

private int getTenantId() {

String tenantDomain = ContextLoader.getTenantDomainFromContext();
String tenantDomain = IdentityTenantUtil.resolveTenantDomain();
if (StringUtils.isBlank(tenantDomain)) {
throw handleException(
Response.Status.INTERNAL_SERVER_ERROR,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ private String getUserName() {

private String getTenantDomain() {

return ContextLoader.getTenantDomainFromContext();
return IdentityTenantUtil.resolveTenantDomain();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package org.wso2.carbon.identity.rest.api.user.session.v1.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.wso2.carbon.identity.api.user.common.ContextLoader;
import org.wso2.carbon.identity.core.util.IdentityTenantUtil;
import org.wso2.carbon.identity.rest.api.user.session.v1.SessionsApiService;
import org.wso2.carbon.identity.rest.api.user.session.v1.core.SessionManagementService;
import org.wso2.carbon.identity.rest.api.user.session.v1.dto.SearchResponseDTO;
Expand All @@ -35,7 +35,7 @@ public class SessionsApiServiceImpl extends SessionsApiService {
@Override
public Response getSessions(String filter, Integer limit, Long since, Long until) {

SearchResponseDTO responseDTO = sessionManagementService.getSessions(ContextLoader.getTenantDomainFromContext(),
SearchResponseDTO responseDTO = sessionManagementService.getSessions(IdentityTenantUtil.resolveTenantDomain(),
filter, limit, since, until);

return Response.ok().entity(responseDTO).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.wso2.carbon.identity.api.user.common.ContextLoader;
import org.wso2.carbon.identity.api.user.common.Util;
import org.wso2.carbon.identity.api.user.session.common.util.SessionManagementServiceHolder;
import org.wso2.carbon.identity.core.util.IdentityTenantUtil;
import org.wso2.carbon.identity.rest.api.user.session.v1.UserIdApiService;
import org.wso2.carbon.identity.rest.api.user.session.v1.core.SessionManagementService;
import org.wso2.carbon.identity.rest.api.user.session.v1.dto.SessionDTO;
Expand All @@ -52,7 +53,7 @@ public class UserIdApiServiceImpl extends UserIdApiService {
public Response getSessionBySessionId(String userId, String sessionId) {

Util.validateUserId(SessionManagementServiceHolder.getRealmService(), userId,
ContextLoader.getTenantDomainFromContext());
IdentityTenantUtil.resolveTenantDomain());

Optional<SessionDTO> session = sessionManagementService.getSessionBySessionId(userId, sessionId);
if (session.isPresent()) {
Expand All @@ -66,7 +67,7 @@ public Response getSessionBySessionId(String userId, String sessionId) {
public Response getSessionsByUserId(String userId, Integer limit, Integer offset, String filter, String sort) {

Util.validateUserId(SessionManagementServiceHolder.getRealmService(), userId,
ContextLoader.getTenantDomainFromContext());
IdentityTenantUtil.resolveTenantDomain());

SessionsDTO sessionsOfUser = sessionManagementService.getSessionsByUserId(userId, limit, offset, filter, sort);
if (sessionsOfUser == null || sessionsOfUser.getSessions().isEmpty()) {
Expand All @@ -80,7 +81,7 @@ public Response getSessionsByUserId(String userId, Integer limit, Integer offset
public Response terminateSessionBySessionId(String userId, String sessionId) {

Util.validateUserId(SessionManagementServiceHolder.getRealmService(), userId,
ContextLoader.getTenantDomainFromContext());
IdentityTenantUtil.resolveTenantDomain());
sessionManagementService.terminateSessionBySessionId(userId, sessionId);
return Response.noContent().build();
}
Expand Down Expand Up @@ -112,7 +113,7 @@ public Response terminateSessionsByUserId(String userId) {
}

Util.validateUserId(SessionManagementServiceHolder.getRealmService(), userId,
ContextLoader.getTenantDomainFromContext());
IdentityTenantUtil.resolveTenantDomain());
sessionManagementService.terminateSessionsByUserId(userId);
return Response.noContent().build();
} catch (UserStoreException e) {
Expand Down

0 comments on commit 30ee888

Please sign in to comment.