Skip to content

Commit 1fba07d

Browse files
committed
Make the SamlResponse returned attribute map preserve attribute order
The map returned by SamlResposne containing the attributes returned by the IdP now preserves the order in which such attributes appear in the SAML response XML. This is not strictly mandatory, but a plus. Indeed, the test method com.onelogin.saml2.test.AuthTest.testProcessResponse() was not deterministic before this change: indeed, the iteration order of HashMap is undetermined so expecting to see attribute names in a given order could lead to a test failure. This change also fixes this and attribute names are expected now to be seen in the order in which the corresponding attributes appear in the test XML file.
1 parent 523786b commit 1fba07d

File tree

3 files changed

+6
-4
lines changed

3 files changed

+6
-4
lines changed

core/src/main/java/com/onelogin/saml2/authn/SamlResponse.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.security.cert.X509Certificate;
66
import java.util.ArrayList;
77
import java.util.HashMap;
8+
import java.util.LinkedHashMap;
89
import java.util.List;
910
import java.util.Map;
1011
import java.util.Objects;
@@ -572,7 +573,7 @@ public String getNameIdSPNameQualifier() throws Exception {
572573
*
573574
*/
574575
public HashMap<String, List<String>> getAttributes() throws XPathExpressionException, ValidationError {
575-
HashMap<String, List<String>> attributes = new HashMap<String, List<String>>();
576+
HashMap<String, List<String>> attributes = new LinkedHashMap<String, List<String>>();
576577

577578
NodeList nodes = this.queryAssertion("/saml:AttributeStatement/saml:Attribute");
578579

toolkit/src/main/java/com/onelogin/saml2/Auth.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public class Auth {
116116
/**
117117
* User attributes data.
118118
*/
119-
private Map<String, List<String>> attributes = new HashMap<String, List<String>>();
119+
private Map<String, List<String>> attributes = new LinkedHashMap<String, List<String>>();
120120

121121
/**
122122
* If user is authenticated.

toolkit/src/test/java/com/onelogin/saml2/test/AuthTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.security.cert.CertificateException;
3232
import java.util.ArrayList;
3333
import java.util.HashMap;
34+
import java.util.LinkedHashMap;
3435
import java.util.List;
3536
import java.util.Map;
3637

@@ -524,7 +525,7 @@ public void testProcessResponse() throws Exception {
524525
when(request.getParameterMap()).thenReturn(singletonMap("SAMLResponse", new String[]{samlResponseEncoded}));
525526
Auth auth2 = new Auth(settings, request, response);
526527

527-
HashMap<String, List<String>> expectedAttributes = new HashMap<String, List<String>>();
528+
HashMap<String, List<String>> expectedAttributes = new LinkedHashMap<String, List<String>>();
528529
List<String> attrValues = new ArrayList<String>();
529530
attrValues.add("smartin");
530531
List<String> attrValues2 = new ArrayList<String>();
@@ -538,9 +539,9 @@ public void testProcessResponse() throws Exception {
538539
attrValues5.add("Martin2");
539540
expectedAttributes.put("uid", attrValues);
540541
expectedAttributes.put("mail", attrValues2);
541-
expectedAttributes.put("eduPersonAffiliation", attrValues3);
542542
expectedAttributes.put("cn", attrValues4);
543543
expectedAttributes.put("sn", attrValues5);
544+
expectedAttributes.put("eduPersonAffiliation", attrValues3);
544545
List<String> keys = new ArrayList<String>(expectedAttributes.keySet());
545546

546547
assertFalse(auth2.isAuthenticated());

0 commit comments

Comments
 (0)