Skip to content

Commit 726ab52

Browse files
committed
DefaultSaml2AuthenticatedPrincipal Can Be a Map Key
Closes gh-15346
1 parent df7732d commit 726ab52

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

saml2/saml2-service-provider/src/main/java/org/springframework/security/saml2/provider/service/authentication/DefaultSaml2AuthenticatedPrincipal.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@
2020
import java.util.Collections;
2121
import java.util.List;
2222
import java.util.Map;
23+
import java.util.Objects;
2324

2425
import org.springframework.util.Assert;
2526

@@ -78,4 +79,20 @@ public void setRelyingPartyRegistrationId(String registrationId) {
7879
this.registrationId = registrationId;
7980
}
8081

82+
@Override
83+
public boolean equals(Object object) {
84+
if (this == object) {
85+
return true;
86+
}
87+
if (!(object instanceof DefaultSaml2AuthenticatedPrincipal that)) {
88+
return false;
89+
}
90+
return Objects.equals(this.name, that.name) && Objects.equals(this.registrationId, that.registrationId);
91+
}
92+
93+
@Override
94+
public int hashCode() {
95+
return Objects.hash(this.name, this.registrationId);
96+
}
97+
8198
}

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -81,4 +81,24 @@ public void getAttributeWhenDistinctValuesThenReturnsValues() {
8181
assertThat((Instant) registrationInfo.get(1)).isEqualTo(registeredDate);
8282
}
8383

84+
// gh-15346
85+
@Test
86+
public void whenUsedAsKeyInMapThenRetrievableAcrossSerialization() {
87+
Map<Saml2AuthenticatedPrincipal, Integer> valuesByPrincipal = new LinkedHashMap<>();
88+
DefaultSaml2AuthenticatedPrincipal principal = new DefaultSaml2AuthenticatedPrincipal("user", Map.of());
89+
valuesByPrincipal.put(principal, 1);
90+
principal = new DefaultSaml2AuthenticatedPrincipal("user", Map.of());
91+
assertThat(valuesByPrincipal.get(principal)).isEqualTo(1);
92+
principal = new DefaultSaml2AuthenticatedPrincipal("user", Map.of());
93+
principal.setRelyingPartyRegistrationId("id");
94+
assertThat(valuesByPrincipal.get(principal)).isNull();
95+
valuesByPrincipal.put(principal, 2);
96+
principal = new DefaultSaml2AuthenticatedPrincipal("user", Map.of());
97+
principal.setRelyingPartyRegistrationId("id");
98+
assertThat(valuesByPrincipal.get(principal)).isEqualTo(2);
99+
principal = new DefaultSaml2AuthenticatedPrincipal("USER", Map.of());
100+
principal.setRelyingPartyRegistrationId("id");
101+
assertThat(valuesByPrincipal.get(principal)).isNull();
102+
}
103+
84104
}

0 commit comments

Comments
 (0)