Skip to content

Commit

Permalink
Improve SAML persistence layer
Browse files Browse the repository at this point in the history
  • Loading branch information
darshanasbg committed Dec 19, 2024
1 parent 17f132e commit 7bcb134
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 183 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,18 @@

package org.wso2.carbon.identity.core;

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.SAMLSSOServiceProviderDAO;
import org.wso2.carbon.identity.core.dao.SAMLSSOServiceProviderRegistryDAOImpl;
import org.wso2.carbon.identity.core.model.SAMLSSOServiceProviderDO;
import org.wso2.carbon.identity.core.util.IdentityTenantUtil;
import org.wso2.carbon.registry.api.RegistryException;
import org.wso2.carbon.registry.core.Registry;

/**
* 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);

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

Registry registry = IdentityTenantUtil.getRegistryService().getConfigSystemRegistry(tenantId);
return new SAMLSSOServiceProviderRegistryDAOImpl(registry);
}

private static SAMLSSOServiceProviderDAO serviceProviderDAO = new SAMLSSOServiceProviderRegistryDAOImpl();

/**
* Add a saml service provider.
Expand All @@ -60,13 +42,7 @@ private SAMLSSOServiceProviderDAO buildSAMLSSOProvider(int tenantId) throws Regi
public boolean addServiceProvider(SAMLSSOServiceProviderDO serviceProviderDO, int tenantId)
throws IdentityException {

try {
SAMLSSOServiceProviderDAO serviceProviderDAO = buildSAMLSSOProvider(tenantId);
return serviceProviderDAO.addServiceProvider(serviceProviderDO);
} catch (RegistryException e) {
LOG.error("Error while adding service provider", e);
throw new IdentityException("Error while retrieving registry", e);
}
return serviceProviderDAO.addServiceProvider(serviceProviderDO, tenantId);
}

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

try {
SAMLSSOServiceProviderDAO serviceProviderDAO = buildSAMLSSOProvider(tenantId);
return serviceProviderDAO.updateServiceProvider(serviceProviderDO, currentIssuer);
} catch (RegistryException e) {
LOG.error("Error while updating service provider", e);
throw new IdentityException("Error while retrieving registry", e);
}
return serviceProviderDAO.updateServiceProvider(serviceProviderDO, currentIssuer, tenantId);
}

/**
Expand All @@ -97,16 +67,9 @@ public boolean updateServiceProvider(SAMLSSOServiceProviderDO serviceProviderDO,
* @return Array of SAMLSSOServiceProviderDO.
* @throws IdentityException Error when getting the SAML service providers.
*/
public SAMLSSOServiceProviderDO[] getServiceProviders(int tenantId)
throws IdentityException {
public SAMLSSOServiceProviderDO[] getServiceProviders(int tenantId) throws IdentityException {

try {
SAMLSSOServiceProviderDAO serviceProviderDOA = buildSAMLSSOProvider(tenantId);
return serviceProviderDOA.getServiceProviders();
} catch (RegistryException e) {
LOG.error("Error while getting service providers", e);
throw new IdentityException("Error while retrieving registry", e);
}
return serviceProviderDAO.getServiceProviders(tenantId);
}

/**
Expand All @@ -117,17 +80,9 @@ public SAMLSSOServiceProviderDO[] getServiceProviders(int tenantId)
* @return SAMLSSOServiceProviderDO
* @throws IdentityException Error when getting the SAML service provider.
*/
public SAMLSSOServiceProviderDO getServiceProvider(String issuer, int tenantId)
throws IdentityException {

try {
SAMLSSOServiceProviderDAO serviceProviderDAO = buildSAMLSSOProvider(tenantId);
return serviceProviderDAO.getServiceProvider(issuer);
} catch (RegistryException e) {
LOG.error("Error while getting service provider", e);
throw new IdentityException("Error while retrieving SAML issuer " + e.getMessage());
}
public SAMLSSOServiceProviderDO getServiceProvider(String issuer, int tenantId) throws IdentityException {

return serviceProviderDAO.getServiceProvider(issuer, tenantId);
}

/**
Expand All @@ -141,13 +96,7 @@ public SAMLSSOServiceProviderDO getServiceProvider(String issuer, int tenantId)
public boolean isServiceProviderExists(String issuer, int tenantId)
throws IdentityException {

try {
SAMLSSOServiceProviderDAO serviceProviderDAO = buildSAMLSSOProvider(tenantId);
return serviceProviderDAO.isServiceProviderExists(issuer);
} catch (RegistryException e) {
LOG.error("Error while getting service provider", e);
throw new IdentityException("Error while retrieving SAML issuer " + e.getMessage());
}
return serviceProviderDAO.isServiceProviderExists(issuer, tenantId);
}

/**
Expand All @@ -157,16 +106,9 @@ public boolean isServiceProviderExists(String issuer, int tenantId)
* @param tenantId Tenant ID.
* @throws IdentityException Error when removing the SAML configuration.
*/
public boolean removeServiceProvider(String issuer, int tenantId)
throws IdentityException {
public boolean removeServiceProvider(String issuer, int tenantId) throws IdentityException {

try {
SAMLSSOServiceProviderDAO serviceProviderDAO = buildSAMLSSOProvider(tenantId);
return serviceProviderDAO.removeServiceProvider(issuer);
} catch (RegistryException e) {
LOG.error("Error while removing service provider", e);
throw new IdentityException("Error while deleting SAML issuer " + e.getMessage());
}
return serviceProviderDAO.removeServiceProvider(issuer, tenantId);
}

/**
Expand All @@ -177,15 +119,9 @@ public boolean removeServiceProvider(String issuer, int tenantId)
* @return SAML service provider information object.
* @throws IdentityException Error when uploading the SAML configuration.
*/
public SAMLSSOServiceProviderDO uploadServiceProvider(SAMLSSOServiceProviderDO samlssoServiceProviderDO, int tenantId)
throws IdentityException {
public SAMLSSOServiceProviderDO uploadServiceProvider(SAMLSSOServiceProviderDO samlssoServiceProviderDO,
int tenantId) throws IdentityException {

try {
SAMLSSOServiceProviderDAO serviceProviderDAO = buildSAMLSSOProvider(tenantId);
return serviceProviderDAO.uploadServiceProvider(samlssoServiceProviderDO);
} catch (RegistryException e) {
LOG.error("Error while uploading service provider", e);
throw new IdentityException("Error while uploading SAML issuer " + e.getMessage());
}
return serviceProviderDAO.uploadServiceProvider(samlssoServiceProviderDO, tenantId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,70 +21,80 @@
import org.wso2.carbon.identity.base.IdentityException;
import org.wso2.carbon.identity.core.model.SAMLSSOServiceProviderDO;

/**
* This interface is used to manage the SAML SSO service providers.
*/
public interface SAMLSSOServiceProviderDAO {

/**
* Add the service provider information to the registry.
*
* @param serviceProviderDO Service provider information object.
* @param tenantId Tenant Id.
* @return True if addition successful.
* @throws IdentityException Error while persisting to the registry.
*/
boolean addServiceProvider(SAMLSSOServiceProviderDO serviceProviderDO) throws IdentityException;
boolean addServiceProvider(SAMLSSOServiceProviderDO serviceProviderDO, int tenantId) throws IdentityException;

/**
* Update the service provider if it exists.
*
* @param serviceProviderDO Service provider to be updated.
* @param currentIssuer Issuer of the service provider before the update.
* @param serviceProviderDO Service provider to be updated.
* @param currentIssuer Issuer of the service provider before the update.
* @param tenantId Tenant Id.
* @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)
boolean updateServiceProvider(SAMLSSOServiceProviderDO serviceProviderDO, String currentIssuer, int tenantId)
throws IdentityException;

/**
* Get all the service providers.
*
* @param tenantId Tenant Id.
* @return Array of service providers.
* @throws IdentityException Error occurred while retrieving the service providers from registry.
*/
SAMLSSOServiceProviderDO[] getServiceProviders() throws IdentityException;
SAMLSSOServiceProviderDO[] getServiceProviders(int tenantId) throws IdentityException;

/**
* Remove the service provider with the given name.
*
* @param issuer Name of the SAML issuer.
* @param tenantId Tenant Id.
* @return True if deletion success.
* @param issuer Name of the SAML issuer.
* @throws IdentityException Error occurred while removing the SAML service provider from registry.
*/
boolean removeServiceProvider(String issuer) throws IdentityException;
boolean removeServiceProvider(String issuer, int tenantId) throws IdentityException;

/**
* Get the service provider.
*
* @param issuer Name of the SAML issuer.
* @param issuer Name of the SAML issuer.
* @param tenantId tenant Id.
* @return Service provider information object.
* @throws IdentityException Error occurred while retrieving the SAML service provider from registry.
*/
SAMLSSOServiceProviderDO getServiceProvider(String issuer) throws IdentityException;
SAMLSSOServiceProviderDO getServiceProvider(String issuer, int tenantId) throws IdentityException;

/**
* Check whether the service provider exists.
*
* @param issuer Name of the SAML issuer.
* @param issuer Name of the SAML issuer.
* @param tenantId Tenant Id.
* @return True if service provider exists.
* @throws IdentityException Error occurred while checking the existence of the SAML service provider.
*/
boolean isServiceProviderExists(String issuer) throws IdentityException;
boolean isServiceProviderExists(String issuer, int tenantId) throws IdentityException;

/**
* Upload service Provider using metadata file.
*
* @param serviceProviderDO Service provider information object.
* @param tenantId Tenant Id.
* @return True if upload success.
* @throws IdentityException Error occurred while adding the information to registry.
*/
SAMLSSOServiceProviderDO uploadServiceProvider(SAMLSSOServiceProviderDO serviceProviderDO) throws
IdentityException;
SAMLSSOServiceProviderDO uploadServiceProvider(SAMLSSOServiceProviderDO serviceProviderDO, int tenantId)
throws IdentityException;
}
Loading

0 comments on commit 7bcb134

Please sign in to comment.