Skip to content

Commit 341d949

Browse files
committed
Add test for SAML custom attributes in authentication response
This commit adds a comprehensive test that verifies SAML custom attributes are correctly handled in the authentication response builder. The test ensures: 1. Custom attributes with single and multiple values are properly included 2. The response with custom attributes is still correctly signed 3. The XML schema validation still passes with custom attributes 4. We can locate and verify individual attribute values in the response This provides critical test coverage for the SAML custom attributes feature implementation.
1 parent b83cdaa commit 341d949

File tree

1 file changed

+54
-3
lines changed

1 file changed

+54
-3
lines changed

x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/authn/SuccessfulAuthenticationResponseMessageBuilderTests.java

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,20 @@
1212
import org.elasticsearch.xpack.idp.saml.sp.ServiceProviderDefaults;
1313
import org.elasticsearch.xpack.idp.saml.support.SamlFactory;
1414
import org.elasticsearch.xpack.idp.saml.support.SamlInit;
15+
import org.elasticsearch.xpack.idp.saml.support.SamlInitiateSingleSignOnAttributes;
1516
import org.elasticsearch.xpack.idp.saml.support.XmlValidator;
1617
import org.elasticsearch.xpack.idp.saml.test.IdpSamlTestCase;
1718
import org.junit.Before;
19+
import org.opensaml.saml.saml2.core.Attribute;
20+
import org.opensaml.saml.saml2.core.AttributeStatement;
1821
import org.opensaml.saml.saml2.core.Response;
1922

2023
import java.net.URI;
2124
import java.time.Clock;
2225
import java.time.Duration;
26+
import java.util.ArrayList;
27+
import java.util.Collections;
28+
import java.util.List;
2329
import java.util.Set;
2430

2531
import static org.hamcrest.Matchers.containsString;
@@ -46,13 +52,58 @@ public void setupSaml() throws Exception {
4652
}
4753

4854
public void testSignedResponseIsValidAgainstXmlSchema() throws Exception {
49-
final Response response = buildResponse();
55+
final Response response = buildResponse(null);
5056
final String xml = super.toString(response);
5157
assertThat(xml, containsString("SignedInfo>"));
5258
validator.validate(xml);
5359
}
5460

55-
private Response buildResponse() throws Exception {
61+
public void testSignedResponseWithCustomAttributes() throws Exception {
62+
// Create custom attributes
63+
SamlInitiateSingleSignOnAttributes attributes = new SamlInitiateSingleSignOnAttributes();
64+
List<SamlInitiateSingleSignOnAttributes.Attribute> attributeList = new ArrayList<>();
65+
attributeList.add(new SamlInitiateSingleSignOnAttributes.Attribute("customAttr1", Collections.singletonList("value1")));
66+
67+
List<String> multipleValues = new ArrayList<>();
68+
multipleValues.add("value2A");
69+
multipleValues.add("value2B");
70+
attributeList.add(new SamlInitiateSingleSignOnAttributes.Attribute("customAttr2", multipleValues));
71+
72+
attributes.setAttributes(attributeList);
73+
74+
// Build response with custom attributes
75+
final Response response = buildResponse(attributes);
76+
final String xml = super.toString(response);
77+
78+
// Validate that response is correctly signed
79+
assertThat(xml, containsString("SignedInfo>"));
80+
validator.validate(xml);
81+
82+
// Verify custom attributes are included
83+
boolean foundCustomAttr1 = false;
84+
boolean foundCustomAttr2 = false;
85+
86+
for (AttributeStatement statement : response.getAssertions().get(0).getAttributeStatements()) {
87+
for (Attribute attribute : statement.getAttributes()) {
88+
String name = attribute.getName();
89+
if (name.equals("customAttr1")) {
90+
foundCustomAttr1 = true;
91+
assertEquals(1, attribute.getAttributeValues().size());
92+
assertThat(attribute.getAttributeValues().get(0).getDOM().getTextContent(), containsString("value1"));
93+
} else if (name.equals("customAttr2")) {
94+
foundCustomAttr2 = true;
95+
assertEquals(2, attribute.getAttributeValues().size());
96+
assertThat(attribute.getAttributeValues().get(0).getDOM().getTextContent(), containsString("value2A"));
97+
assertThat(attribute.getAttributeValues().get(1).getDOM().getTextContent(), containsString("value2B"));
98+
}
99+
}
100+
}
101+
102+
assertTrue("Custom attribute 'customAttr1' not found in SAML response", foundCustomAttr1);
103+
assertTrue("Custom attribute 'customAttr2' not found in SAML response", foundCustomAttr2);
104+
}
105+
106+
private Response buildResponse(SamlInitiateSingleSignOnAttributes customAttributes) throws Exception {
56107
final Clock clock = Clock.systemUTC();
57108

58109
final SamlServiceProvider sp = mock(SamlServiceProvider.class);
@@ -75,7 +126,7 @@ private Response buildResponse() throws Exception {
75126
clock,
76127
idp
77128
);
78-
return builder.build(user, null, null);
129+
return builder.build(user, null, customAttributes);
79130
}
80131

81132
}

0 commit comments

Comments
 (0)