Skip to content

Commit

Permalink
Bug spring-projects#16340: check username attribute when calling user…
Browse files Browse the repository at this point in the history
….getName()
  • Loading branch information
jloisel committed Feb 6, 2025
1 parent 4776446 commit ddae9d2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ public DefaultOAuth2User(Collection<? extends GrantedAuthority> authorities, Map
String nameAttributeKey) {
Assert.notEmpty(attributes, "attributes cannot be empty");
Assert.hasText(nameAttributeKey, "nameAttributeKey cannot be empty");
Assert.notNull(attributes.get(nameAttributeKey),
"Attribute value for '" + nameAttributeKey + "' cannot be null");

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

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

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ public void constructorWhenAttributesIsEmptyThenThrowIllegalArgumentException()
}

@Test
public void constructorWhenAttributeValueIsNullThenThrowIllegalArgumentException() {
assertThatIllegalArgumentException().isThrownBy(() -> new DefaultOAuth2User(AUTHORITIES,
Collections.singletonMap(ATTRIBUTE_NAME_KEY, null), ATTRIBUTE_NAME_KEY));
public void getNameWhenAttributeValueIsNullThenThrowIllegalArgumentException() {
final DefaultOAuth2User user = new DefaultOAuth2User(AUTHORITIES,
Collections.singletonMap(ATTRIBUTE_NAME_KEY, null), ATTRIBUTE_NAME_KEY);
assertThatIllegalArgumentException().isThrownBy(user::getName);
}

@Test
Expand All @@ -72,9 +73,10 @@ public void constructorWhenNameAttributeKeyIsNullThenThrowIllegalArgumentExcepti
}

@Test
public void constructorWhenNameAttributeKeyIsInvalidThenThrowIllegalArgumentException() {
public void getNameWhenNameAttributeKeyIsInvalidThenThrowIllegalArgumentException() {
final DefaultOAuth2User user = new DefaultOAuth2User(AUTHORITIES, ATTRIBUTES, "invalid");
assertThatIllegalArgumentException()
.isThrownBy(() -> new DefaultOAuth2User(AUTHORITIES, ATTRIBUTES, "invalid"));
.isThrownBy(user::getName);
}

@Test
Expand Down

0 comments on commit ddae9d2

Please sign in to comment.