Skip to content

Commit

Permalink
Add SAMLSSOPersistenceManagerFactory class and refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
Osara-B committed Dec 19, 2024
1 parent 59aab15 commit 95929a8
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 153 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,65 +18,19 @@

package org.wso2.carbon.identity.core;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
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.JDBCSAMLSSOServiceProviderDAOImpl;
import org.wso2.carbon.identity.core.dao.RegistrySAMLSSOServiceProviderDAOImpl;
import org.wso2.carbon.identity.core.model.SAMLSSOServiceProviderDO;
import org.wso2.carbon.identity.core.util.IdentityTenantUtil;
import org.wso2.carbon.identity.core.util.IdentityUtil;
import org.wso2.carbon.registry.api.RegistryException;
import org.wso2.carbon.registry.core.Registry;

import static org.wso2.carbon.identity.core.dao.SAMLSSOServiceProviderConstants.SAML_STORAGE_CONFIG;

/**
* This class is used for managing SAML SSO providers. Adding, retrieving and removing service
* providers are supported here.
*/
public class SAMLSSOServiceProviderManager {

private static final Log LOG = LogFactory.getLog(SAMLSSOServiceProviderManager.class);
private static final String SAML_STORAGE_TYPE = IdentityUtil.getProperty(SAML_STORAGE_CONFIG);
private static final String HYBRID = "hybrid";
private static final String REGISTRY = "registry";

/**
* Build the SAML service provider.
*
* @param tenantId Tenant ID.
* @return SAML service provider.
*/
private SAMLSSOServiceProviderDAO buildSAMLSSOProvider(int tenantId) throws IdentityException {

SAMLSSOServiceProviderDAO samlSSOServiceProviderDAO = new JDBCSAMLSSOServiceProviderDAOImpl(tenantId);
if (StringUtils.isNotBlank(SAML_STORAGE_TYPE)) {
switch (SAML_STORAGE_TYPE) {
case HYBRID:
LOG.info("Hybrid SAML storage initialized.");
break;
case REGISTRY:
try {
Registry registry = IdentityTenantUtil.getRegistryService().getConfigSystemRegistry(tenantId);
samlSSOServiceProviderDAO = new RegistrySAMLSSOServiceProviderDAOImpl(registry);
} catch (RegistryException e) {
LOG.error("Error while retrieving registry", e);
throw new IdentityException("Error while retrieving registry", e);
}
LOG.warn("Registry based SAML storage initialized.");
break;
}
}

if (LOG.isDebugEnabled()) {
LOG.debug(
"SAML SSO Service Provider DAO initialized with the type: " + samlSSOServiceProviderDAO.getClass());
}
return samlSSOServiceProviderDAO;
}
SAMLSSOPersistenceManagerFactory samlSSOPersistenceManagerFactory = new SAMLSSOPersistenceManagerFactory();
SAMLSSOServiceProviderDAO serviceProviderDAO = samlSSOPersistenceManagerFactory.buildSSOServiceProviderManager();

/**
* Add a saml service provider.
Expand All @@ -89,8 +43,7 @@ private SAMLSSOServiceProviderDAO buildSAMLSSOProvider(int tenantId) throws Iden
public boolean addServiceProvider(SAMLSSOServiceProviderDO serviceProviderDO, int tenantId)
throws IdentityException {

SAMLSSOServiceProviderDAO serviceProviderDAO = buildSAMLSSOProvider(tenantId);
return serviceProviderDAO.addServiceProvider(serviceProviderDO);
return serviceProviderDAO.addServiceProvider(serviceProviderDO, tenantId);
}

/**
Expand All @@ -105,8 +58,7 @@ public boolean addServiceProvider(SAMLSSOServiceProviderDO serviceProviderDO, in
public boolean updateServiceProvider(SAMLSSOServiceProviderDO serviceProviderDO, String currentIssuer, int tenantId)
throws IdentityException {

SAMLSSOServiceProviderDAO serviceProviderDAO = buildSAMLSSOProvider(tenantId);
return serviceProviderDAO.updateServiceProvider(serviceProviderDO, currentIssuer);
return serviceProviderDAO.updateServiceProvider(serviceProviderDO, currentIssuer, tenantId);
}

/**
Expand All @@ -119,8 +71,7 @@ public boolean updateServiceProvider(SAMLSSOServiceProviderDO serviceProviderDO,
public SAMLSSOServiceProviderDO[] getServiceProviders(int tenantId)
throws IdentityException {

SAMLSSOServiceProviderDAO serviceProviderDAO = buildSAMLSSOProvider(tenantId);
return serviceProviderDAO.getServiceProviders();
return serviceProviderDAO.getServiceProviders(tenantId);
}

/**
Expand All @@ -134,8 +85,7 @@ public SAMLSSOServiceProviderDO[] getServiceProviders(int tenantId)
public SAMLSSOServiceProviderDO getServiceProvider(String issuer, int tenantId)
throws IdentityException {

SAMLSSOServiceProviderDAO serviceProviderDAO = buildSAMLSSOProvider(tenantId);
return serviceProviderDAO.getServiceProvider(issuer);
return serviceProviderDAO.getServiceProvider(issuer, tenantId);

}

Expand All @@ -150,8 +100,7 @@ public SAMLSSOServiceProviderDO getServiceProvider(String issuer, int tenantId)
public boolean isServiceProviderExists(String issuer, int tenantId)
throws IdentityException {

SAMLSSOServiceProviderDAO serviceProviderDAO = buildSAMLSSOProvider(tenantId);
return serviceProviderDAO.isServiceProviderExists(issuer);
return serviceProviderDAO.isServiceProviderExists(issuer, tenantId);
}

/**
Expand All @@ -164,8 +113,7 @@ public boolean isServiceProviderExists(String issuer, int tenantId)
public boolean removeServiceProvider(String issuer, int tenantId)
throws IdentityException {

SAMLSSOServiceProviderDAO serviceProviderDAO = buildSAMLSSOProvider(tenantId);
return serviceProviderDAO.removeServiceProvider(issuer);
return serviceProviderDAO.removeServiceProvider(issuer, tenantId);
}

/**
Expand All @@ -180,7 +128,6 @@ public SAMLSSOServiceProviderDO uploadServiceProvider(SAMLSSOServiceProviderDO s
int tenantId)
throws IdentityException {

SAMLSSOServiceProviderDAO serviceProviderDAO = buildSAMLSSOProvider(tenantId);
return serviceProviderDAO.uploadServiceProvider(samlssoServiceProviderDO);
return serviceProviderDAO.uploadServiceProvider(samlssoServiceProviderDO, tenantId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
public class JDBCSAMLSSOServiceProviderDAOImpl implements SAMLSSOServiceProviderDAO {

private static final Log log = LogFactory.getLog(JDBCSAMLSSOServiceProviderDAOImpl.class);
private final int tenantId;
private int tenantId;
private static final String CERTIFICATE_PROPERTY_NAME = "CERTIFICATE";
private static final String QUERY_TO_GET_APPLICATION_CERTIFICATE_ID = "SELECT " +
"META.VALUE FROM SP_INBOUND_AUTH INBOUND, SP_APP SP, SP_METADATA META WHERE SP.ID = INBOUND.APP_ID AND " +
Expand All @@ -89,14 +89,13 @@ public class JDBCSAMLSSOServiceProviderDAOImpl implements SAMLSSOServiceProvider
"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 = ?";

public JDBCSAMLSSOServiceProviderDAOImpl(int tenantId) {
public JDBCSAMLSSOServiceProviderDAOImpl() {

this.tenantId = tenantId;
}

@Override
public boolean addServiceProvider(SAMLSSOServiceProviderDO serviceProviderDO) throws IdentityException {

public boolean addServiceProvider(SAMLSSOServiceProviderDO serviceProviderDO, int tenantId) throws IdentityException {
this.tenantId = tenantId;
validateServiceProvider(serviceProviderDO);
try {
if (processIsServiceProviderExists(serviceProviderDO.getIssuer())) {
Expand All @@ -119,9 +118,10 @@ public boolean addServiceProvider(SAMLSSOServiceProviderDO serviceProviderDO) th
}

@Override
public boolean updateServiceProvider(SAMLSSOServiceProviderDO serviceProviderDO, String currentIssuer)
public boolean updateServiceProvider(SAMLSSOServiceProviderDO serviceProviderDO, String currentIssuer, int tenantId)
throws IdentityException {

this.tenantId = tenantId;
validateServiceProvider(serviceProviderDO);
String newIssuer = serviceProviderDO.getIssuer();
boolean isIssuerUpdated = !StringUtils.equals(currentIssuer, newIssuer);
Expand All @@ -148,8 +148,9 @@ public boolean updateServiceProvider(SAMLSSOServiceProviderDO serviceProviderDO,
}

@Override
public SAMLSSOServiceProviderDO[] getServiceProviders() throws IdentityException {
public SAMLSSOServiceProviderDO[] getServiceProviders(int tenantId) throws IdentityException {

this.tenantId = tenantId;
List<SAMLSSOServiceProviderDO> serviceProvidersList;
try {
serviceProvidersList = processGetServiceProviders();
Expand All @@ -161,8 +162,9 @@ public SAMLSSOServiceProviderDO[] getServiceProviders() throws IdentityException
}

@Override
public boolean removeServiceProvider(String issuer) throws IdentityException {
public boolean removeServiceProvider(String issuer, int tenantId) throws IdentityException {

this.tenantId = tenantId;
if (issuer == null || StringUtils.isEmpty(issuer.trim())) {
throw new IllegalArgumentException("Trying to delete issuer \'" + issuer + "\'");
}
Expand All @@ -181,12 +183,13 @@ public boolean removeServiceProvider(String issuer) throws IdentityException {
}

@Override
public SAMLSSOServiceProviderDO getServiceProvider(String issuer) throws IdentityException {
public SAMLSSOServiceProviderDO getServiceProvider(String issuer, int tenantId) throws IdentityException {

this.tenantId = tenantId;
SAMLSSOServiceProviderDO serviceProviderDO = null;

try {
if (isServiceProviderExists(issuer)) {
if (isServiceProviderExists(issuer, tenantId)) {
serviceProviderDO = processGetServiceProvider(issuer);
}
} catch (DataAccessException e) {
Expand Down Expand Up @@ -216,8 +219,9 @@ public SAMLSSOServiceProviderDO getServiceProvider(String issuer) throws Identit
}

@Override
public boolean isServiceProviderExists(String issuer) throws IdentityException {
public boolean isServiceProviderExists(String issuer, int tenantId) throws IdentityException {

this.tenantId = tenantId;
try {
return processIsServiceProviderExists(issuer);
} catch (DataAccessException e) {
Expand All @@ -228,9 +232,10 @@ public boolean isServiceProviderExists(String issuer) throws IdentityException {
}

@Override
public SAMLSSOServiceProviderDO uploadServiceProvider(SAMLSSOServiceProviderDO serviceProviderDO)
public SAMLSSOServiceProviderDO uploadServiceProvider(SAMLSSOServiceProviderDO serviceProviderDO, int tenantId)
throws IdentityException {

this.tenantId = tenantId;
validateServiceProvider(serviceProviderDO);
if (serviceProviderDO.getDefaultAssertionConsumerUrl() == null) {
throw new IdentityException("No default assertion consumer URL provided for service provider :" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ public class RegistrySAMLSSOServiceProviderDAOImpl extends AbstractDAO<SAMLSSOSe

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

public RegistrySAMLSSOServiceProviderDAOImpl(Registry registry) {
this.registry = registry;
public RegistrySAMLSSOServiceProviderDAOImpl() {

}

protected SAMLSSOServiceProviderDO resourceToObject(Resource resource) {
Expand Down Expand Up @@ -255,8 +255,8 @@ protected SAMLSSOServiceProviderDO resourceToObject(Resource resource) {
* @return True if addition successful.
* @throws IdentityException Error while persisting to the registry.
*/
public boolean addServiceProvider(SAMLSSOServiceProviderDO serviceProviderDO) throws IdentityException {

public boolean addServiceProvider(SAMLSSOServiceProviderDO serviceProviderDO, int tenantId) throws IdentityException {
registry = getRegistry(tenantId);
if (serviceProviderDO == null || serviceProviderDO.getIssuer() == null ||
StringUtils.isBlank(serviceProviderDO.getIssuer())) {
throw new IdentityException("Issuer cannot be found in the provided arguments.");
Expand Down Expand Up @@ -287,7 +287,7 @@ public boolean addServiceProvider(SAMLSSOServiceProviderDO serviceProviderDO) th
return false;
}

Resource resource = createResource(serviceProviderDO);
Resource resource = createResource(serviceProviderDO, registry);
if (!isTransactionStarted) {
registry.beginTransaction();
}
Expand Down Expand Up @@ -319,7 +319,7 @@ public boolean addServiceProvider(SAMLSSOServiceProviderDO serviceProviderDO) th
}
}

private Resource createResource(SAMLSSOServiceProviderDO serviceProviderDO) throws RegistryException {
private Resource createResource(SAMLSSOServiceProviderDO serviceProviderDO, Registry registry) throws RegistryException {
Resource resource;
resource = registry.newResource();
resource.addProperty(IdentityRegistryResources.PROP_SAML_SSO_ISSUER,
Expand Down Expand Up @@ -484,9 +484,10 @@ private String getIssuerWithQualifier(String issuer, String qualifier) {
* @return True if the update is successful.
* @throws IdentityException If an error occurs while updating the service provider.
*/
public boolean updateServiceProvider(SAMLSSOServiceProviderDO serviceProviderDO, String currentIssuer)
public boolean updateServiceProvider(SAMLSSOServiceProviderDO serviceProviderDO, String currentIssuer, int tenantId)
throws IdentityException {

registry = getRegistry(tenantId);
if (serviceProviderDO == null || serviceProviderDO.getIssuer() == null ||
StringUtils.isBlank(serviceProviderDO.getIssuer())) {
throw new IdentityException("Issuer cannot be found in the provided arguments.");
Expand Down Expand Up @@ -520,7 +521,7 @@ public boolean updateServiceProvider(SAMLSSOServiceProviderDO serviceProviderDO,
return false;
}

Resource resource = createResource(serviceProviderDO);
Resource resource = createResource(serviceProviderDO, registry);
if (!isTransactionStarted) {
registry.beginTransaction();
}
Expand Down Expand Up @@ -559,7 +560,8 @@ public boolean updateServiceProvider(SAMLSSOServiceProviderDO serviceProviderDO,
}
}

public SAMLSSOServiceProviderDO[] getServiceProviders() throws IdentityException {
public SAMLSSOServiceProviderDO[] getServiceProviders(int tenantId) throws IdentityException {
registry = getRegistry(tenantId);
List<SAMLSSOServiceProviderDO> serviceProvidersList = new ArrayList<>();
try {
if (registry.resourceExists(IdentityRegistryResources.SAML_SSO_SERVICE_PROVIDERS)) {
Expand All @@ -586,8 +588,8 @@ public SAMLSSOServiceProviderDO[] getServiceProviders() throws IdentityException
* @param issuer Name of the SAML issuer.
* @throws IdentityException Error occurred while removing the SAML service provider from registry.
*/
public boolean removeServiceProvider(String issuer) throws IdentityException {

public boolean removeServiceProvider(String issuer, int tenantId) throws IdentityException {
registry = getRegistry(tenantId);
if (issuer == null || StringUtils.isEmpty(issuer.trim())) {
throw new IllegalArgumentException("Trying to delete issuer \'" + issuer + "\'");
}
Expand Down Expand Up @@ -627,8 +629,8 @@ public boolean removeServiceProvider(String issuer) throws IdentityException {
* @return
* @throws IdentityException
*/
public SAMLSSOServiceProviderDO getServiceProvider(String issuer) throws IdentityException {

public SAMLSSOServiceProviderDO getServiceProvider(String issuer, int tenantId) throws IdentityException {
registry = getRegistry(tenantId);
String path = IdentityRegistryResources.SAML_SSO_SERVICE_PROVIDERS + encodePath(issuer);
SAMLSSOServiceProviderDO serviceProviderDO = null;

Expand Down Expand Up @@ -731,7 +733,8 @@ private int getApplicationCertificateId(String issuer, int tenantId) throws SQLE
}
}

public boolean isServiceProviderExists(String issuer) throws IdentityException {
public boolean isServiceProviderExists(String issuer, int tenantId) throws IdentityException {
registry = getRegistry(tenantId);
String path = IdentityRegistryResources.SAML_SSO_SERVICE_PROVIDERS + encodePath(issuer);
try {
return registry.resourceExists(path);
Expand All @@ -752,9 +755,9 @@ private String encodePath(String path) {
* @return True if upload success.
* @throws IdentityException Error occurred while adding the information to registry.
*/
public SAMLSSOServiceProviderDO uploadServiceProvider(SAMLSSOServiceProviderDO serviceProviderDO) throws
public SAMLSSOServiceProviderDO uploadServiceProvider(SAMLSSOServiceProviderDO serviceProviderDO, int tenantId) throws
IdentityException {

registry = getRegistry(tenantId);
if (serviceProviderDO == null || serviceProviderDO.getIssuer() == null) {
throw new IdentityException("Issuer cannot be found in the provided arguments.");
}
Expand Down Expand Up @@ -792,7 +795,7 @@ public SAMLSSOServiceProviderDO uploadServiceProvider(SAMLSSOServiceProviderDO s
registry.beginTransaction();
}

Resource resource = createResource(serviceProviderDO);
Resource resource = createResource(serviceProviderDO, registry);
registry.put(path, resource);
if (log.isDebugEnabled()) {
if (StringUtils.isNotBlank(serviceProviderDO.getIssuerQualifier())) {
Expand Down Expand Up @@ -854,4 +857,14 @@ private void getChildResources(String parentResource, List<SAMLSSOServiceProvide
}
}
}

private Registry getRegistry(int tenantId) throws IdentityException {

try {
Registry registry = IdentityTenantUtil.getRegistryService().getConfigSystemRegistry(tenantId);
return registry;
} catch (RegistryException e) {
throw new IdentityException("Error while retrieving registry", e);
}
}
}
Loading

0 comments on commit 95929a8

Please sign in to comment.