Skip to content
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

Fix SAML name ID format inconsistency #436

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
}
}
Loading