Skip to content

Commit

Permalink
Merge Same-named Attribute Elements
Browse files Browse the repository at this point in the history
Closes gh-11042
  • Loading branch information
jzheaux committed Jul 21, 2022
1 parent bf138c5 commit e092ec7
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ public void authenticateWhenAssertionContainsAttributesThenItSucceeds() {
expected.put("age", Collections.singletonList(21));
expected.put("website", Collections.singletonList("https://johndoe.com/"));
expected.put("registered", Collections.singletonList(true));
expected.put("role", Arrays.asList("RoleTwo"));
Instant registeredDate = Instant.ofEpochMilli(DateTime.parse("1970-01-01T00:00:00Z").getMillis());
expected.put("registeredDate", Collections.singletonList(registeredDate));
assertThat((String) principal.getFirstAttribute("name")).isEqualTo("John Doe");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
Expand Down Expand Up @@ -92,6 +91,8 @@
import org.springframework.security.saml2.provider.service.registration.Saml2MessageBinding;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;

/**
Expand Down Expand Up @@ -645,7 +646,7 @@ private boolean hasName(Assertion assertion) {
}

private static Map<String, List<Object>> getAssertionAttributes(Assertion assertion) {
Map<String, List<Object>> attributeMap = new LinkedHashMap<>();
MultiValueMap<String, Object> attributeMap = new LinkedMultiValueMap<>();
for (AttributeStatement attributeStatement : assertion.getAttributeStatements()) {
for (Attribute attribute : attributeStatement.getAttributes()) {
List<Object> attributeValues = new ArrayList<>();
Expand All @@ -655,7 +656,7 @@ private static Map<String, List<Object>> getAssertionAttributes(Assertion assert
attributeValues.add(attributeValue);
}
}
attributeMap.put(attribute.getName(), attributeValues);
attributeMap.addAll(attribute.getName(), attributeValues);
}
}
return attributeMap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ public void authenticateWhenAssertionContainsAttributesThenItSucceeds() {
expected.put("registered", Collections.singletonList(true));
Instant registeredDate = Instant.parse("1970-01-01T00:00:00Z");
expected.put("registeredDate", Collections.singletonList(registeredDate));
expected.put("role", Arrays.asList("RoleOne", "RoleTwo")); // gh-11042
assertThat((String) principal.getFirstAttribute("name")).isEqualTo("John Doe");
assertThat(principal.getAttributes()).isEqualTo(expected);
assertThat(principal.getSessionIndexes()).contains("session-index");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,18 @@ static List<AttributeStatement> attributeStatements() {
name.setValue("John Doe");
nameAttr.getAttributeValues().add(name);
attrStmt1.getAttributes().add(nameAttr);
Attribute roleOneAttr = attributeBuilder.buildObject(); // gh-11042
roleOneAttr.setName("role");
XSString roleOne = new XSStringBuilder().buildObject(AttributeValue.DEFAULT_ELEMENT_NAME, XSString.TYPE_NAME);
roleOne.setValue("RoleOne");
roleOneAttr.getAttributeValues().add(roleOne);
attrStmt1.getAttributes().add(roleOneAttr);
Attribute roleTwoAttr = attributeBuilder.buildObject(); // gh-11042
roleTwoAttr.setName("role");
XSString roleTwo = new XSStringBuilder().buildObject(AttributeValue.DEFAULT_ELEMENT_NAME, XSString.TYPE_NAME);
roleTwo.setValue("RoleTwo");
roleTwoAttr.getAttributeValues().add(roleTwo);
attrStmt1.getAttributes().add(roleTwoAttr);
Attribute ageAttr = attributeBuilder.buildObject();
ageAttr.setName("age");
XSInteger age = new XSIntegerBuilder().buildObject(AttributeValue.DEFAULT_ELEMENT_NAME, XSInteger.TYPE_NAME);
Expand Down

0 comments on commit e092ec7

Please sign in to comment.