Polish Testing for Custom Attributes Values

- Moved construction and management of custom objects
into TestCustomOpenSamlObjects

Issue gh-9696
This commit is contained in:
Josh Cummings 2022-02-04 13:40:25 -07:00
parent 3cc7f384e6
commit 70bb588a25
3 changed files with 62 additions and 51 deletions

View File

@ -71,6 +71,7 @@ import org.springframework.security.saml2.core.Saml2ErrorCodes;
import org.springframework.security.saml2.core.Saml2ResponseValidatorResult; import org.springframework.security.saml2.core.Saml2ResponseValidatorResult;
import org.springframework.security.saml2.core.TestSaml2X509Credentials; import org.springframework.security.saml2.core.TestSaml2X509Credentials;
import org.springframework.security.saml2.provider.service.authentication.OpenSaml4AuthenticationProvider.ResponseToken; import org.springframework.security.saml2.provider.service.authentication.OpenSaml4AuthenticationProvider.ResponseToken;
import org.springframework.security.saml2.provider.service.authentication.TestCustomOpenSamlObjects.CustomOpenSamlObject;
import org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistration; import org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistration;
import org.springframework.security.saml2.provider.service.registration.TestRelyingPartyRegistrations; import org.springframework.security.saml2.provider.service.registration.TestRelyingPartyRegistrations;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
@ -252,28 +253,22 @@ public class OpenSaml4AuthenticationProviderTests {
@Test @Test
public void authenticateWhenAssertionContainsCustomAttributesThenItSucceeds() { public void authenticateWhenAssertionContainsCustomAttributesThenItSucceeds() {
XMLObjectProviderRegistrySupport.getMarshallerFactory().registerMarshaller(
TestCustomOpenSamlObject.CustomSamlObject.TYPE_NAME,
new TestCustomOpenSamlObject.CustomSamlObjectMarshaller());
XMLObjectProviderRegistrySupport.getUnmarshallerFactory().registerUnmarshaller(
TestCustomOpenSamlObject.CustomSamlObject.TYPE_NAME,
new TestCustomOpenSamlObject.CustomSamlObjectUnmarshaller());
Response response = response(); Response response = response();
Assertion assertion = assertion(); Assertion assertion = assertion();
List<AttributeStatement> attributes = TestOpenSamlObjects.customAttributeStatements(); AttributeStatement attribute = TestOpenSamlObjects.customAttributeStatement("Address",
assertion.getAttributeStatements().addAll(attributes); TestCustomOpenSamlObjects.instance());
assertion.getAttributeStatements().add(attribute);
TestOpenSamlObjects.signed(assertion, TestSaml2X509Credentials.assertingPartySigningCredential(), TestOpenSamlObjects.signed(assertion, TestSaml2X509Credentials.assertingPartySigningCredential(),
RELYING_PARTY_ENTITY_ID); RELYING_PARTY_ENTITY_ID);
response.getAssertions().add(assertion); response.getAssertions().add(assertion);
Saml2AuthenticationToken token = token(response, verifying(registration())); Saml2AuthenticationToken token = token(response, verifying(registration()));
Authentication authentication = this.provider.authenticate(token); Authentication authentication = this.provider.authenticate(token);
Saml2AuthenticatedPrincipal principal = (Saml2AuthenticatedPrincipal) authentication.getPrincipal(); Saml2AuthenticatedPrincipal principal = (Saml2AuthenticatedPrincipal) authentication.getPrincipal();
TestCustomOpenSamlObject.CustomSamlObject customSamlObject; CustomOpenSamlObject address = (CustomOpenSamlObject) principal.getAttribute("Address").get(0);
customSamlObject = (TestCustomOpenSamlObject.CustomSamlObject) principal.getAttribute("Address").get(0); assertThat(address.getStreet()).isEqualTo("Test Street");
assertThat(customSamlObject.getStreet()).isEqualTo("Test Street"); assertThat(address.getStreetNumber()).isEqualTo("1");
assertThat(customSamlObject.getStreetNumber()).isEqualTo("1"); assertThat(address.getZIP()).isEqualTo("11111");
assertThat(customSamlObject.getZIP()).isEqualTo("11111"); assertThat(address.getCity()).isEqualTo("Test City");
assertThat(customSamlObject.getCity()).isEqualTo("Test City");
} }
@Test @Test

View File

@ -29,18 +29,54 @@ import org.opensaml.core.xml.AbstractXMLObjectBuilder;
import org.opensaml.core.xml.ElementExtensibleXMLObject; import org.opensaml.core.xml.ElementExtensibleXMLObject;
import org.opensaml.core.xml.Namespace; import org.opensaml.core.xml.Namespace;
import org.opensaml.core.xml.XMLObject; import org.opensaml.core.xml.XMLObject;
import org.opensaml.core.xml.config.XMLObjectProviderRegistrySupport;
import org.opensaml.core.xml.io.AbstractXMLObjectMarshaller; import org.opensaml.core.xml.io.AbstractXMLObjectMarshaller;
import org.opensaml.core.xml.io.AbstractXMLObjectUnmarshaller; import org.opensaml.core.xml.io.AbstractXMLObjectUnmarshaller;
import org.opensaml.core.xml.io.UnmarshallingException; import org.opensaml.core.xml.io.UnmarshallingException;
import org.opensaml.core.xml.schema.XSAny; import org.opensaml.core.xml.schema.XSAny;
import org.opensaml.core.xml.schema.impl.XSAnyBuilder;
import org.opensaml.core.xml.util.IndexedXMLObjectChildrenList; import org.opensaml.core.xml.util.IndexedXMLObjectChildrenList;
import org.opensaml.saml.common.xml.SAMLConstants; import org.opensaml.saml.common.xml.SAMLConstants;
import org.opensaml.saml.saml2.core.AttributeValue; import org.opensaml.saml.saml2.core.AttributeValue;
import org.w3c.dom.Element; import org.w3c.dom.Element;
public class TestCustomOpenSamlObject { import org.springframework.security.saml2.core.OpenSamlInitializationService;
public interface CustomSamlObject extends ElementExtensibleXMLObject { public class TestCustomOpenSamlObjects {
static {
OpenSamlInitializationService.initialize();
XMLObjectProviderRegistrySupport.getMarshallerFactory().registerMarshaller(
CustomOpenSamlObject.TYPE_NAME,
new TestCustomOpenSamlObjects.CustomSamlObjectMarshaller());
XMLObjectProviderRegistrySupport.getUnmarshallerFactory().registerUnmarshaller(
CustomOpenSamlObject.TYPE_NAME,
new TestCustomOpenSamlObjects.CustomSamlObjectUnmarshaller());
}
public static CustomOpenSamlObject instance() {
CustomOpenSamlObject samlObject = new TestCustomOpenSamlObjects.CustomSamlObjectBuilder()
.buildObject(AttributeValue.DEFAULT_ELEMENT_NAME, CustomOpenSamlObject.TYPE_NAME);
XSAny street = new XSAnyBuilder().buildObject(CustomOpenSamlObject.CUSTOM_NS, "Street",
CustomOpenSamlObject.TYPE_CUSTOM_PREFIX);
street.setTextContent("Test Street");
samlObject.getUnknownXMLObjects().add(street);
XSAny streetNumber = new XSAnyBuilder().buildObject(CustomOpenSamlObject.CUSTOM_NS,
"Number", CustomOpenSamlObject.TYPE_CUSTOM_PREFIX);
streetNumber.setTextContent("1");
samlObject.getUnknownXMLObjects().add(streetNumber);
XSAny zip = new XSAnyBuilder().buildObject(CustomOpenSamlObject.CUSTOM_NS, "ZIP",
CustomOpenSamlObject.TYPE_CUSTOM_PREFIX);
zip.setTextContent("11111");
samlObject.getUnknownXMLObjects().add(zip);
XSAny city = new XSAnyBuilder().buildObject(CustomOpenSamlObject.CUSTOM_NS, "City",
CustomOpenSamlObject.TYPE_CUSTOM_PREFIX);
city.setTextContent("Test City");
samlObject.getUnknownXMLObjects().add(city);
return samlObject;
}
public interface CustomOpenSamlObject extends ElementExtensibleXMLObject {
String TYPE_LOCAL_NAME = "CustomType"; String TYPE_LOCAL_NAME = "CustomType";
@ -61,8 +97,8 @@ public class TestCustomOpenSamlObject {
} }
public static class CustomSamlObjectImpl extends AbstractXMLObject public static class CustomOpenSamlObjectImpl extends AbstractXMLObject
implements TestCustomOpenSamlObject.CustomSamlObject { implements CustomOpenSamlObject {
@Nonnull @Nonnull
private IndexedXMLObjectChildrenList<XMLObject> unknownXMLObjects; private IndexedXMLObjectChildrenList<XMLObject> unknownXMLObjects;
@ -74,7 +110,7 @@ public class TestCustomOpenSamlObject {
* represents * represents
* @param namespacePrefix the prefix for the given namespace * @param namespacePrefix the prefix for the given namespace
*/ */
protected CustomSamlObjectImpl(@Nullable String namespaceURI, @Nonnull String elementLocalName, protected CustomOpenSamlObjectImpl(@Nullable String namespaceURI, @Nonnull String elementLocalName,
@Nullable String namespacePrefix) { @Nullable String namespacePrefix) {
super(namespaceURI, elementLocalName, namespacePrefix); super(namespaceURI, elementLocalName, namespacePrefix);
super.getNamespaceManager().registerNamespaceDeclaration(new Namespace(CUSTOM_NS, TYPE_CUSTOM_PREFIX)); super.getNamespaceManager().registerNamespaceDeclaration(new Namespace(CUSTOM_NS, TYPE_CUSTOM_PREFIX));
@ -122,13 +158,13 @@ public class TestCustomOpenSamlObject {
} }
public static class CustomSamlObjectBuilder public static class CustomSamlObjectBuilder
extends AbstractXMLObjectBuilder<TestCustomOpenSamlObject.CustomSamlObject> { extends AbstractXMLObjectBuilder<CustomOpenSamlObject> {
@Nonnull @Nonnull
@Override @Override
public TestCustomOpenSamlObject.CustomSamlObject buildObject(@Nullable String namespaceURI, public CustomOpenSamlObject buildObject(@Nullable String namespaceURI,
@Nonnull String localName, @Nullable String namespacePrefix) { @Nonnull String localName, @Nullable String namespacePrefix) {
return new TestCustomOpenSamlObject.CustomSamlObjectImpl(namespaceURI, localName, namespacePrefix); return new CustomOpenSamlObjectImpl(namespaceURI, localName, namespacePrefix);
} }
} }
@ -141,7 +177,7 @@ public class TestCustomOpenSamlObject {
@Override @Override
protected void marshallElementContent(@Nonnull XMLObject xmlObject, @Nonnull Element domElement) { protected void marshallElementContent(@Nonnull XMLObject xmlObject, @Nonnull Element domElement) {
final TestCustomOpenSamlObject.CustomSamlObject customSamlObject = (TestCustomOpenSamlObject.CustomSamlObject) xmlObject; final CustomOpenSamlObject customSamlObject = (CustomOpenSamlObject) xmlObject;
for (XMLObject object : customSamlObject.getOrderedChildren()) { for (XMLObject object : customSamlObject.getOrderedChildren()) {
ElementSupport.appendChildElement(domElement, object.getDOM()); ElementSupport.appendChildElement(domElement, object.getDOM());
@ -159,7 +195,7 @@ public class TestCustomOpenSamlObject {
@Override @Override
protected void processChildElement(@Nonnull XMLObject parentXMLObject, @Nonnull XMLObject childXMLObject) protected void processChildElement(@Nonnull XMLObject parentXMLObject, @Nonnull XMLObject childXMLObject)
throws UnmarshallingException { throws UnmarshallingException {
final TestCustomOpenSamlObject.CustomSamlObject customSamlObject = (TestCustomOpenSamlObject.CustomSamlObject) parentXMLObject; final CustomOpenSamlObject customSamlObject = (CustomOpenSamlObject) parentXMLObject;
super.processChildElement(customSamlObject, childXMLObject); super.processChildElement(customSamlObject, childXMLObject);
customSamlObject.getUnknownXMLObjects().add(childXMLObject); customSamlObject.getUnknownXMLObjects().add(childXMLObject);
} }
@ -167,9 +203,9 @@ public class TestCustomOpenSamlObject {
@Nonnull @Nonnull
@Override @Override
protected XMLObject buildXMLObject(@Nonnull Element domElement) { protected XMLObject buildXMLObject(@Nonnull Element domElement) {
return new TestCustomOpenSamlObject.CustomSamlObjectImpl(SAMLConstants.SAML20_NS, return new CustomOpenSamlObjectImpl(SAMLConstants.SAML20_NS,
AttributeValue.DEFAULT_ELEMENT_LOCAL_NAME, AttributeValue.DEFAULT_ELEMENT_LOCAL_NAME,
TestCustomOpenSamlObject.CustomSamlObject.TYPE_CUSTOM_PREFIX); CustomOpenSamlObject.TYPE_CUSTOM_PREFIX);
} }
} }

View File

@ -296,35 +296,15 @@ public final class TestOpenSamlObjects {
return attribute; return attribute;
} }
static List<AttributeStatement> customAttributeStatements() { static AttributeStatement customAttributeStatement(String attributeName, XMLObject customAttributeValue) {
List<AttributeStatement> attributeStatements = new ArrayList<>();
AttributeStatementBuilder attributeStatementBuilder = new AttributeStatementBuilder(); AttributeStatementBuilder attributeStatementBuilder = new AttributeStatementBuilder();
AttributeBuilder attributeBuilder = new AttributeBuilder(); AttributeBuilder attributeBuilder = new AttributeBuilder();
Attribute attribute = attributeBuilder.buildObject(); Attribute attribute = attributeBuilder.buildObject();
attribute.setName("Address"); attribute.setName(attributeName);
TestCustomOpenSamlObject.CustomSamlObject samlObject = new TestCustomOpenSamlObject.CustomSamlObjectBuilder() attribute.getAttributeValues().add(customAttributeValue);
.buildObject(AttributeValue.DEFAULT_ELEMENT_NAME, TestCustomOpenSamlObject.CustomSamlObject.TYPE_NAME);
XSAny street = new XSAnyBuilder().buildObject(TestCustomOpenSamlObject.CustomSamlObject.CUSTOM_NS, "Street",
TestCustomOpenSamlObject.CustomSamlObject.TYPE_CUSTOM_PREFIX);
street.setTextContent("Test Street");
samlObject.getUnknownXMLObjects().add(street);
XSAny streetNumber = new XSAnyBuilder().buildObject(TestCustomOpenSamlObject.CustomSamlObject.CUSTOM_NS,
"Number", TestCustomOpenSamlObject.CustomSamlObject.TYPE_CUSTOM_PREFIX);
streetNumber.setTextContent("1");
samlObject.getUnknownXMLObjects().add(streetNumber);
XSAny zip = new XSAnyBuilder().buildObject(TestCustomOpenSamlObject.CustomSamlObject.CUSTOM_NS, "ZIP",
TestCustomOpenSamlObject.CustomSamlObject.TYPE_CUSTOM_PREFIX);
zip.setTextContent("11111");
samlObject.getUnknownXMLObjects().add(zip);
XSAny city = new XSAnyBuilder().buildObject(TestCustomOpenSamlObject.CustomSamlObject.CUSTOM_NS, "City",
TestCustomOpenSamlObject.CustomSamlObject.TYPE_CUSTOM_PREFIX);
city.setTextContent("Test City");
samlObject.getUnknownXMLObjects().add(city);
attribute.getAttributeValues().add(samlObject);
AttributeStatement attributeStatement = attributeStatementBuilder.buildObject(); AttributeStatement attributeStatement = attributeStatementBuilder.buildObject();
attributeStatement.getAttributes().add(attribute); attributeStatement.getAttributes().add(attribute);
attributeStatements.add(attributeStatement); return attributeStatement;
return attributeStatements;
} }
static List<AttributeStatement> attributeStatements() { static List<AttributeStatement> attributeStatements() {