Skip to content

Commit ddae9d2

Browse files
committed
Bug #16340: check username attribute when calling user.getName()
1 parent 4776446 commit ddae9d2

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,6 @@ public DefaultOAuth2User(Collection<? extends GrantedAuthority> authorities, Map
6969
String nameAttributeKey) {
7070
Assert.notEmpty(attributes, "attributes cannot be empty");
7171
Assert.hasText(nameAttributeKey, "nameAttributeKey cannot be empty");
72-
Assert.notNull(attributes.get(nameAttributeKey),
73-
"Attribute value for '" + nameAttributeKey + "' cannot be null");
7472

7573
this.authorities = (authorities != null)
7674
? Collections.unmodifiableSet(new LinkedHashSet<>(this.sortAuthorities(authorities)))
@@ -81,7 +79,9 @@ public DefaultOAuth2User(Collection<? extends GrantedAuthority> authorities, Map
8179

8280
@Override
8381
public String getName() {
84-
return this.getAttribute(this.nameAttributeKey).toString();
82+
final Object name = attributes.get(nameAttributeKey);
83+
Assert.notNull(name, "Attribute value for '" + nameAttributeKey + "' cannot be null");
84+
return name.toString();
8585
}
8686

8787
@Override

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

+7-5
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,10 @@ public void constructorWhenAttributesIsEmptyThenThrowIllegalArgumentException()
6161
}
6262

6363
@Test
64-
public void constructorWhenAttributeValueIsNullThenThrowIllegalArgumentException() {
65-
assertThatIllegalArgumentException().isThrownBy(() -> new DefaultOAuth2User(AUTHORITIES,
66-
Collections.singletonMap(ATTRIBUTE_NAME_KEY, null), ATTRIBUTE_NAME_KEY));
64+
public void getNameWhenAttributeValueIsNullThenThrowIllegalArgumentException() {
65+
final DefaultOAuth2User user = new DefaultOAuth2User(AUTHORITIES,
66+
Collections.singletonMap(ATTRIBUTE_NAME_KEY, null), ATTRIBUTE_NAME_KEY);
67+
assertThatIllegalArgumentException().isThrownBy(user::getName);
6768
}
6869

6970
@Test
@@ -72,9 +73,10 @@ public void constructorWhenNameAttributeKeyIsNullThenThrowIllegalArgumentExcepti
7273
}
7374

7475
@Test
75-
public void constructorWhenNameAttributeKeyIsInvalidThenThrowIllegalArgumentException() {
76+
public void getNameWhenNameAttributeKeyIsInvalidThenThrowIllegalArgumentException() {
77+
final DefaultOAuth2User user = new DefaultOAuth2User(AUTHORITIES, ATTRIBUTES, "invalid");
7678
assertThatIllegalArgumentException()
77-
.isThrownBy(() -> new DefaultOAuth2User(AUTHORITIES, ATTRIBUTES, "invalid"));
79+
.isThrownBy(user::getName);
7880
}
7981

8082
@Test

0 commit comments

Comments
 (0)