Skip to content

Commit e7acd12

Browse files
committed
Allow null or empty authorities for DefaultOAuth2User
Make DefaultOAuth2User more inline with other part of spring-security. For example, - DefaultOAuth2AuthenticatedPrincipal - AbstractAuthenticationToken Closes gh-9366
1 parent 84b5609 commit e7acd12

File tree

3 files changed

+47
-21
lines changed

3 files changed

+47
-21
lines changed

oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/user/DefaultOAuth2User.java

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2021 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.
@@ -30,6 +30,8 @@
3030
import java.util.Comparator;
3131
import java.util.LinkedHashSet;
3232

33+
import org.springframework.security.core.authority.AuthorityUtils;
34+
3335
/**
3436
* The default implementation of an {@link OAuth2User}.
3537
*
@@ -59,14 +61,16 @@ public class DefaultOAuth2User implements OAuth2User, Serializable {
5961
* @param attributes the attributes about the user
6062
* @param nameAttributeKey the key used to access the user's "name" from {@link #getAttributes()}
6163
*/
62-
public DefaultOAuth2User(Collection<? extends GrantedAuthority> authorities, Map<String, Object> attributes, String nameAttributeKey) {
63-
Assert.notEmpty(authorities, "authorities cannot be empty");
64+
public DefaultOAuth2User(Collection<? extends GrantedAuthority> authorities, Map<String, Object> attributes,
65+
String nameAttributeKey) {
6466
Assert.notEmpty(attributes, "attributes cannot be empty");
6567
Assert.hasText(nameAttributeKey, "nameAttributeKey cannot be empty");
6668
if (!attributes.containsKey(nameAttributeKey)) {
6769
throw new IllegalArgumentException("Missing attribute '" + nameAttributeKey + "' in attributes");
6870
}
69-
this.authorities = Collections.unmodifiableSet(new LinkedHashSet<>(this.sortAuthorities(authorities)));
71+
this.authorities = (authorities != null)
72+
? Collections.unmodifiableSet(new LinkedHashSet<>(this.sortAuthorities(authorities)))
73+
: Collections.unmodifiableSet(new LinkedHashSet<>(AuthorityUtils.NO_AUTHORITIES));
7074
this.attributes = Collections.unmodifiableMap(new LinkedHashMap<>(attributes));
7175
this.nameAttributeKey = nameAttributeKey;
7276
}

oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/oidc/user/DefaultOidcUserTests.java

+22-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2021 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.
@@ -18,6 +18,7 @@
1818

1919
import org.junit.Test;
2020
import org.springframework.security.core.GrantedAuthority;
21+
import org.springframework.security.core.authority.AuthorityUtils;
2122
import org.springframework.security.core.authority.SimpleGrantedAuthority;
2223
import org.springframework.security.oauth2.core.oidc.IdTokenClaimNames;
2324
import org.springframework.security.oauth2.core.oidc.OidcIdToken;
@@ -57,11 +58,6 @@ public class DefaultOidcUserTests {
5758
private static final OidcIdToken ID_TOKEN = new OidcIdToken("id-token-value", Instant.EPOCH, Instant.MAX, ID_TOKEN_CLAIMS);
5859
private static final OidcUserInfo USER_INFO = new OidcUserInfo(USER_INFO_CLAIMS);
5960

60-
@Test(expected = IllegalArgumentException.class)
61-
public void constructorWhenAuthoritiesIsNullThenThrowIllegalArgumentException() {
62-
new DefaultOidcUser(null, ID_TOKEN);
63-
}
64-
6561
@Test(expected = IllegalArgumentException.class)
6662
public void constructorWhenIdTokenIsNullThenThrowIllegalArgumentException() {
6763
new DefaultOidcUser(AUTHORITIES, null);
@@ -72,6 +68,26 @@ public void constructorWhenNameAttributeKeyInvalidThenThrowIllegalArgumentExcept
7268
new DefaultOidcUser(AUTHORITIES, ID_TOKEN, "invalid");
7369
}
7470

71+
@Test
72+
public void constructorWhenAuthoritiesIsNullThenCreatedWithEmptyAuthorities() {
73+
DefaultOidcUser user = new DefaultOidcUser(null, ID_TOKEN);
74+
assertThat(user.getClaims()).containsOnlyKeys(IdTokenClaimNames.ISS, IdTokenClaimNames.SUB);
75+
assertThat(user.getIdToken()).isEqualTo(ID_TOKEN);
76+
assertThat(user.getName()).isEqualTo(SUBJECT);
77+
assertThat(user.getAuthorities()).isEmpty();
78+
assertThat(user.getAttributes()).containsOnlyKeys(IdTokenClaimNames.ISS, IdTokenClaimNames.SUB);
79+
}
80+
81+
@Test
82+
public void constructorWhenAuthoritiesIsEmptyThenCreated() {
83+
DefaultOidcUser user = new DefaultOidcUser(AuthorityUtils.NO_AUTHORITIES, ID_TOKEN);
84+
assertThat(user.getClaims()).containsOnlyKeys(IdTokenClaimNames.ISS, IdTokenClaimNames.SUB);
85+
assertThat(user.getIdToken()).isEqualTo(ID_TOKEN);
86+
assertThat(user.getName()).isEqualTo(SUBJECT);
87+
assertThat(user.getAuthorities()).isEmpty();
88+
assertThat(user.getAttributes()).containsOnlyKeys(IdTokenClaimNames.ISS, IdTokenClaimNames.SUB);
89+
}
90+
7591
@Test
7692
public void constructorWhenAuthoritiesIdTokenProvidedThenCreated() {
7793
DefaultOidcUser user = new DefaultOidcUser(AUTHORITIES, ID_TOKEN);

oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/user/DefaultOAuth2UserTests.java

+17-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2021 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.
@@ -41,16 +41,6 @@ public class DefaultOAuth2UserTests {
4141
private static final Map<String, Object> ATTRIBUTES = Collections.singletonMap(
4242
ATTRIBUTE_NAME_KEY, USERNAME);
4343

44-
@Test(expected = IllegalArgumentException.class)
45-
public void constructorWhenAuthoritiesIsNullThenThrowIllegalArgumentException() {
46-
new DefaultOAuth2User(null, ATTRIBUTES, ATTRIBUTE_NAME_KEY);
47-
}
48-
49-
@Test(expected = IllegalArgumentException.class)
50-
public void constructorWhenAuthoritiesIsEmptyThenThrowIllegalArgumentException() {
51-
new DefaultOAuth2User(Collections.emptySet(), ATTRIBUTES, ATTRIBUTE_NAME_KEY);
52-
}
53-
5444
@Test(expected = IllegalArgumentException.class)
5545
public void constructorWhenAttributesIsNullThenThrowIllegalArgumentException() {
5646
new DefaultOAuth2User(AUTHORITIES, null, ATTRIBUTE_NAME_KEY);
@@ -71,6 +61,22 @@ public void constructorWhenNameAttributeKeyIsInvalidThenThrowIllegalArgumentExce
7161
new DefaultOAuth2User(AUTHORITIES, ATTRIBUTES, "invalid");
7262
}
7363

64+
@Test
65+
public void constructorWhenAuthoritiesIsNullThenCreatedWithEmptyAuthorities() {
66+
DefaultOAuth2User user = new DefaultOAuth2User(null, ATTRIBUTES, ATTRIBUTE_NAME_KEY);
67+
assertThat(user.getName()).isEqualTo(USERNAME);
68+
assertThat(user.getAuthorities()).isEmpty();
69+
assertThat(user.getAttributes()).containsOnlyKeys(ATTRIBUTE_NAME_KEY);
70+
}
71+
72+
@Test
73+
public void constructorWhenAuthoritiesIsEmptyThenCreated() {
74+
DefaultOAuth2User user = new DefaultOAuth2User(Collections.emptySet(), ATTRIBUTES, ATTRIBUTE_NAME_KEY);
75+
assertThat(user.getName()).isEqualTo(USERNAME);
76+
assertThat(user.getAuthorities()).isEmpty();
77+
assertThat(user.getAttributes()).containsOnlyKeys(ATTRIBUTE_NAME_KEY);
78+
}
79+
7480
@Test
7581
public void constructorWhenAllParametersProvidedAndValidThenCreated() {
7682
DefaultOAuth2User user = new DefaultOAuth2User(AUTHORITIES, ATTRIBUTES, ATTRIBUTE_NAME_KEY);

0 commit comments

Comments
 (0)