Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug #16340: check username attribute when calling user.getName() (instead of checking in constructor) #16546

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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