Skip to content

Commit 56a6133

Browse files
committed
Merge Same-named Attribute Elements
Closes gh-11042
1 parent aaf20e7 commit 56a6133

File tree

4 files changed

+18
-3
lines changed

4 files changed

+18
-3
lines changed

saml2/saml2-service-provider/src/opensaml3Test/java/org/springframework/security/saml2/provider/service/authentication/OpenSamlAuthenticationProviderTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ public void authenticateWhenAssertionContainsAttributesThenItSucceeds() {
244244
expected.put("age", Collections.singletonList(21));
245245
expected.put("website", Collections.singletonList("https://johndoe.com/"));
246246
expected.put("registered", Collections.singletonList(true));
247+
expected.put("role", Arrays.asList("RoleTwo"));
247248
Instant registeredDate = Instant.ofEpochMilli(DateTime.parse("1970-01-01T00:00:00Z").getMillis());
248249
expected.put("registeredDate", Collections.singletonList(registeredDate));
249250
assertThat((String) principal.getFirstAttribute("name")).isEqualTo("John Doe");

saml2/saml2-service-provider/src/opensaml4Main/java/org/springframework/security/saml2/provider/service/authentication/OpenSaml4AuthenticationProvider.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import java.util.Collection;
2424
import java.util.Collections;
2525
import java.util.HashMap;
26-
import java.util.LinkedHashMap;
2726
import java.util.List;
2827
import java.util.Map;
2928
import java.util.function.Consumer;
@@ -86,6 +85,8 @@
8685
import org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistration;
8786
import org.springframework.util.Assert;
8887
import org.springframework.util.CollectionUtils;
88+
import org.springframework.util.LinkedMultiValueMap;
89+
import org.springframework.util.MultiValueMap;
8990
import org.springframework.util.StringUtils;
9091

9192
/**
@@ -601,7 +602,7 @@ private boolean hasName(Assertion assertion) {
601602
}
602603

603604
private static Map<String, List<Object>> getAssertionAttributes(Assertion assertion) {
604-
Map<String, List<Object>> attributeMap = new LinkedHashMap<>();
605+
MultiValueMap<String, Object> attributeMap = new LinkedMultiValueMap<>();
605606
for (AttributeStatement attributeStatement : assertion.getAttributeStatements()) {
606607
for (Attribute attribute : attributeStatement.getAttributes()) {
607608
List<Object> attributeValues = new ArrayList<>();
@@ -611,7 +612,7 @@ private static Map<String, List<Object>> getAssertionAttributes(Assertion assert
611612
attributeValues.add(attributeValue);
612613
}
613614
}
614-
attributeMap.put(attribute.getName(), attributeValues);
615+
attributeMap.addAll(attribute.getName(), attributeValues);
615616
}
616617
}
617618
return attributeMap;

saml2/saml2-service-provider/src/opensaml4Test/java/org/springframework/security/saml2/provider/service/authentication/OpenSaml4AuthenticationProviderTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ public void authenticateWhenAssertionContainsAttributesThenItSucceeds() {
245245
expected.put("registered", Collections.singletonList(true));
246246
Instant registeredDate = Instant.parse("1970-01-01T00:00:00Z");
247247
expected.put("registeredDate", Collections.singletonList(registeredDate));
248+
expected.put("role", Arrays.asList("RoleOne", "RoleTwo")); // gh-11042
248249
assertThat((String) principal.getFirstAttribute("name")).isEqualTo("John Doe");
249250
assertThat(principal.getAttributes()).isEqualTo(expected);
250251
}

saml2/saml2-service-provider/src/test/java/org/springframework/security/saml2/provider/service/authentication/TestOpenSamlObjects.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,18 @@ static List<AttributeStatement> attributeStatements() {
312312
name.setValue("John Doe");
313313
nameAttr.getAttributeValues().add(name);
314314
attrStmt1.getAttributes().add(nameAttr);
315+
Attribute roleOneAttr = attributeBuilder.buildObject(); // gh-11042
316+
roleOneAttr.setName("role");
317+
XSString roleOne = new XSStringBuilder().buildObject(AttributeValue.DEFAULT_ELEMENT_NAME, XSString.TYPE_NAME);
318+
roleOne.setValue("RoleOne");
319+
roleOneAttr.getAttributeValues().add(roleOne);
320+
attrStmt1.getAttributes().add(roleOneAttr);
321+
Attribute roleTwoAttr = attributeBuilder.buildObject(); // gh-11042
322+
roleTwoAttr.setName("role");
323+
XSString roleTwo = new XSStringBuilder().buildObject(AttributeValue.DEFAULT_ELEMENT_NAME, XSString.TYPE_NAME);
324+
roleTwo.setValue("RoleTwo");
325+
roleTwoAttr.getAttributeValues().add(roleTwo);
326+
attrStmt1.getAttributes().add(roleTwoAttr);
315327
Attribute ageAttr = attributeBuilder.buildObject();
316328
ageAttr.setName("age");
317329
XSInteger age = new XSIntegerBuilder().buildObject(AttributeValue.DEFAULT_ELEMENT_NAME, XSInteger.TYPE_NAME);

0 commit comments

Comments
 (0)