Skip to content

Commit

Permalink
Introduce SAMLSSOServiceProviderDAO Factory class
Browse files Browse the repository at this point in the history
  • Loading branch information
Osara-B committed Dec 20, 2024
1 parent 44b8576 commit d7d8318
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
package org.wso2.carbon.identity.core;

import org.wso2.carbon.identity.base.IdentityException;
import org.wso2.carbon.identity.core.dao.SAMLSSOPersistenceManagerFactory;
import org.wso2.carbon.identity.core.dao.SAMLSSOServiceProviderDAO;
import org.wso2.carbon.identity.core.dao.SAMLSSOServiceProviderRegistryDAOImpl;
import org.wso2.carbon.identity.core.model.SAMLSSOServiceProviderDO;

/**
Expand All @@ -29,7 +29,8 @@
*/
public class SAMLSSOServiceProviderManager {

private static SAMLSSOServiceProviderDAO serviceProviderDAO = new SAMLSSOServiceProviderRegistryDAOImpl();
SAMLSSOPersistenceManagerFactory samlSSOPersistenceManagerFactory = new SAMLSSOPersistenceManagerFactory();
SAMLSSOServiceProviderDAO serviceProviderDAO = samlSSOPersistenceManagerFactory.getSAMLServiceProviderPersistenceManager();

/**
* Add a saml service provider.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
/**
* This class is used for managing SAML SSO service providers in the Registry.
*/
public class SAMLSSOServiceProviderRegistryDAOImpl extends AbstractDAO<SAMLSSOServiceProviderDO>
public class RegistrySAMLSSOServiceProviderDAOImpl extends AbstractDAO<SAMLSSOServiceProviderDO>
implements SAMLSSOServiceProviderDAO {

private static final String CERTIFICATE_PROPERTY_NAME = "CERTIFICATE";
Expand All @@ -67,9 +67,9 @@ public class SAMLSSOServiceProviderRegistryDAOImpl extends AbstractDAO<SAMLSSOSe
"META.`VALUE` FROM SP_INBOUND_AUTH INBOUND, SP_APP SP, SP_METADATA META WHERE SP.ID = INBOUND.APP_ID AND " +
"SP.ID = META.SP_ID AND META.NAME = ? AND INBOUND.INBOUND_AUTH_KEY = ? AND META.TENANT_ID = ?";

private static Log log = LogFactory.getLog(SAMLSSOServiceProviderRegistryDAOImpl.class);
private static Log log = LogFactory.getLog(RegistrySAMLSSOServiceProviderDAOImpl.class);

public SAMLSSOServiceProviderRegistryDAOImpl () {
public RegistrySAMLSSOServiceProviderDAOImpl() {
}

protected SAMLSSOServiceProviderDO resourceToObject(Resource resource) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright (c) 2024, 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.core.dao;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.identity.core.util.IdentityUtil;

/**
* Factory class to create instances of SAMLSSOServiceProviderDAO based on the configured storage type.
*/
public class SAMLSSOPersistenceManagerFactory {

private static final Log LOG = LogFactory.getLog(SAMLSSOPersistenceManagerFactory.class);
private static String SAML_STORAGE_TYPE = IdentityUtil.getProperty("DataStorageType.SAML");
private static final String HYBRID = "hybrid";
private static final String DATABASE = "database";

public SAMLSSOServiceProviderDAO getSAMLServiceProviderPersistenceManager() {

SAMLSSOServiceProviderDAO samlSSOServiceProviderDAO = new RegistrySAMLSSOServiceProviderDAOImpl();
if (StringUtils.isNotBlank(SAML_STORAGE_TYPE)) {
switch (SAML_STORAGE_TYPE) {
case HYBRID:
//Initialize hybrid SAML storage
LOG.info("Hybrid SAML storage initialized.");
break;
case DATABASE:
//Initialize JDBC SAML storage
LOG.info("JDBC based SAML storage initialized.");
break;
}
}

if (LOG.isDebugEnabled()) {
LOG.debug(
"SAML SSO Service Provider DAO initialized with the type: " + samlSSOServiceProviderDAO.getClass());
}
return samlSSOServiceProviderDAO;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import org.wso2.carbon.identity.core.dao.OpenIDAdminDAO;
import org.wso2.carbon.identity.core.dao.OpenIDUserDAO;
import org.wso2.carbon.identity.core.dao.ParameterDAO;
import org.wso2.carbon.identity.core.dao.SAMLSSOPersistenceManagerFactory;
import org.wso2.carbon.identity.core.dao.SAMLSSOServiceProviderDAO;
import org.wso2.carbon.identity.core.dao.SAMLSSOServiceProviderRegistryDAOImpl;
import org.wso2.carbon.identity.core.dao.XMPPSettingsDAO;
import org.wso2.carbon.identity.core.model.OpenIDAdminDO;
import org.wso2.carbon.identity.core.model.OpenIDUserDO;
Expand All @@ -36,7 +36,8 @@
public class IdentityPersistenceManager {

private static IdentityPersistenceManager manager = new IdentityPersistenceManager();
private static SAMLSSOServiceProviderDAO serviceProviderDAO = new SAMLSSOServiceProviderRegistryDAOImpl();
SAMLSSOPersistenceManagerFactory samlSSOPersistenceManagerFactory = new SAMLSSOPersistenceManagerFactory();
SAMLSSOServiceProviderDAO serviceProviderDAO = samlSSOPersistenceManagerFactory.getSAMLServiceProviderPersistenceManager();

private IdentityPersistenceManager() {
}
Expand Down Expand Up @@ -266,8 +267,7 @@ public SAMLSSOServiceProviderDO[] getServiceProviders(Registry registry)
throws IdentityException {

int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
SAMLSSOServiceProviderDAO serviceProviderDOA = new SAMLSSOServiceProviderRegistryDAOImpl();
return serviceProviderDOA.getServiceProviders(tenantId);
return serviceProviderDAO.getServiceProviders(tenantId);
}

public boolean removeServiceProvider(Registry registry, String issuer) throws IdentityException {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright (c) 2024, 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.core.dao;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.lang.reflect.Field;

import static org.testng.Assert.assertTrue;

public class SAMLSSOPersistenceManagerFactoryTest {

private SAMLSSOPersistenceManagerFactory factory;

@BeforeMethod
public void setUp() {

factory = new SAMLSSOPersistenceManagerFactory();

}

@AfterMethod
public void tearDown() throws Exception {

setPrivateStaticField(SAMLSSOPersistenceManagerFactory.class, "SAML_STORAGE_TYPE", "");
factory = null;
}

@Test
public void testGetSAMLServiceProviderPersistenceManagerWithDefaultStorage() throws Exception {

setPrivateStaticField(SAMLSSOPersistenceManagerFactory.class, "SAML_STORAGE_TYPE", "database");
SAMLSSOServiceProviderDAO samlSSOServiceProviderDAO = factory.getSAMLServiceProviderPersistenceManager();
// assertTrue(samlSSOServiceProviderDAO instanceof JDBCSAMLSSOServiceProviderDAOImpl);
}

@Test
public void testGetSAMLServiceProviderPersistenceManagerWithRegistryStorage() throws Exception {

setPrivateStaticField(SAMLSSOPersistenceManagerFactory.class, "SAML_STORAGE_TYPE", "registry");
SAMLSSOServiceProviderDAO samlSSOServiceProviderDAO = factory.getSAMLServiceProviderPersistenceManager();
assertTrue(samlSSOServiceProviderDAO instanceof RegistrySAMLSSOServiceProviderDAOImpl);
}

@Test
public void testGetSAMLServiceProviderPersistenceManagerWithHybridStorage() throws Exception {

setPrivateStaticField(SAMLSSOPersistenceManagerFactory.class, "SAML_STORAGE_TYPE", "hybrid");
SAMLSSOServiceProviderDAO samlSSOServiceProviderDAO = factory.getSAMLServiceProviderPersistenceManager();
// assertTrue(samlSSOServiceProviderDAO instanceof JDBCSAMLSSOServiceProviderDAOImpl);
}

private void setPrivateStaticField(Class<?> clazz, String fieldName, Object newValue)
throws NoSuchFieldException, IllegalAccessException {

Field field = clazz.getDeclaredField(fieldName);
field.setAccessible(true);
field.set(null, newValue);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
*/
public class SAMLSSOServiceProviderDAOTest {

private SAMLSSOServiceProviderRegistryDAOImpl objUnderTest;
private RegistrySAMLSSOServiceProviderDAOImpl objUnderTest;
private boolean transactionStarted = false;

private Registry mockRegistry;
Expand Down Expand Up @@ -106,7 +106,7 @@ public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
}
}).when(mockRegistry).beginTransaction();

objUnderTest = new SAMLSSOServiceProviderRegistryDAOImpl();
objUnderTest = new RegistrySAMLSSOServiceProviderDAOImpl();
identityTenantUtil = mockStatic(IdentityTenantUtil.class);
when(IdentityTenantUtil.getRegistryService()).thenReturn(mockRegistryService);
when(mockRegistryService.getConfigSystemRegistry(TENANT_ID)).thenReturn((UserRegistry) mockRegistry);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<class name="org.wso2.carbon.identity.core.util.IdentityUtilTest"/>
<class name="org.wso2.carbon.identity.core.util.IdentityConfigParserTest"/>
<class name="org.wso2.carbon.identity.core.dao.SAMLSSOServiceProviderDAOTest"/>
<class name="org.wso2.carbon.identity.core.dao.SAMLSSOPersistenceManagerFactoryTest"/>
<class name="org.wso2.carbon.identity.core.internal.DefaultServiceURLBuilderTest"/>
<class name="org.wso2.carbon.identity.core.cache.BaseCacheTest"/>
<class name="org.wso2.carbon.identity.core.ThreadLocalAwareThreadPoolExecutorTest"/>
Expand Down

0 comments on commit d7d8318

Please sign in to comment.