Skip to content

Commit 0962459

Browse files
committed
Update SimpleSaml2AuthenticatedPrincipal class name
Rename it to DefaultSaml2AuthenticatedPrincipal to be more in line with the respective class in the OAuth2 module. Also make the class public to be able to whitelist the SAML2 auth classes in Jackson object mappers for deserialization in e.g. Spring Session MongoDB. Closes gh-8852
1 parent 7c4a706 commit 0962459

File tree

3 files changed

+32
-10
lines changed

3 files changed

+32
-10
lines changed
Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.security.saml2.provider.service.authentication;
1818

19+
import org.springframework.util.Assert;
20+
1921
import java.io.Serializable;
2022
import java.util.List;
2123
import java.util.Map;
@@ -24,14 +26,17 @@
2426
* Default implementation of a {@link Saml2AuthenticatedPrincipal}.
2527
*
2628
* @author Clement Stoquart
27-
* @since 5.2.2
29+
* @since 5.4
2830
*/
29-
class SimpleSaml2AuthenticatedPrincipal implements Saml2AuthenticatedPrincipal, Serializable {
31+
public class DefaultSaml2AuthenticatedPrincipal implements Saml2AuthenticatedPrincipal, Serializable {
3032

3133
private final String name;
3234
private final Map<String, List<Object>> attributes;
3335

34-
SimpleSaml2AuthenticatedPrincipal(String name, Map<String, List<Object>> attributes) {
36+
public DefaultSaml2AuthenticatedPrincipal(String name, Map<String, List<Object>> attributes) {
37+
Assert.notNull(name, "name cannot be null");
38+
Assert.notNull(attributes, "attributes cannot be null");
39+
3540
this.name = name;
3641
this.attributes = attributes;
3742
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ public final class OpenSamlAuthenticationProvider implements AuthenticationProvi
183183
String username = assertion.getSubject().getNameID().getValue();
184184
Map<String, List<Object>> attributes = getAssertionAttributes(assertion);
185185
return new Saml2Authentication(
186-
new SimpleSaml2AuthenticatedPrincipal(username, attributes), token.getSaml2Response(),
186+
new DefaultSaml2AuthenticatedPrincipal(username, attributes), token.getSaml2Response(),
187187
this.authoritiesMapper.mapAuthorities(getAssertionAuthorities(assertion)));
188188
};
189189

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,31 +26,48 @@
2626
import java.util.Map;
2727

2828
import static org.assertj.core.api.Assertions.assertThat;
29+
import static org.assertj.core.api.Assertions.assertThatCode;
2930

30-
public class SimpleSaml2AuthenticatedPrincipalTests {
31+
public class DefaultSaml2AuthenticatedPrincipalTests {
3132

3233
@Test
33-
public void createSimpleSaml2AuthenticatedPrincipal() {
34+
public void createDefaultSaml2AuthenticatedPrincipal() {
3435
Map<String, List<Object>> attributes = new LinkedHashMap<>();
3536
attributes.put("email", Arrays.asList("[email protected]", "[email protected]"));
36-
SimpleSaml2AuthenticatedPrincipal principal = new SimpleSaml2AuthenticatedPrincipal("user", attributes);
37+
DefaultSaml2AuthenticatedPrincipal principal = new DefaultSaml2AuthenticatedPrincipal("user", attributes);
3738
assertThat(principal.getName()).isEqualTo("user");
3839
assertThat(principal.getAttributes()).isEqualTo(attributes);
3940
}
4041

42+
@Test
43+
public void createDefaultSaml2AuthenticatedPrincipalWhenNameNullThenException() {
44+
Map<String, List<Object>> attributes = new LinkedHashMap<>();
45+
attributes.put("email", Arrays.asList("[email protected]", "[email protected]"));
46+
assertThatCode(() -> new DefaultSaml2AuthenticatedPrincipal(null, attributes))
47+
.isInstanceOf(IllegalArgumentException.class)
48+
.hasMessageContaining("name cannot be null");
49+
}
50+
51+
@Test
52+
public void createDefaultSaml2AuthenticatedPrincipalWhenAttributesNullThenException() {
53+
assertThatCode(() -> new DefaultSaml2AuthenticatedPrincipal("user", null))
54+
.isInstanceOf(IllegalArgumentException.class)
55+
.hasMessageContaining("attributes cannot be null");
56+
}
57+
4158
@Test
4259
public void getFirstAttributeWhenStringValueThenReturnsValue() {
4360
Map<String, List<Object>> attributes = new LinkedHashMap<>();
4461
attributes.put("email", Arrays.asList("[email protected]", "[email protected]"));
45-
SimpleSaml2AuthenticatedPrincipal principal = new SimpleSaml2AuthenticatedPrincipal("user", attributes);
62+
DefaultSaml2AuthenticatedPrincipal principal = new DefaultSaml2AuthenticatedPrincipal("user", attributes);
4663
assertThat(principal.<String>getFirstAttribute("email")).isEqualTo(attributes.get("email").get(0));
4764
}
4865

4966
@Test
5067
public void getAttributeWhenStringValuesThenReturnsValues() {
5168
Map<String, List<Object>> attributes = new LinkedHashMap<>();
5269
attributes.put("email", Arrays.asList("[email protected]", "[email protected]"));
53-
SimpleSaml2AuthenticatedPrincipal principal = new SimpleSaml2AuthenticatedPrincipal("user", attributes);
70+
DefaultSaml2AuthenticatedPrincipal principal = new DefaultSaml2AuthenticatedPrincipal("user", attributes);
5471
assertThat(principal.<String>getAttribute("email")).isEqualTo(attributes.get("email"));
5572
}
5673

@@ -62,7 +79,7 @@ public void getAttributeWhenDistinctValuesThenReturnsValues() {
6279
Map<String, List<Object>> attributes = new LinkedHashMap<>();
6380
attributes.put("registration", Arrays.asList(registered, registeredDate));
6481

65-
SimpleSaml2AuthenticatedPrincipal principal = new SimpleSaml2AuthenticatedPrincipal("user", attributes);
82+
DefaultSaml2AuthenticatedPrincipal principal = new DefaultSaml2AuthenticatedPrincipal("user", attributes);
6683

6784
List<Object> registrationInfo = principal.getAttribute("registration");
6885

0 commit comments

Comments
 (0)