-
Notifications
You must be signed in to change notification settings - Fork 549
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Move SAML SP validation to SAMLSSOServiceProviderManager layer #6263
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,9 @@ | |
|
||
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.SAMLServiceProviderPersistenceManagerFactory; | ||
import org.wso2.carbon.identity.core.dao.SAMLSSOServiceProviderDAO; | ||
|
@@ -33,6 +36,7 @@ public class SAMLSSOServiceProviderManager { | |
new SAMLServiceProviderPersistenceManagerFactory(); | ||
SAMLSSOServiceProviderDAO serviceProviderDAO = | ||
samlSSOPersistenceManagerFactory.getSAMLServiceProviderPersistenceManager(); | ||
private static Log LOG = LogFactory.getLog(SAMLSSOServiceProviderManager.class); | ||
|
||
/** | ||
* Add a saml service provider. | ||
|
@@ -45,6 +49,13 @@ public class SAMLSSOServiceProviderManager { | |
public boolean addServiceProvider(SAMLSSOServiceProviderDO serviceProviderDO, int tenantId) | ||
throws IdentityException { | ||
|
||
validateServiceProvider(serviceProviderDO); | ||
if (isServiceProviderExists(serviceProviderDO.getIssuer(), tenantId)) { | ||
if (LOG.isDebugEnabled()){ | ||
LOG.debug(serviceProviderInfo(serviceProviderDO) + " already exists."); | ||
} | ||
return false; | ||
} | ||
return serviceProviderDAO.addServiceProvider(serviceProviderDO, tenantId); | ||
} | ||
|
||
|
@@ -60,6 +71,15 @@ public boolean addServiceProvider(SAMLSSOServiceProviderDO serviceProviderDO, in | |
public boolean updateServiceProvider(SAMLSSOServiceProviderDO serviceProviderDO, String currentIssuer, int tenantId) | ||
throws IdentityException { | ||
|
||
validateServiceProvider(serviceProviderDO); | ||
String newIssuer = serviceProviderDO.getIssuer(); | ||
boolean isIssuerUpdated = !StringUtils.equals(currentIssuer, newIssuer); | ||
if (isIssuerUpdated && isServiceProviderExists(newIssuer, tenantId)) { | ||
if (LOG.isDebugEnabled()) { | ||
LOG.debug(serviceProviderInfo(serviceProviderDO) + " already exists."); | ||
} | ||
return false; | ||
} | ||
return serviceProviderDAO.updateServiceProvider(serviceProviderDO, currentIssuer, tenantId); | ||
} | ||
|
||
|
@@ -110,20 +130,89 @@ public boolean isServiceProviderExists(String issuer, int tenantId) throws Ident | |
*/ | ||
public boolean removeServiceProvider(String issuer, int tenantId) throws IdentityException { | ||
|
||
if (issuer == null || StringUtils.isEmpty(issuer.trim())) { | ||
throw new IllegalArgumentException("Trying to delete issuer \'" + issuer + "\'"); | ||
} | ||
if (!isServiceProviderExists(issuer, tenantId)) { | ||
if (LOG.isDebugEnabled()) { | ||
LOG.debug("Service Provider with issuer: " + issuer + " does not exist."); | ||
} | ||
return false; | ||
} | ||
return serviceProviderDAO.removeServiceProvider(issuer, tenantId); | ||
} | ||
|
||
/** | ||
* Upload the SAML configuration related to the application, using metadata. | ||
* | ||
* @param samlssoServiceProviderDO SAML service provider information object. | ||
* @param serviceProviderDO SAML service provider information object. | ||
* @param tenantId Tenant ID. | ||
* @return SAML service provider information object. | ||
* @throws IdentityException Error when uploading the SAML configuration. | ||
*/ | ||
public SAMLSSOServiceProviderDO uploadServiceProvider(SAMLSSOServiceProviderDO samlssoServiceProviderDO, | ||
public SAMLSSOServiceProviderDO uploadServiceProvider(SAMLSSOServiceProviderDO serviceProviderDO, | ||
int tenantId) throws IdentityException { | ||
|
||
return serviceProviderDAO.uploadServiceProvider(samlssoServiceProviderDO, tenantId); | ||
validateServiceProvider(serviceProviderDO); | ||
if (serviceProviderDO.getDefaultAssertionConsumerUrl() == null) { | ||
throw new IdentityException("No default assertion consumer URL provided for service provider :" + | ||
serviceProviderDO.getIssuer()); | ||
} | ||
if (isServiceProviderExists(serviceProviderDO.getIssuer(), tenantId)) { | ||
if (LOG.isDebugEnabled()){ | ||
LOG.debug(serviceProviderInfo(serviceProviderDO) + " already exists."); | ||
} | ||
throw new IdentityException("A Service Provider already exists."); | ||
} | ||
|
||
return serviceProviderDAO.uploadServiceProvider(serviceProviderDO, tenantId); | ||
} | ||
|
||
private void validateServiceProvider(SAMLSSOServiceProviderDO serviceProviderDO) throws IdentityException { | ||
|
||
if (serviceProviderDO == null || serviceProviderDO.getIssuer() == null || | ||
StringUtils.isBlank(serviceProviderDO.getIssuer())) { | ||
throw new IdentityException("Issuer cannot be found in the provided arguments."); | ||
} | ||
|
||
if (StringUtils.isNotBlank(serviceProviderDO.getIssuerQualifier()) && | ||
!serviceProviderDO.getIssuer().contains(IdentityRegistryResources.QUALIFIER_ID)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same concern is there for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We added the check to ensure that the qualifier wont be appended again to the issuer value and set in the DO |
||
serviceProviderDO.setIssuer( | ||
getIssuerWithQualifier(serviceProviderDO.getIssuer(), serviceProviderDO.getIssuerQualifier())); | ||
} | ||
} | ||
|
||
private String serviceProviderInfo(SAMLSSOServiceProviderDO serviceProviderDO) { | ||
|
||
if (StringUtils.isNotBlank(serviceProviderDO.getIssuerQualifier())) { | ||
return "SAML2 Service Provider with issuer: " + getIssuerWithoutQualifier(serviceProviderDO.getIssuer()) + | ||
" and qualifier name " + serviceProviderDO.getIssuerQualifier(); | ||
} else { | ||
return "SAML2 Service Provider with issuer: " + serviceProviderDO.getIssuer(); | ||
} | ||
} | ||
|
||
/** | ||
* Get the issuer value to be added to registry by appending the qualifier. | ||
* | ||
* @param issuer value given as 'issuer' when configuring SAML SP. | ||
* @return issuer value with qualifier appended. | ||
*/ | ||
private String getIssuerWithQualifier(String issuer, String qualifier) { | ||
|
||
return issuer + IdentityRegistryResources.QUALIFIER_ID + qualifier; | ||
} | ||
|
||
/** | ||
* Get the issuer value by removing the qualifier. | ||
* | ||
* @param issuerWithQualifier issuer value saved in the registry. | ||
* @return issuer value given as 'issuer' when configuring SAML SP. | ||
*/ | ||
private String getIssuerWithoutQualifier(String issuerWithQualifier) { | ||
|
||
String issuerWithoutQualifier = StringUtils.substringBeforeLast(issuerWithQualifier, | ||
IdentityRegistryResources.QUALIFIER_ID); | ||
return issuerWithoutQualifier; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
StringUtils.isBlank(serviceProviderDO.getIssuer()
part is not available in theuploadServiceProvider()
validation in the RegistrySAMLSSOServiceProviderDAOImpl.Is that expected?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added the check to ensure the issuer isnt white space