diff --git a/components/org.wso2.carbon.identity.query.saml/src/main/java/org/wso2/carbon/identity/query/saml/internal/SAMLQueryServiceComponent.java b/components/org.wso2.carbon.identity.query.saml/src/main/java/org/wso2/carbon/identity/query/saml/internal/SAMLQueryServiceComponent.java index 9f6402c8b..bed86b047 100644 --- a/components/org.wso2.carbon.identity.query.saml/src/main/java/org/wso2/carbon/identity/query/saml/internal/SAMLQueryServiceComponent.java +++ b/components/org.wso2.carbon.identity.query.saml/src/main/java/org/wso2/carbon/identity/query/saml/internal/SAMLQueryServiceComponent.java @@ -27,6 +27,7 @@ import org.osgi.service.component.annotations.Reference; import org.osgi.service.component.annotations.ReferenceCardinality; import org.osgi.service.component.annotations.ReferencePolicy; +import org.wso2.carbon.identity.core.SAMLSSOServiceProviderManager; import org.wso2.carbon.user.core.service.RealmService; /** @@ -41,6 +42,8 @@ public class SAMLQueryServiceComponent { private static RealmService realmservice = null; + private static SAMLSSOServiceProviderManager samlSSOServiceProviderManager = null; + /** * This method is used to get created realm service * @@ -106,4 +109,46 @@ protected void unsetRealmService(RealmService realmService) { log.debug("DefaultUserRealm unset in to bundle"); } } + + /** + * This method is used to set SAMLSSOServiceProviderManager instance + * + * @param samlSSOServiceProviderManager SAMLSSOServiceProviderManager instance + */ + @Reference( + name = "saml.sso.service.provider.manager", + service = SAMLSSOServiceProviderManager.class, + cardinality = ReferenceCardinality.MANDATORY, + policy = ReferencePolicy.DYNAMIC, + unbind = "unsetSAMLSSOServiceProviderManager") + protected void setSAMLSSOServiceProviderManager(SAMLSSOServiceProviderManager samlSSOServiceProviderManager) { + + this.samlSSOServiceProviderManager = samlSSOServiceProviderManager; + if (log.isDebugEnabled()) { + log.debug("SAMLSSOServiceProviderManager set in to bundle"); + } + } + + /** + * This method is used to un-set SAMLSSOServiceProviderManager instance + * + * @param samlSSOServiceProviderManager SAMLSSOServiceProviderManager instance + */ + protected void unsetSAMLSSOServiceProviderManager(SAMLSSOServiceProviderManager samlSSOServiceProviderManager) { + + this.samlSSOServiceProviderManager = null; + if (log.isDebugEnabled()) { + log.debug("SAMLSSOServiceProviderManager unset in to bundle"); + } + } + + /** + * This method is used to get SAMLSSOServiceProviderManager instance + * + * @return SAMLSSOServiceProviderManager instance + */ + public static SAMLSSOServiceProviderManager getSAMLSSOServiceProviderManager() { + + return samlSSOServiceProviderManager; + } } diff --git a/components/org.wso2.carbon.identity.query.saml/src/main/java/org/wso2/carbon/identity/query/saml/util/SAMLQueryRequestUtil.java b/components/org.wso2.carbon.identity.query.saml/src/main/java/org/wso2/carbon/identity/query/saml/util/SAMLQueryRequestUtil.java index f3d82ed99..36c865df7 100644 --- a/components/org.wso2.carbon.identity.query.saml/src/main/java/org/wso2/carbon/identity/query/saml/util/SAMLQueryRequestUtil.java +++ b/components/org.wso2.carbon.identity.query.saml/src/main/java/org/wso2/carbon/identity/query/saml/util/SAMLQueryRequestUtil.java @@ -1,19 +1,19 @@ /* - * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * Copyright (c) (2016-2023), WSO2 LLC. (http://www.wso2.com). * - * WSO2 Inc. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. - * You may obtain a copy of the License at + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ package org.wso2.carbon.identity.query.saml.util; @@ -73,15 +73,13 @@ import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.identity.base.IdentityException; import org.wso2.carbon.identity.core.model.SAMLSSOServiceProviderDO; -import org.wso2.carbon.identity.core.persistence.IdentityPersistenceManager; import org.wso2.carbon.identity.query.saml.SignKeyDataHolder; import org.wso2.carbon.identity.query.saml.exception.IdentitySAML2QueryException; +import org.wso2.carbon.identity.query.saml.internal.SAMLQueryServiceComponent; import org.wso2.carbon.identity.saml.common.util.SAMLInitializer; import org.wso2.carbon.identity.sso.saml.SAMLSSOConstants; import org.wso2.carbon.identity.sso.saml.SSOServiceProviderConfigManager; import org.wso2.carbon.identity.sso.saml.util.SAMLSSOUtil; -import org.wso2.carbon.registry.core.exceptions.RegistryException; -import org.wso2.carbon.registry.core.session.UserRegistry; import org.xml.sax.SAXException; import javax.xml.XMLConstants; @@ -212,19 +210,11 @@ public static SAMLSSOServiceProviderDO getServiceProviderConfig(String issuer) SSOServiceProviderConfigManager.getInstance(); SAMLSSOServiceProviderDO ssoIdpConfigs = idPConfigManager.getServiceProvider(issuer); if (ssoIdpConfigs == null) { - IdentityPersistenceManager persistenceManager = - IdentityPersistenceManager.getPersistanceManager(); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); - UserRegistry registry = - SAMLSSOUtil.getRegistryService() - .getConfigSystemRegistry(tenantId); - ssoIdpConfigs = persistenceManager.getServiceProvider(registry, issuer); + ssoIdpConfigs = SAMLQueryServiceComponent.getSAMLSSOServiceProviderManager() + .getServiceProvider(issuer, tenantId); } return ssoIdpConfigs; - } catch (RegistryException e) { - log.error("Unable to load registry service", e); - throw new IdentitySAML2QueryException("Unable to load registry service"); - } catch (IdentityException e) { log.error("Unable to load Identity persistence service manager", e); throw new IdentitySAML2QueryException("Unable to load Identity persistence service manager"); diff --git a/components/org.wso2.carbon.identity.sso.saml/src/main/java/org/wso2/carbon/identity/sso/saml/admin/SAMLSSOConfigAdmin.java b/components/org.wso2.carbon.identity.sso.saml/src/main/java/org/wso2/carbon/identity/sso/saml/admin/SAMLSSOConfigAdmin.java index 4d7ad6204..d5a3beb83 100644 --- a/components/org.wso2.carbon.identity.sso.saml/src/main/java/org/wso2/carbon/identity/sso/saml/admin/SAMLSSOConfigAdmin.java +++ b/components/org.wso2.carbon.identity.sso.saml/src/main/java/org/wso2/carbon/identity/sso/saml/admin/SAMLSSOConfigAdmin.java @@ -1,8 +1,8 @@ /* - * Copyright (c) 2007, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * Copyright (c) (2007-2023), WSO2 LLC. (http://www.wso2.com). * - * WSO2 Inc. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except * in compliance with the License. * You may obtain a copy of the License at * @@ -27,7 +27,6 @@ import org.wso2.carbon.core.util.KeyStoreManager; import org.wso2.carbon.identity.base.IdentityException; import org.wso2.carbon.identity.core.model.SAMLSSOServiceProviderDO; -import org.wso2.carbon.identity.core.persistence.IdentityPersistenceManager; import org.wso2.carbon.identity.core.util.IdentityTenantUtil; import org.wso2.carbon.identity.core.util.IdentityUtil; import org.wso2.carbon.identity.sp.metadata.saml2.exception.InvalidMetadataException; @@ -38,6 +37,7 @@ import org.wso2.carbon.identity.sso.saml.dto.SAMLSSOServiceProviderInfoDTO; import org.wso2.carbon.identity.sso.saml.exception.IdentitySAML2ClientException; import org.wso2.carbon.identity.sso.saml.internal.IdentitySAMLSSOServiceComponent; +import org.wso2.carbon.identity.sso.saml.internal.IdentitySAMLSSOServiceComponentHolder; import org.wso2.carbon.identity.sso.saml.util.SAMLSSOUtil; import org.wso2.carbon.registry.core.Registry; import org.wso2.carbon.registry.core.session.UserRegistry; @@ -58,9 +58,11 @@ public class SAMLSSOConfigAdmin { private static final Log log = LogFactory.getLog(SAMLSSOConfigAdmin.class); private UserRegistry registry; + private final int tenantId; public SAMLSSOConfigAdmin(Registry userRegistry) { registry = (UserRegistry) userRegistry; + tenantId = ((UserRegistry) userRegistry).getTenantId(); } /** @@ -73,8 +75,6 @@ public SAMLSSOConfigAdmin(Registry userRegistry) { public boolean addRelyingPartyServiceProvider(SAMLSSOServiceProviderDTO serviceProviderDTO) throws IdentityException { SAMLSSOServiceProviderDO serviceProviderDO = createSAMLSSOServiceProviderDO(serviceProviderDTO); - IdentityPersistenceManager persistenceManager = IdentityPersistenceManager - .getPersistanceManager(); try { String issuer = getIssuerWithQualifier(serviceProviderDO); SAMLSSOServiceProviderDO samlssoServiceProviderDO = SSOServiceProviderConfigManager.getInstance(). @@ -86,7 +86,8 @@ public boolean addRelyingPartyServiceProvider(SAMLSSOServiceProviderDTO serviceP log.error(message); return false; } - return persistenceManager.addServiceProvider(registry, serviceProviderDO); + return IdentitySAMLSSOServiceComponentHolder.getInstance().getSAMLSSOServiceProviderManager() + .addServiceProvider(serviceProviderDO, tenantId); } catch (IdentityException e) { String message = "Error obtaining a registry for adding a new service provider"; throw new IdentityException(message, e); @@ -130,8 +131,8 @@ private String getIssuerWithQualifier(SAMLSSOServiceProviderDO serviceProviderDO private SAMLSSOServiceProviderDTO persistSAMLServiceProvider(SAMLSSOServiceProviderDO samlssoServiceProviderDO) throws IdentityException { - IdentityPersistenceManager persistenceManager = IdentityPersistenceManager.getPersistanceManager(); - boolean response = persistenceManager.addServiceProvider(registry, samlssoServiceProviderDO); + boolean response = IdentitySAMLSSOServiceComponentHolder.getInstance().getSAMLSSOServiceProviderManager() + .addServiceProvider(samlssoServiceProviderDO, tenantId); if (response) { return createSAMLSSOServiceProviderDTO(samlssoServiceProviderDO); } else { @@ -150,10 +151,10 @@ private SAMLSSOServiceProviderDTO persistSAMLServiceProvider(SAMLSSOServiceProvi */ private void saveCertificateToKeyStore(SAMLSSOServiceProviderDO serviceProviderDO) throws Exception { - KeyStoreManager manager = KeyStoreManager.getInstance(registry.getTenantId(), IdentitySAMLSSOServiceComponent + KeyStoreManager manager = KeyStoreManager.getInstance(tenantId, IdentitySAMLSSOServiceComponent .getServerConfigurationService(), IdentityTenantUtil.getRegistryService()); - if (MultitenantConstants.SUPER_TENANT_ID == registry.getTenantId()) { + if (MultitenantConstants.SUPER_TENANT_ID == tenantId) { KeyStore keyStore = manager.getPrimaryKeyStore(); @@ -167,7 +168,7 @@ private void saveCertificateToKeyStore(SAMLSSOServiceProviderDO serviceProviderD } } else { - String keyStoreName = getKeyStoreName(registry.getTenantId()); + String keyStoreName = getKeyStoreName(tenantId); KeyStore keyStore = manager.getKeyStore(keyStoreName); // Add new certificate @@ -196,7 +197,6 @@ private String getKeyStoreName(int tenantId) { */ public SAMLSSOServiceProviderDTO uploadRelyingPartyServiceProvider(String metadata) throws IdentityException { - IdentityPersistenceManager persistenceManager = IdentityPersistenceManager.getPersistanceManager(); Parser parser = new Parser(registry); SAMLSSOServiceProviderDO samlssoServiceProviderDO = new SAMLSSOServiceProviderDO(); @@ -411,9 +411,8 @@ private SAMLSSOServiceProviderDTO createSAMLSSOServiceProviderDTO(SAMLSSOService public SAMLSSOServiceProviderInfoDTO getServiceProviders() throws IdentityException { SAMLSSOServiceProviderDTO[] serviceProviders = null; try { - IdentityPersistenceManager persistenceManager = IdentityPersistenceManager - .getPersistanceManager(); - SAMLSSOServiceProviderDO[] providersSet = persistenceManager.getServiceProviders(registry); + SAMLSSOServiceProviderDO[] providersSet = IdentitySAMLSSOServiceComponentHolder.getInstance() + .getSAMLSSOServiceProviderManager().getServiceProviders(tenantId); serviceProviders = new SAMLSSOServiceProviderDTO[providersSet.length]; for (int i = 0; i < providersSet.length; i++) { @@ -486,7 +485,7 @@ public SAMLSSOServiceProviderInfoDTO getServiceProviders() throws IdentityExcept serviceProviderInfoDTO.setServiceProviders(serviceProviders); //if it is tenant zero - if (registry.getTenantId() == 0) { + if (tenantId == 0) { serviceProviderInfoDTO.setTenantZero(true); } return serviceProviderInfoDTO; @@ -501,8 +500,8 @@ public SAMLSSOServiceProviderInfoDTO getServiceProviders() throws IdentityExcept */ public boolean removeServiceProvider(String issuer) throws IdentityException { try { - IdentityPersistenceManager persistenceManager = IdentityPersistenceManager.getPersistanceManager(); - return persistenceManager.removeServiceProvider(registry, issuer); + return IdentitySAMLSSOServiceComponentHolder.getInstance() + .getSAMLSSOServiceProviderManager().removeServiceProvider(issuer, tenantId); } catch (IdentityException e) { throw new IdentityException("Error removing a Service Provider with issuer: " + issuer, e); } diff --git a/components/org.wso2.carbon.identity.sso.saml/src/main/java/org/wso2/carbon/identity/sso/saml/internal/IdentitySAMLSSOServiceComponent.java b/components/org.wso2.carbon.identity.sso.saml/src/main/java/org/wso2/carbon/identity/sso/saml/internal/IdentitySAMLSSOServiceComponent.java index d5dd9b1a2..166c6132a 100644 --- a/components/org.wso2.carbon.identity.sso.saml/src/main/java/org/wso2/carbon/identity/sso/saml/internal/IdentitySAMLSSOServiceComponent.java +++ b/components/org.wso2.carbon.identity.sso.saml/src/main/java/org/wso2/carbon/identity/sso/saml/internal/IdentitySAMLSSOServiceComponent.java @@ -34,6 +34,7 @@ import org.wso2.carbon.identity.application.mgt.ApplicationManagementService; import org.wso2.carbon.identity.application.mgt.listener.ApplicationMgtListener; import org.wso2.carbon.identity.base.IdentityConstants; +import org.wso2.carbon.identity.core.SAMLSSOServiceProviderManager; import org.wso2.carbon.identity.core.util.IdentityCoreInitializedEvent; import org.wso2.carbon.identity.core.util.IdentityUtil; import org.wso2.carbon.identity.event.handler.AbstractEventHandler; @@ -458,4 +459,27 @@ protected void unsetApplicationManagementService(ApplicationManagementService ap log.debug("Unset the ApplicationManagementService"); } } + + + @Reference( + name = "saml.sso.service.provider.manager", + service = org.wso2.carbon.identity.core.SAMLSSOServiceProviderManager.class, + cardinality = ReferenceCardinality.MANDATORY, + policy = ReferencePolicy.DYNAMIC, + unbind = "unsetSAMLSSOServiceProviderManager") + protected void setSAMLSSOServiceProviderManager(SAMLSSOServiceProviderManager samlSSOServiceProviderManager) { + + IdentitySAMLSSOServiceComponentHolder.getInstance().setSAMLSSOServiceProviderManager(samlSSOServiceProviderManager); + if (log.isDebugEnabled()) { + log.debug("SAMLSSOServiceProviderManager set in to bundle"); + } + } + + protected void unsetSAMLSSOServiceProviderManager(SAMLSSOServiceProviderManager samlSSOServiceProviderManager) { + + IdentitySAMLSSOServiceComponentHolder.getInstance().setSAMLSSOServiceProviderManager(null); + if (log.isDebugEnabled()) { + log.debug("SAMLSSOServiceProviderManager unset in to bundle"); + } + } } diff --git a/components/org.wso2.carbon.identity.sso.saml/src/main/java/org/wso2/carbon/identity/sso/saml/internal/IdentitySAMLSSOServiceComponentHolder.java b/components/org.wso2.carbon.identity.sso.saml/src/main/java/org/wso2/carbon/identity/sso/saml/internal/IdentitySAMLSSOServiceComponentHolder.java new file mode 100644 index 000000000..b2c109cf2 --- /dev/null +++ b/components/org.wso2.carbon.identity.sso.saml/src/main/java/org/wso2/carbon/identity/sso/saml/internal/IdentitySAMLSSOServiceComponentHolder.java @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2023, WSO2 LLC. (http://www.wso2.com). + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.carbon.identity.sso.saml.internal; + +import org.wso2.carbon.identity.core.SAMLSSOServiceProviderManager; + +/** + * Identity SAML SSO Service Component Holder. + */ +public class IdentitySAMLSSOServiceComponentHolder { + + private SAMLSSOServiceProviderManager samlSSOServiceProviderManager; + + private static final IdentitySAMLSSOServiceComponentHolder instance = new IdentitySAMLSSOServiceComponentHolder(); + + private IdentitySAMLSSOServiceComponentHolder() { + + } + + public static IdentitySAMLSSOServiceComponentHolder getInstance() { + + return instance; + } + + /** + * Set SAMLSSOServiceProviderManager. + * + * @param samlSSOServiceProviderManager SAMLSSOServiceProviderManager. + */ + public void setSAMLSSOServiceProviderManager(SAMLSSOServiceProviderManager samlSSOServiceProviderManager) { + + this.samlSSOServiceProviderManager = samlSSOServiceProviderManager; + } + + /** + * Get SAMLSSOServiceProviderManager. + * + * @return SAMLSSOServiceProviderManager. + */ + public SAMLSSOServiceProviderManager getSAMLSSOServiceProviderManager() { + + return samlSSOServiceProviderManager; + } + + +} diff --git a/components/org.wso2.carbon.identity.sso.saml/src/main/java/org/wso2/carbon/identity/sso/saml/processors/IdPInitSSOAuthnRequestProcessor.java b/components/org.wso2.carbon.identity.sso.saml/src/main/java/org/wso2/carbon/identity/sso/saml/processors/IdPInitSSOAuthnRequestProcessor.java index 5eac7f090..a42fcd4ea 100644 --- a/components/org.wso2.carbon.identity.sso.saml/src/main/java/org/wso2/carbon/identity/sso/saml/processors/IdPInitSSOAuthnRequestProcessor.java +++ b/components/org.wso2.carbon.identity.sso.saml/src/main/java/org/wso2/carbon/identity/sso/saml/processors/IdPInitSSOAuthnRequestProcessor.java @@ -1,8 +1,8 @@ /* - * Copyright (c) 2010, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * Copyright (c) (2010-2023), WSO2 LLC. (http://www.wso2.com). * - * WSO2 Inc. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except * in compliance with the License. * You may obtain a copy of the License at * @@ -22,10 +22,8 @@ import org.apache.commons.logging.LogFactory; import org.opensaml.saml.saml2.core.Response; import org.wso2.carbon.context.PrivilegedCarbonContext; -import org.wso2.carbon.context.RegistryType; import org.wso2.carbon.identity.base.IdentityException; import org.wso2.carbon.identity.core.model.SAMLSSOServiceProviderDO; -import org.wso2.carbon.identity.core.persistence.IdentityPersistenceManager; import org.wso2.carbon.identity.sso.saml.SAMLSSOConstants; import org.wso2.carbon.identity.sso.saml.SSOServiceProviderConfigManager; import org.wso2.carbon.identity.sso.saml.builders.ErrorResponseBuilder; @@ -33,9 +31,9 @@ import org.wso2.carbon.identity.sso.saml.builders.SAMLArtifactBuilder; import org.wso2.carbon.identity.sso.saml.dto.SAMLSSOAuthnReqDTO; import org.wso2.carbon.identity.sso.saml.dto.SAMLSSORespDTO; +import org.wso2.carbon.identity.sso.saml.internal.IdentitySAMLSSOServiceComponentHolder; import org.wso2.carbon.identity.sso.saml.session.SSOSessionPersistenceManager; import org.wso2.carbon.identity.sso.saml.util.SAMLSSOUtil; -import org.wso2.carbon.registry.core.Registry; import org.wso2.carbon.registry.core.utils.UUIDGenerator; import java.util.ArrayList; @@ -232,10 +230,9 @@ private SAMLSSOServiceProviderDO getServiceProviderConfig(SAMLSSOAuthnReqDTO aut SAMLSSOServiceProviderDO ssoIdpConfigs = stratosIdpConfigManager .getServiceProvider(authnReqDTO.getIssuer()); if (ssoIdpConfigs == null) { - IdentityPersistenceManager persistenceManager = IdentityPersistenceManager - .getPersistanceManager(); - Registry registry = (Registry) PrivilegedCarbonContext.getThreadLocalCarbonContext().getRegistry(RegistryType.SYSTEM_CONFIGURATION); - ssoIdpConfigs = persistenceManager.getServiceProvider(registry,authnReqDTO.getIssuer()); + int tenantID = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + ssoIdpConfigs = IdentitySAMLSSOServiceComponentHolder.getInstance().getSAMLSSOServiceProviderManager() + .getServiceProvider(authnReqDTO.getIssuer(), tenantID); authnReqDTO.setStratosDeployment(false); // not stratos } else { authnReqDTO.setStratosDeployment(true); // stratos deployment diff --git a/components/org.wso2.carbon.identity.sso.saml/src/main/java/org/wso2/carbon/identity/sso/saml/processors/SPInitLogoutRequestProcessor.java b/components/org.wso2.carbon.identity.sso.saml/src/main/java/org/wso2/carbon/identity/sso/saml/processors/SPInitLogoutRequestProcessor.java index 4a5c3ca2d..f07b14d8d 100644 --- a/components/org.wso2.carbon.identity.sso.saml/src/main/java/org/wso2/carbon/identity/sso/saml/processors/SPInitLogoutRequestProcessor.java +++ b/components/org.wso2.carbon.identity.sso.saml/src/main/java/org/wso2/carbon/identity/sso/saml/processors/SPInitLogoutRequestProcessor.java @@ -1,8 +1,8 @@ /* - * Copyright (c) 2010, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * Copyright (c) (2010-2023), WSO2 LLC. (http://www.wso2.com). * - * WSO2 Inc. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except * in compliance with the License. * You may obtain a copy of the License at * @@ -23,23 +23,21 @@ import org.opensaml.saml.saml2.core.LogoutRequest; import org.opensaml.saml.saml2.core.LogoutResponse; import org.wso2.carbon.context.PrivilegedCarbonContext; -import org.wso2.carbon.context.RegistryType; import org.wso2.carbon.identity.application.common.util.IdentityApplicationManagementUtil; import org.wso2.carbon.identity.base.IdentityException; import org.wso2.carbon.identity.core.model.SAMLSSOServiceProviderDO; -import org.wso2.carbon.identity.core.persistence.IdentityPersistenceManager; import org.wso2.carbon.identity.core.util.IdentityTenantUtil; import org.wso2.carbon.identity.core.util.IdentityUtil; import org.wso2.carbon.identity.sso.saml.SAMLSSOConstants; import org.wso2.carbon.identity.sso.saml.SSOServiceProviderConfigManager; import org.wso2.carbon.identity.sso.saml.builders.SingleLogoutMessageBuilder; import org.wso2.carbon.identity.sso.saml.dto.SAMLSSOReqValidationResponseDTO; +import org.wso2.carbon.identity.sso.saml.internal.IdentitySAMLSSOServiceComponentHolder; import org.wso2.carbon.identity.sso.saml.session.SSOSessionPersistenceManager; import org.wso2.carbon.identity.sso.saml.session.SessionInfoData; import org.wso2.carbon.identity.sso.saml.util.LambdaExceptionUtils; import org.wso2.carbon.identity.sso.saml.util.SAMLSSOUtil; import org.wso2.carbon.identity.sso.saml.validators.ValidationResult; -import org.wso2.carbon.registry.core.Registry; import org.wso2.carbon.user.api.UserStoreException; import org.wso2.carbon.user.core.UserCoreConstants; import org.wso2.carbon.utils.multitenancy.MultitenantConstants; @@ -271,10 +269,8 @@ private SAMLSSOServiceProviderDO getServiceProviderConfig(String issuer, String privilegedCarbonContext.setTenantId(tenantId); privilegedCarbonContext.setTenantDomain(tenantDomain); IdentityTenantUtil.initializeRegistry(tenantId, tenantDomain); - IdentityPersistenceManager persistenceManager = IdentityPersistenceManager.getPersistanceManager(); - Registry registry = (Registry) PrivilegedCarbonContext.getThreadLocalCarbonContext().getRegistry - (RegistryType.SYSTEM_CONFIGURATION); - ssoIdpConfigs = persistenceManager.getServiceProvider(registry, issuer); + ssoIdpConfigs = IdentitySAMLSSOServiceComponentHolder.getInstance().getSAMLSSOServiceProviderManager() + .getServiceProvider(issuer, tenantId); } finally { PrivilegedCarbonContext.endTenantFlow(); } diff --git a/components/org.wso2.carbon.identity.sso.saml/src/main/java/org/wso2/carbon/identity/sso/saml/processors/SPInitSSOAuthnRequestProcessor.java b/components/org.wso2.carbon.identity.sso.saml/src/main/java/org/wso2/carbon/identity/sso/saml/processors/SPInitSSOAuthnRequestProcessor.java index 504d01df2..b9ae527ea 100644 --- a/components/org.wso2.carbon.identity.sso.saml/src/main/java/org/wso2/carbon/identity/sso/saml/processors/SPInitSSOAuthnRequestProcessor.java +++ b/components/org.wso2.carbon.identity.sso.saml/src/main/java/org/wso2/carbon/identity/sso/saml/processors/SPInitSSOAuthnRequestProcessor.java @@ -1,8 +1,8 @@ /* - * Copyright (c) 2010, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * Copyright (c) (2010-2023), WSO2 LLC. (http://www.wso2.com). * - * WSO2 Inc. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except * in compliance with the License. * You may obtain a copy of the License at * @@ -15,6 +15,7 @@ * specific language governing permissions and limitations * under the License. */ + package org.wso2.carbon.identity.sso.saml.processors; import org.apache.commons.lang.StringUtils; @@ -22,12 +23,10 @@ import org.apache.commons.logging.LogFactory; import org.opensaml.saml.saml2.core.Response; import org.wso2.carbon.context.PrivilegedCarbonContext; -import org.wso2.carbon.context.RegistryType; import org.wso2.carbon.identity.base.IdentityException; import org.wso2.carbon.identity.core.model.SAMLSSOServiceProviderDO; -import org.wso2.carbon.identity.core.persistence.IdentityPersistenceManager; -import org.wso2.carbon.identity.core.util.IdentityTenantUtil; +import org.wso2.carbon.identity.core.util.IdentityTenantUtil; import org.wso2.carbon.identity.sso.saml.SAMLSSOConstants; import org.wso2.carbon.identity.sso.saml.SSOServiceProviderConfigManager; import org.wso2.carbon.identity.sso.saml.builders.ErrorResponseBuilder; @@ -35,9 +34,9 @@ import org.wso2.carbon.identity.sso.saml.builders.SAMLArtifactBuilder; import org.wso2.carbon.identity.sso.saml.dto.SAMLSSOAuthnReqDTO; import org.wso2.carbon.identity.sso.saml.dto.SAMLSSORespDTO; +import org.wso2.carbon.identity.sso.saml.internal.IdentitySAMLSSOServiceComponentHolder; import org.wso2.carbon.identity.sso.saml.session.SSOSessionPersistenceManager; import org.wso2.carbon.identity.sso.saml.util.SAMLSSOUtil; -import org.wso2.carbon.registry.core.Registry; import org.wso2.carbon.registry.core.utils.UUIDGenerator; import java.util.ArrayList; @@ -219,10 +218,9 @@ private SAMLSSOServiceProviderDO getServiceProviderConfig(SAMLSSOAuthnReqDTO aut if (ssoIdpConfigs == null) { IdentityTenantUtil.initializeRegistry(PrivilegedCarbonContext.getThreadLocalCarbonContext() .getTenantId(), PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain()); - IdentityPersistenceManager persistenceManager = IdentityPersistenceManager.getPersistanceManager(); - Registry registry = (Registry) PrivilegedCarbonContext.getThreadLocalCarbonContext().getRegistry - (RegistryType.SYSTEM_CONFIGURATION); - ssoIdpConfigs = persistenceManager.getServiceProvider(registry, authnReqDTO.getIssuer()); + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + ssoIdpConfigs = IdentitySAMLSSOServiceComponentHolder.getInstance().getSAMLSSOServiceProviderManager() + .getServiceProvider(authnReqDTO.getIssuer(), tenantId); authnReqDTO.setStratosDeployment(false); // not stratos } else { authnReqDTO.setStratosDeployment(true); // stratos deployment diff --git a/components/org.wso2.carbon.identity.sso.saml/src/main/java/org/wso2/carbon/identity/sso/saml/servlet/SAMLSSOProviderServlet.java b/components/org.wso2.carbon.identity.sso.saml/src/main/java/org/wso2/carbon/identity/sso/saml/servlet/SAMLSSOProviderServlet.java index 9f136e565..96540bcf4 100644 --- a/components/org.wso2.carbon.identity.sso.saml/src/main/java/org/wso2/carbon/identity/sso/saml/servlet/SAMLSSOProviderServlet.java +++ b/components/org.wso2.carbon.identity.sso.saml/src/main/java/org/wso2/carbon/identity/sso/saml/servlet/SAMLSSOProviderServlet.java @@ -1,8 +1,8 @@ /* - * Copyright (c) 2010, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * Copyright (c) (2010-2023), WSO2 LLC. (http://www.wso2.com). * - * WSO2 Inc. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except * in compliance with the License. * You may obtain a copy of the License at * @@ -15,6 +15,7 @@ * specific language governing permissions and limitations * under the License. */ + package org.wso2.carbon.identity.sso.saml.servlet; import org.apache.commons.lang.StringUtils; @@ -25,7 +26,6 @@ import org.opensaml.core.xml.XMLObject; import org.owasp.encoder.Encode; import org.wso2.carbon.context.PrivilegedCarbonContext; -import org.wso2.carbon.context.RegistryType; import org.wso2.carbon.core.SameSiteCookie; import org.wso2.carbon.core.ServletCookie; import org.wso2.carbon.identity.application.authentication.framework.AuthenticatorFlowStatus; @@ -50,7 +50,6 @@ import org.wso2.carbon.identity.core.URLBuilderException; import org.wso2.carbon.identity.core.model.IdentityCookieConfig; import org.wso2.carbon.identity.core.model.SAMLSSOServiceProviderDO; -import org.wso2.carbon.identity.core.persistence.IdentityPersistenceManager; import org.wso2.carbon.identity.core.util.IdentityTenantUtil; import org.wso2.carbon.identity.core.util.IdentityUtil; import org.wso2.carbon.identity.sso.saml.FrontChannelSLOParticipantInfo; @@ -77,12 +76,12 @@ import org.wso2.carbon.identity.sso.saml.exception.IdentitySAML2ClientException; import org.wso2.carbon.identity.sso.saml.exception.IdentitySAML2SSOException; import org.wso2.carbon.identity.sso.saml.internal.IdentitySAMLSSOServiceComponent; +import org.wso2.carbon.identity.sso.saml.internal.IdentitySAMLSSOServiceComponentHolder; import org.wso2.carbon.identity.sso.saml.session.SSOSessionPersistenceManager; import org.wso2.carbon.identity.sso.saml.session.SessionInfoData; import org.wso2.carbon.identity.sso.saml.util.SAMLSOAPUtils; import org.wso2.carbon.identity.sso.saml.util.SAMLSSOUtil; import org.wso2.carbon.idp.mgt.util.IdPManagementUtil; -import org.wso2.carbon.registry.core.Registry; import org.wso2.carbon.registry.core.utils.UUIDGenerator; import org.wso2.carbon.user.api.UserStoreException; import org.wso2.carbon.utils.multitenancy.MultitenantConstants; @@ -1799,10 +1798,8 @@ private SAMLSSOServiceProviderDO getServiceProviderConfig(SAMLSSOAuthnReqDTO aut privilegedCarbonContext.setTenantId(tenantId); privilegedCarbonContext.setTenantDomain(tenantDomain); - IdentityPersistenceManager persistenceManager = IdentityPersistenceManager.getPersistanceManager(); - Registry registry = (Registry) PrivilegedCarbonContext.getThreadLocalCarbonContext().getRegistry - (RegistryType.SYSTEM_CONFIGURATION); - serviceProviderConfigs = persistenceManager.getServiceProvider(registry, issuer); + serviceProviderConfigs = IdentitySAMLSSOServiceComponentHolder.getInstance() + .getSAMLSSOServiceProviderManager().getServiceProvider(issuer, tenantId); authnReqDTO.setStratosDeployment(false); // not stratos } catch (IdentityException e) { throw new IdentitySAML2SSOException("Error occurred while retrieving SAML service provider for " diff --git a/components/org.wso2.carbon.identity.sso.saml/src/main/java/org/wso2/carbon/identity/sso/saml/util/SAMLSSOUtil.java b/components/org.wso2.carbon.identity.sso.saml/src/main/java/org/wso2/carbon/identity/sso/saml/util/SAMLSSOUtil.java index 34e2603c9..d152bb25e 100644 --- a/components/org.wso2.carbon.identity.sso.saml/src/main/java/org/wso2/carbon/identity/sso/saml/util/SAMLSSOUtil.java +++ b/components/org.wso2.carbon.identity.sso.saml/src/main/java/org/wso2/carbon/identity/sso/saml/util/SAMLSSOUtil.java @@ -1,8 +1,8 @@ /* - * Copyright (c) 2010, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * Copyright (c) (2010-2023), WSO2 LLC. (http://www.wso2.com). * - * WSO2 Inc. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except * in compliance with the License. * You may obtain a copy of the License at * @@ -73,10 +73,10 @@ import org.wso2.carbon.identity.base.IdentityException; import org.wso2.carbon.identity.base.IdentityRuntimeException; import org.wso2.carbon.identity.core.IdentityRegistryResources; +import org.wso2.carbon.identity.core.SAMLSSOServiceProviderManager; import org.wso2.carbon.identity.core.ServiceURLBuilder; import org.wso2.carbon.identity.core.URLBuilderException; import org.wso2.carbon.identity.core.model.SAMLSSOServiceProviderDO; -import org.wso2.carbon.identity.core.persistence.IdentityPersistenceManager; import org.wso2.carbon.identity.core.util.IdentityTenantUtil; import org.wso2.carbon.identity.core.util.IdentityUtil; import org.wso2.carbon.identity.saml.common.util.SAMLInitializer; @@ -98,6 +98,7 @@ import org.wso2.carbon.identity.sso.saml.exception.IdentitySAML2ClientException; import org.wso2.carbon.identity.sso.saml.exception.IdentitySAML2SSOException; import org.wso2.carbon.identity.sso.saml.extension.SAMLExtensionProcessor; +import org.wso2.carbon.identity.sso.saml.internal.IdentitySAMLSSOServiceComponentHolder; import org.wso2.carbon.identity.sso.saml.processors.IdPInitLogoutRequestProcessor; import org.wso2.carbon.identity.sso.saml.processors.IdPInitSSOAuthnRequestProcessor; import org.wso2.carbon.identity.sso.saml.processors.SPInitLogoutRequestProcessor; @@ -1842,10 +1843,8 @@ public static boolean isSAMLIssuerExists(String issuerName, String tenantDomain) privilegedCarbonContext.setTenantDomain(tenantDomain); IdentityTenantUtil.initializeRegistry(tenantId, tenantDomain); - IdentityPersistenceManager persistenceManager = IdentityPersistenceManager.getPersistanceManager(); - Registry registry = (Registry) PrivilegedCarbonContext.getThreadLocalCarbonContext().getRegistry - (RegistryType.SYSTEM_CONFIGURATION); - return persistenceManager.isServiceProviderExists(registry, issuerName); + return IdentitySAMLSSOServiceComponentHolder.getInstance().getSAMLSSOServiceProviderManager() + .isServiceProviderExists(issuerName, tenantId); } catch (IdentityException e) { throw new IdentitySAML2SSOException("Error occurred while validating existence of SAML service provider " + "'" + issuerName + "' in the tenant domain '" + tenantDomain + "'"); @@ -1900,10 +1899,8 @@ public static boolean validateACS(String tenantDomain, String issuerName, String privilegedCarbonContext.setTenantId(tenantId); privilegedCarbonContext.setTenantDomain(tenantDomain); - IdentityPersistenceManager persistenceManager = IdentityPersistenceManager.getPersistanceManager(); - Registry registry = (Registry) PrivilegedCarbonContext.getThreadLocalCarbonContext().getRegistry - (RegistryType.SYSTEM_CONFIGURATION); - SAMLSSOServiceProviderDO spDO=persistenceManager.getServiceProvider(registry, issuerName); + SAMLSSOServiceProviderDO spDO = IdentitySAMLSSOServiceComponentHolder.getInstance() + .getSAMLSSOServiceProviderManager().getServiceProvider(issuerName, tenantId); if (StringUtils.isBlank(requestedACSUrl) || !spDO.getAssertionConsumerUrlList().contains (requestedACSUrl)) { String msg = "ALERT: Invalid Assertion Consumer URL value '" + requestedACSUrl + "' in the " + @@ -2684,10 +2681,8 @@ private static SAMLSSOServiceProviderDO getSAMLServiceProviderFromRegistry(Strin privilegedCarbonContext.setTenantDomain(tenantDomain); IdentityTenantUtil.getTenantRegistryLoader().loadTenantRegistry(tenantId); - IdentityPersistenceManager persistenceManager = IdentityPersistenceManager.getPersistanceManager(); - Registry registry = (Registry) PrivilegedCarbonContext.getThreadLocalCarbonContext(). - getRegistry(RegistryType.SYSTEM_CONFIGURATION); - return persistenceManager.getServiceProvider(registry, issuer); + return IdentitySAMLSSOServiceComponentHolder.getInstance().getSAMLSSOServiceProviderManager() + .getServiceProvider(issuer, tenantId); } catch (IdentityException | RegistryException e) { throw new IdentitySAML2SSOException("Error occurred while retrieving SAML service provider for " diff --git a/components/org.wso2.carbon.identity.sso.saml/src/test/java/org/wso2/carbon/identity/sso/saml/admin/SAMLSSOConfigAdminTest.java b/components/org.wso2.carbon.identity.sso.saml/src/test/java/org/wso2/carbon/identity/sso/saml/admin/SAMLSSOConfigAdminTest.java index fc76a3a68..0640cf921 100644 --- a/components/org.wso2.carbon.identity.sso.saml/src/test/java/org/wso2/carbon/identity/sso/saml/admin/SAMLSSOConfigAdminTest.java +++ b/components/org.wso2.carbon.identity.sso.saml/src/test/java/org/wso2/carbon/identity/sso/saml/admin/SAMLSSOConfigAdminTest.java @@ -1,8 +1,8 @@ /* - * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * Copyright (c) (2017-2023), WSO2 LLC. (http://www.wso2.com). * - * WSO2 Inc. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except * in compliance with the License. * You may obtain a copy of the License at * @@ -29,23 +29,23 @@ import org.testng.annotations.DataProvider; import org.testng.annotations.Test; import org.wso2.carbon.identity.base.IdentityException; +import org.wso2.carbon.identity.core.SAMLSSOServiceProviderManager; import org.wso2.carbon.identity.core.model.SAMLSSOServiceProviderDO; -import org.wso2.carbon.identity.core.persistence.IdentityPersistenceManager; import org.wso2.carbon.identity.sp.metadata.saml2.util.Parser; import org.wso2.carbon.identity.sso.saml.SSOServiceProviderConfigManager; import org.wso2.carbon.identity.sso.saml.TestUtils; import org.wso2.carbon.identity.sso.saml.dto.SAMLSSOServiceProviderDTO; import org.wso2.carbon.identity.sso.saml.exception.IdentitySAML2ClientException; -import org.wso2.carbon.registry.core.Registry; +import org.wso2.carbon.identity.sso.saml.internal.IdentitySAMLSSOServiceComponentHolder; import org.wso2.carbon.registry.core.session.UserRegistry; import org.wso2.carbon.utils.multitenancy.MultitenantConstants; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.MockitoAnnotations.initMocks; import static org.powermock.api.mockito.PowerMockito.*; -@PrepareForTest({IdentityPersistenceManager.class, SSOServiceProviderConfigManager.class, +@PrepareForTest({IdentitySAMLSSOServiceComponentHolder.class, SSOServiceProviderConfigManager.class, SAMLSSOServiceProviderDO.class, Parser.class, UserRegistry.class, SAMLSSOConfigAdmin.class}) @PowerMockIgnore({"javax.xml.*", "org.xml.*", "org.apache.xerces.*", "org.w3c.dom.*"}) public class SAMLSSOConfigAdminTest extends PowerMockTestCase { @@ -57,7 +57,9 @@ public class SAMLSSOConfigAdminTest extends PowerMockTestCase { UserRegistry userRegistry; @Mock - private IdentityPersistenceManager identityPersistenceManager; + private SAMLSSOServiceProviderManager samlSSOServiceProviderManager; + + @Mock IdentitySAMLSSOServiceComponentHolder identitySAMLSSOServiceComponentHolder; @Mock SAMLSSOServiceProviderDO samlssoServiceProvDO; @@ -73,8 +75,11 @@ public void setUp() throws Exception { TestUtils.startTenantFlow(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME); samlssoConfigAdmin = new SAMLSSOConfigAdmin(userRegistry); - mockStatic(IdentityPersistenceManager.class); - when(IdentityPersistenceManager.getPersistanceManager()).thenReturn(identityPersistenceManager); + mockStatic(IdentitySAMLSSOServiceComponentHolder.class); + when(IdentitySAMLSSOServiceComponentHolder.getInstance()) + .thenReturn(identitySAMLSSOServiceComponentHolder); + when(identitySAMLSSOServiceComponentHolder.getSAMLSSOServiceProviderManager()) + .thenReturn(samlSSOServiceProviderManager); mockStatic(SAMLSSOServiceProviderDO.class); } @@ -88,7 +93,7 @@ public void testAddRelyingPartyServiceProvider() throws IdentityException { mockStatic(SSOServiceProviderConfigManager.class); when(SSOServiceProviderConfigManager.getInstance()).thenReturn(ssoServiceProviderConfigManager); - when(identityPersistenceManager.addServiceProvider(any(Registry.class), any(SAMLSSOServiceProviderDO.class))) + when(samlSSOServiceProviderManager.addServiceProvider(any(SAMLSSOServiceProviderDO.class), anyInt())) .thenReturn(true); SAMLSSOServiceProviderDTO samlssoServiceProviderDTO = new SAMLSSOServiceProviderDTO(); samlssoServiceProviderDTO.setIssuer("testUser"); @@ -131,8 +136,8 @@ public void testCreateSAMLSSOServiceProviderDOWithInvalidIssuerQualifier() throw public void testUploadRelyingPartyServiceProvider() throws Exception { String metadata = "metadata"; - when(identityPersistenceManager.addServiceProvider(any(Registry.class), any(SAMLSSOServiceProviderDO.class))). - thenReturn(true); + when(samlSSOServiceProviderManager.addServiceProvider(any(SAMLSSOServiceProviderDO.class), anyInt())) + .thenReturn(true); whenNew(SAMLSSOServiceProviderDO.class).withNoArguments().thenReturn(samlssoServiceProvDO); when(samlssoServiceProvDO.getIssuer()).thenReturn("issuer"); whenNew(Parser.class).withArguments(any(UserRegistry.class)).thenReturn(parser); @@ -147,7 +152,8 @@ public void testUploadRelyingPartyServiceProvider1() throws Exception { String metadata = "metadata"; whenNew(SAMLSSOServiceProviderDO.class).withNoArguments().thenReturn(samlssoServiceProvDO); when(samlssoServiceProvDO.getIssuer()).thenReturn("issuer"); - when(identityPersistenceManager.addServiceProvider(userRegistry, samlssoServiceProvDO)).thenReturn(false); + when(samlSSOServiceProviderManager.addServiceProvider(samlssoServiceProvDO, userRegistry.getTenantId())) + .thenReturn(false); whenNew(Parser.class).withArguments(any(UserRegistry.class)).thenReturn(parser); when(parser.parse(anyString(), any(SAMLSSOServiceProviderDO.class))).thenReturn(samlssoServiceProvDO); samlssoConfigAdmin.uploadRelyingPartyServiceProvider(metadata); @@ -157,7 +163,7 @@ public void testUploadRelyingPartyServiceProvider1() throws Exception { public void testUploadRelyingPartyServiceProvider2(String issuer) throws Exception { String metadata = "metadata"; - when(identityPersistenceManager.addServiceProvider(any(Registry.class), any(SAMLSSOServiceProviderDO.class))) + when(samlSSOServiceProviderManager.addServiceProvider(any(SAMLSSOServiceProviderDO.class), anyInt())) .thenReturn(true); whenNew(SAMLSSOServiceProviderDO.class).withNoArguments().thenReturn(samlssoServiceProvDO); when(samlssoServiceProvDO.getIssuer()).thenReturn(issuer); @@ -172,7 +178,7 @@ public void testGetServiceProviders() throws Exception { mockStatic(UserRegistry.class); SAMLSSOServiceProviderDO[] serviceProvidersList = new SAMLSSOServiceProviderDO[3]; when(userRegistry.getTenantId()).thenReturn(0); - when(identityPersistenceManager.getServiceProviders(any(UserRegistry.class))).thenReturn(serviceProvidersList); + when(samlSSOServiceProviderManager.getServiceProviders(anyInt())).thenReturn(serviceProvidersList); SAMLSSOServiceProviderDO samlssoServiceProviderDO = new SAMLSSOServiceProviderDO(); samlssoServiceProviderDO.setIssuer("issuer"); diff --git a/components/org.wso2.carbon.identity.sso.saml/src/test/java/org/wso2/carbon/identity/sso/saml/util/AssertionBuildingTest.java b/components/org.wso2.carbon.identity.sso.saml/src/test/java/org/wso2/carbon/identity/sso/saml/util/AssertionBuildingTest.java index 5d30a5a6e..7fa2d7ac6 100644 --- a/components/org.wso2.carbon.identity.sso.saml/src/test/java/org/wso2/carbon/identity/sso/saml/util/AssertionBuildingTest.java +++ b/components/org.wso2.carbon.identity.sso.saml/src/test/java/org/wso2/carbon/identity/sso/saml/util/AssertionBuildingTest.java @@ -1,8 +1,8 @@ /* - * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * Copyright (c) (2017-2023), WSO2 LLC. (http://www.wso2.com). * - * WSO2 Inc. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except * in compliance with the License. * You may obtain a copy of the License at * @@ -34,11 +34,9 @@ import org.powermock.modules.testng.PowerMockObjectFactory; import org.powermock.modules.testng.PowerMockTestCase; import org.testng.IObjectFactory; -import org.testng.annotations.BeforeTest; import org.testng.annotations.DataProvider; import org.testng.annotations.ObjectFactory; import org.testng.annotations.Test; -import org.wso2.carbon.context.internal.OSGiDataHolder; import org.wso2.carbon.core.util.KeyStoreManager; import org.wso2.carbon.identity.application.common.model.FederatedAuthenticatorConfig; import org.wso2.carbon.identity.application.common.model.IdentityProvider; @@ -46,8 +44,8 @@ import org.wso2.carbon.identity.application.common.util.IdentityApplicationConstants; import org.wso2.carbon.identity.base.IdentityException; import org.wso2.carbon.identity.common.testng.WithCarbonHome; +import org.wso2.carbon.identity.core.SAMLSSOServiceProviderManager; import org.wso2.carbon.identity.core.model.SAMLSSOServiceProviderDO; -import org.wso2.carbon.identity.core.persistence.IdentityPersistenceManager; import org.wso2.carbon.identity.core.util.IdentityTenantUtil; import org.wso2.carbon.identity.core.util.IdentityUtil; import org.wso2.carbon.identity.saml.common.util.SAMLInitializer; @@ -57,13 +55,9 @@ import org.wso2.carbon.identity.sso.saml.TestConstants; import org.wso2.carbon.identity.sso.saml.TestUtils; import org.wso2.carbon.identity.sso.saml.dto.SAMLSSOAuthnReqDTO; +import org.wso2.carbon.identity.sso.saml.internal.IdentitySAMLSSOServiceComponentHolder; import org.wso2.carbon.identity.sso.saml.validators.SSOAuthnRequestValidator; import org.wso2.carbon.idp.mgt.IdentityProviderManager; -import org.wso2.carbon.registry.core.Registry; -import org.wso2.carbon.registry.core.exceptions.RegistryException; -import org.wso2.carbon.registry.core.service.RegistryService; -import org.wso2.carbon.registry.core.session.UserRegistry; -import org.wso2.carbon.user.api.UserStoreException; import org.wso2.carbon.user.core.service.RealmService; import org.wso2.carbon.user.core.tenant.TenantManager; import org.wso2.carbon.utils.multitenancy.MultitenantConstants; @@ -75,8 +69,8 @@ import java.util.Map; import java.util.concurrent.TimeUnit; -import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyBoolean; +import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; import static org.powermock.api.mockito.PowerMockito.mockStatic; @@ -90,8 +84,8 @@ /** * Tests Assertion building functionality. */ -@PrepareForTest({IdentityUtil.class, IdentityTenantUtil.class, IdentityProviderManager.class, OSGiDataHolder.class, - SSOServiceProviderConfigManager.class, IdentityPersistenceManager.class}) +@PrepareForTest({IdentityUtil.class, IdentityTenantUtil.class, IdentityProviderManager.class, + SSOServiceProviderConfigManager.class, IdentitySAMLSSOServiceComponentHolder.class}) @WithCarbonHome @PowerMockIgnore({"javax.net.*", "javax.xml.*", "org.xml.*", "org.w3c.dom.*", "javax.security.*", "org.mockito.*"}) @@ -106,7 +100,10 @@ public IObjectFactory getObjectFactory() { private RealmService realmService; @Mock - private IdentityPersistenceManager identityPersistenceManager; + private IdentitySAMLSSOServiceComponentHolder identitySAMLSSOServiceComponentHolder; + + @Mock + private SAMLSSOServiceProviderManager samlssoServiceProviderManager; @Mock private TenantManager tenantManager; @@ -129,20 +126,6 @@ public IObjectFactory getObjectFactory() { @Mock private X509Credential x509Credential; - @Mock - private UserRegistry registry; - - @Mock - private RegistryService registryService; - - private static OSGiDataHolder dataHolder = OSGiDataHolder.getInstance(); - - @BeforeTest - public void setUp() throws Exception { - - TestUtils.startTenantFlow(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME); - } - @Test public void testBuildAssertion() throws Exception { @@ -211,7 +194,6 @@ public void validateACS() throws Exception { @Test public void validateACSWithoutIssuer() throws Exception { - setRegistryAndTenantDomain(); prepareIdentityPersistentManager(TestConstants.ATTRIBUTE_CONSUMER_INDEX, TestConstants.TRAVELOCITY_ISSUER, Collections.emptyList()); boolean isACSValied = SAMLSSOUtil.validateACS(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME, TestConstants @@ -224,7 +206,6 @@ public void validateACSWithACSInSP() throws Exception { List acs = new ArrayList(); acs.add(TestConstants.ACS_URL); - setRegistryAndTenantDomain(); prepareIdentityPersistentManager(TestConstants.ATTRIBUTE_CONSUMER_INDEX, TestConstants.TRAVELOCITY_ISSUER, acs); boolean isACSValied = SAMLSSOUtil.validateACS(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME, TestConstants .TRAVELOCITY_ISSUER, TestConstants.ACS_URL); @@ -385,21 +366,13 @@ private void prepareIdentityPersistentManager(String attrConsumerIndex, String i samlssoServiceProviderDO.setEnableAttributesByDefault(true); samlssoServiceProviderDO.setIssuer(issuer); samlssoServiceProviderDO.setAssertionConsumerUrls(acsList); - when(identityPersistenceManager.getServiceProvider(any(Registry.class), eq(issuer))) + when(samlssoServiceProviderManager.getServiceProvider(eq(issuer), anyInt())) .thenReturn(samlssoServiceProviderDO); - mockStatic(IdentityPersistenceManager.class); - when(IdentityPersistenceManager.getPersistanceManager()).thenReturn(identityPersistenceManager); - } - - private void setRegistryAndTenantDomain() throws UserStoreException, IdentityException, RegistryException { - - when(realmService.getTenantManager()).thenReturn(tenantManager); - SAMLSSOUtil.setRealmService(realmService); - SAMLSSOUtil.setTenantDomainInThreadLocal(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME); - - mockStatic(OSGiDataHolder.class); - dataHolder.setRegistryService(registryService); - when(registryService.getConfigSystemRegistry(eq(0))).thenReturn(registry); + mockStatic(IdentitySAMLSSOServiceComponentHolder.class); + when(IdentitySAMLSSOServiceComponentHolder.getInstance()) + .thenReturn(identitySAMLSSOServiceComponentHolder); + when(identitySAMLSSOServiceComponentHolder.getSAMLSSOServiceProviderManager()) + .thenReturn(samlssoServiceProviderManager); } @Test diff --git a/components/org.wso2.carbon.identity.sso.saml/src/test/java/org/wso2/carbon/identity/sso/saml/util/SAMLSSOUtilTest.java b/components/org.wso2.carbon.identity.sso.saml/src/test/java/org/wso2/carbon/identity/sso/saml/util/SAMLSSOUtilTest.java index f65ed2395..846ebcecb 100644 --- a/components/org.wso2.carbon.identity.sso.saml/src/test/java/org/wso2/carbon/identity/sso/saml/util/SAMLSSOUtilTest.java +++ b/components/org.wso2.carbon.identity.sso.saml/src/test/java/org/wso2/carbon/identity/sso/saml/util/SAMLSSOUtilTest.java @@ -1,8 +1,8 @@ /* - * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * Copyright (c) (2017-2023), WSO2 LLC. (http://www.wso2.com). * - * WSO2 Inc. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except * in compliance with the License. * You may obtain a copy of the License at * @@ -39,11 +39,11 @@ import org.wso2.carbon.identity.application.common.util.IdentityApplicationManagementUtil; import org.wso2.carbon.identity.base.IdentityConstants; import org.wso2.carbon.identity.base.IdentityException; +import org.wso2.carbon.identity.core.SAMLSSOServiceProviderManager; import org.wso2.carbon.identity.core.ServiceURL; import org.wso2.carbon.identity.core.ServiceURLBuilder; import org.wso2.carbon.identity.core.URLBuilderException; import org.wso2.carbon.identity.core.model.SAMLSSOServiceProviderDO; -import org.wso2.carbon.identity.core.persistence.IdentityPersistenceManager; import org.wso2.carbon.identity.core.util.IdentityTenantUtil; import org.wso2.carbon.identity.core.util.IdentityUtil; import org.wso2.carbon.identity.sso.saml.SAMLSSOConstants; @@ -53,11 +53,11 @@ import org.wso2.carbon.identity.sso.saml.builders.X509CredentialImpl; import org.wso2.carbon.identity.sso.saml.exception.IdentitySAML2SSOException; import org.wso2.carbon.identity.sso.saml.extension.eidas.EidasExtensionProcessor; +import org.wso2.carbon.identity.sso.saml.internal.IdentitySAMLSSOServiceComponentHolder; import org.wso2.carbon.identity.sso.saml.session.SSOSessionPersistenceManager; import org.wso2.carbon.identity.sso.saml.session.SessionInfoData; import org.wso2.carbon.idp.mgt.IdentityProviderManagementException; import org.wso2.carbon.idp.mgt.IdentityProviderManager; -import org.wso2.carbon.registry.core.Registry; import org.wso2.carbon.user.api.UserStoreException; import org.wso2.carbon.user.core.service.RealmService; import org.wso2.carbon.user.core.tenant.TenantManager; @@ -67,7 +67,7 @@ import java.util.List; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyBoolean; +import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; import static org.powermock.api.mockito.PowerMockito.mockStatic; @@ -83,7 +83,7 @@ * Unit test cases for SAMLSSOUtil. */ @PrepareForTest({IdentityProviderManager.class, IdentityUtil.class, IdentityApplicationManagementUtil.class, - KeyStoreManager.class, IdentityPersistenceManager.class, SSOServiceProviderConfigManager.class, + KeyStoreManager.class, IdentitySAMLSSOServiceComponentHolder.class, SSOServiceProviderConfigManager.class, IdentityTenantUtil.class, ServiceURLBuilder.class, IdentityConstants.class, FrameworkServiceComponent.class}) @PowerMockIgnore({"javax.xml.*", "org.xml.*", "org.w3c.dom.*", "org.apache.xerces.*"}) public class SAMLSSOUtilTest extends PowerMockTestCase { @@ -111,7 +111,10 @@ public class SAMLSSOUtilTest extends PowerMockTestCase { private SessionInfoData sessionInfoData; @Mock - private IdentityPersistenceManager identityPersistenceManager; + private SAMLSSOServiceProviderManager samlSSOServiceProviderManager; + + @Mock + private IdentitySAMLSSOServiceComponentHolder identitySAMLSSOServiceComponentHolder; @Mock private SSOServiceProviderConfigManager ssoServiceProviderConfigManager; @@ -154,11 +157,14 @@ private void prepareForGetSPConfig() throws Exception { samlssoServiceProviderDO.setIssuerQualifier(TestConstants.ISSUER_QUALIFIER); samlssoServiceProviderDO.setIdpEntityIDAlias(TestConstants.IDP_ENTITY_ID_ALIAS); - when(identityPersistenceManager.getServiceProvider(any(Registry.class), anyString())) + when(samlSSOServiceProviderManager.getServiceProvider(anyString(), anyInt())) .thenReturn(samlssoServiceProviderDO); - mockStatic(IdentityPersistenceManager.class); - when(IdentityPersistenceManager.getPersistanceManager()).thenReturn(identityPersistenceManager); - when(identityPersistenceManager.isServiceProviderExists(any(Registry.class), anyString())).thenReturn(true); + mockStatic(IdentitySAMLSSOServiceComponentHolder.class); + when(IdentitySAMLSSOServiceComponentHolder.getInstance()) + .thenReturn(identitySAMLSSOServiceComponentHolder); + when(identitySAMLSSOServiceComponentHolder.getSAMLSSOServiceProviderManager()) + .thenReturn(samlSSOServiceProviderManager); + when(samlSSOServiceProviderManager.isServiceProviderExists(anyString(), anyInt())).thenReturn(true); mockStatic(SSOServiceProviderConfigManager.class); when(SSOServiceProviderConfigManager.getInstance()).thenReturn(ssoServiceProviderConfigManager); diff --git a/pom.xml b/pom.xml index a44fad625..54e521e00 100644 --- a/pom.xml +++ b/pom.xml @@ -452,8 +452,8 @@ 4.9.0 4.9.0 - 5.25.157 - [5.15.0, 7.0.0) + 5.25.234 + [5.25.234, 7.0.0) 1.0.0