Skip to content

Commit d991019

Browse files
committed
Check for null Name Attribute Value
Closes gh-15338
1 parent beff600 commit d991019

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2024 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.
@@ -44,6 +44,7 @@
4444
*
4545
* @author Joe Grandja
4646
* @author Eddú Meléndez
47+
* @author Park Hyojong
4748
* @since 5.0
4849
* @see OAuth2User
4950
*/
@@ -68,9 +69,8 @@ public DefaultOAuth2User(Collection<? extends GrantedAuthority> authorities, Map
6869
String nameAttributeKey) {
6970
Assert.notEmpty(attributes, "attributes cannot be empty");
7071
Assert.hasText(nameAttributeKey, "nameAttributeKey cannot be empty");
71-
if (!attributes.containsKey(nameAttributeKey)) {
72-
throw new IllegalArgumentException("Missing attribute '" + nameAttributeKey + "' in attributes");
73-
}
72+
Assert.notNull(attributes.get(nameAttributeKey), "Attribute value for '" + nameAttributeKey + "' cannot be null");
73+
7474
this.authorities = (authorities != null)
7575
? Collections.unmodifiableSet(new LinkedHashSet<>(this.sortAuthorities(authorities)))
7676
: Collections.unmodifiableSet(new LinkedHashSet<>(AuthorityUtils.NO_AUTHORITIES));

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2024 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.
@@ -34,6 +34,7 @@
3434
*
3535
* @author Vedran Pavic
3636
* @author Joe Grandja
37+
* @author Park Hyojong
3738
*/
3839
public class DefaultOAuth2UserTests {
3940

@@ -59,6 +60,12 @@ public void constructorWhenAttributesIsEmptyThenThrowIllegalArgumentException()
5960
.isThrownBy(() -> new DefaultOAuth2User(AUTHORITIES, Collections.emptyMap(), ATTRIBUTE_NAME_KEY));
6061
}
6162

63+
@Test
64+
public void constructorWhenAttributeValueIsNullThenThrowIllegalArgumentException() {
65+
assertThatIllegalArgumentException().isThrownBy(() -> new DefaultOAuth2User(AUTHORITIES,
66+
Collections.singletonMap(ATTRIBUTE_NAME_KEY, null), ATTRIBUTE_NAME_KEY));
67+
}
68+
6269
@Test
6370
public void constructorWhenNameAttributeKeyIsNullThenThrowIllegalArgumentException() {
6471
assertThatIllegalArgumentException().isThrownBy(() -> new DefaultOAuth2User(AUTHORITIES, ATTRIBUTES, null));

0 commit comments

Comments
 (0)