Skip to content

Commit

Permalink
Merge pull request #436 from SujanSanjula96/saml-name-format
Browse files Browse the repository at this point in the history
Fix SAML name ID format inconsistency
  • Loading branch information
SujanSanjula96 authored Jan 6, 2025
2 parents 8ab8362 + 4c5d03c commit cbbe1b1
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public class SAMLSSOConstants {
public static final String SAML_IDP_INIT_LOGOUT_RESPONSE_SIGNING_ENABLED = "SSOService.SAMLIdpInitLogoutResponseSigningEnabled";
public static final String SAML_ASSERTION_ENCRYPT_WITH_APP_CERT = "SSOService.SAMLAssertionEncyptWithAppCert";
public static final String SEPARATE_MULTI_ATTRS_FROM_IDPS_USING_ATTRIBUTE_SEPARATOR = "SSOService.SeparateMultiAttributesFromIdP";
public static final String SAML_RETURN_VALID_NAME_ID_FORMAT = "SSOService.ReturnValidNameIDFormat";
public static final String START_SOAP_BINDING = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
"<SOAP-ENV:Body>";
public static final String END_SOAP_BINDING = "</SOAP-ENV:Body>" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.wso2.carbon.identity.sp.metadata.saml2.exception.InvalidMetadataException;
import org.wso2.carbon.identity.sp.metadata.saml2.util.Parser;
import org.wso2.carbon.identity.sso.saml.Error;
import org.wso2.carbon.identity.sso.saml.SAMLSSOConstants;
import org.wso2.carbon.identity.sso.saml.SSOServiceProviderConfigManager;
import org.wso2.carbon.identity.sso.saml.dto.SAMLSSOServiceProviderDTO;
import org.wso2.carbon.identity.sso.saml.dto.SAMLSSOServiceProviderInfoDTO;
Expand Down Expand Up @@ -695,7 +696,11 @@ public SAMLSSOServiceProviderInfoDTO getServiceProviders() throws IdentityExcept
if (providerDTO.getNameIDFormat() == null) {
providerDTO.setNameIDFormat(NameIdentifier.UNSPECIFIED);
}
providerDTO.setNameIDFormat(providerDTO.getNameIDFormat().replace(":", "/"));
boolean returnValidNameIDFormat = Boolean.parseBoolean(
IdentityUtil.getProperty(SAMLSSOConstants.SAML_RETURN_VALID_NAME_ID_FORMAT));
if (!returnValidNameIDFormat) {
providerDTO.setNameIDFormat(providerDTO.getNameIDFormat().replace(":", "/"));
}

providerDTO.setIdPInitSSOEnabled(providerDO.isIdPInitSSOEnabled());
providerDTO.setIdPInitSLOEnabled(providerDO.isIdPInitSLOEnabled());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
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.util.IdentityUtil;
import org.wso2.carbon.identity.sp.metadata.saml2.util.Parser;
import org.wso2.carbon.identity.sso.saml.SAMLSSOConstants;
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;
Expand All @@ -48,7 +50,8 @@
import static org.powermock.api.mockito.PowerMockito.*;

@PrepareForTest({IdentitySAMLSSOServiceComponentHolder.class, SSOServiceProviderConfigManager.class,
SAMLSSOServiceProviderDO.class, Parser.class, UserRegistry.class, SAMLSSOConfigAdmin.class, SAMLSSOUtil.class})
SAMLSSOServiceProviderDO.class, Parser.class, UserRegistry.class, SAMLSSOConfigAdmin.class, SAMLSSOUtil.class,
IdentityUtil.class})
@PowerMockIgnore({"javax.xml.*", "org.xml.*", "org.apache.xerces.*", "org.w3c.dom.*"})
public class SAMLSSOConfigAdminTest extends PowerMockTestCase {

Expand All @@ -57,6 +60,9 @@ public class SAMLSSOConfigAdminTest extends PowerMockTestCase {
@Mock
UserRegistry userRegistry;

@Mock
IdentityUtil identityUtil;

@Mock
private SAMLSSOServiceProviderManager samlSSOServiceProviderManager;

Expand Down Expand Up @@ -261,4 +267,61 @@ public void testGetServiceProviders() throws Exception {
Assert.assertEquals(samlssoConfigAdmin.getServiceProviders().getServiceProviders().length, 3);
}

@Test
public void testGetServiceProvidersForValidNameIDFormat() throws Exception {

mockStatic(UserRegistry.class);
mockStatic(IdentityUtil.class);
SAMLSSOServiceProviderDO[] serviceProvidersList = new SAMLSSOServiceProviderDO[2];
when(userRegistry.getTenantId()).thenReturn(0);
when(samlSSOServiceProviderManager.getServiceProviders(anyInt())).thenReturn(serviceProvidersList);

SAMLSSOServiceProviderDO samlssoServiceProviderDO = new SAMLSSOServiceProviderDO();
samlssoServiceProviderDO.setIssuer("issuer");
samlssoServiceProviderDO.setNameIDFormat(null);
SAMLSSOServiceProviderDO samlssoServiceProviderDO1 = new SAMLSSOServiceProviderDO();
samlssoServiceProviderDO1.setIssuer("issuer1");
samlssoServiceProviderDO1.setNameIDFormat("urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress");
serviceProvidersList[0] = samlssoServiceProviderDO;
serviceProvidersList[1] = samlssoServiceProviderDO1;

when(IdentityUtil.getProperty(SAMLSSOConstants.SAML_RETURN_VALID_NAME_ID_FORMAT)).thenReturn("true");

when(userRegistry.getTenantId()).thenReturn(0);
SAMLSSOServiceProviderDTO[] serviceProviders = samlssoConfigAdmin.getServiceProviders().getServiceProviders();
Assert.assertEquals(serviceProviders.length, 2);
Assert.assertEquals(serviceProviders[0].getNameIDFormat(),
"urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified");
Assert.assertEquals(serviceProviders[1].getNameIDFormat(),
"urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress");
}

@Test
public void testGetServiceProvidersForLegacyNameIDFormat() throws Exception {

mockStatic(UserRegistry.class);
mockStatic(IdentityUtil.class);
SAMLSSOServiceProviderDO[] serviceProvidersList = new SAMLSSOServiceProviderDO[2];
when(userRegistry.getTenantId()).thenReturn(0);
when(samlSSOServiceProviderManager.getServiceProviders(anyInt())).thenReturn(serviceProvidersList);

SAMLSSOServiceProviderDO samlssoServiceProviderDO = new SAMLSSOServiceProviderDO();
samlssoServiceProviderDO.setIssuer("issuer");
samlssoServiceProviderDO.setNameIDFormat(null);
SAMLSSOServiceProviderDO samlssoServiceProviderDO1 = new SAMLSSOServiceProviderDO();
samlssoServiceProviderDO1.setIssuer("issuer1");
samlssoServiceProviderDO1.setNameIDFormat("urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress");
serviceProvidersList[0] = samlssoServiceProviderDO;
serviceProvidersList[1] = samlssoServiceProviderDO1;

when(IdentityUtil.getProperty(SAMLSSOConstants.SAML_RETURN_VALID_NAME_ID_FORMAT)).thenReturn("false");

when(userRegistry.getTenantId()).thenReturn(0);
SAMLSSOServiceProviderDTO[] serviceProviders = samlssoConfigAdmin.getServiceProviders().getServiceProviders();
Assert.assertEquals(serviceProviders.length, 2);
Assert.assertEquals(serviceProviders[0].getNameIDFormat(),
"urn/oasis/names/tc/SAML/1.1/nameid-format/unspecified");
Assert.assertEquals(serviceProviders[1].getNameIDFormat(),
"urn/oasis/names/tc/SAML/1.1/nameid-format/emailAddress");
}
}

0 comments on commit cbbe1b1

Please sign in to comment.