Skip to content

Commit 5b2212b

Browse files
committed
Add copy constructor for DefaultOAuth2User
Accepts null name for consistency with DefaultOAuth2AuthenticatedPrincipal. The private static method DefaultOAuth2User.getNameFromAttributes was added in order to keep assertions about nameAttributeKey and the looked up name for the deprecated constructor.
1 parent 1cc7a73 commit 5b2212b

File tree

8 files changed

+44
-22
lines changed

8 files changed

+44
-22
lines changed

docs/modules/ROOT/pages/reactive/test/web/oauth2.adoc

+2-2
Original file line numberDiff line numberDiff line change
@@ -509,9 +509,9 @@ Java::
509509
[source,java,role="primary"]
510510
----
511511
OAuth2User oauth2User = new DefaultOAuth2User(
512-
AuthorityUtils.createAuthorityList("SCOPE_message:read"),
512+
"foo_user",
513513
Collections.singletonMap("user_name", "foo_user"),
514-
"user_name");
514+
AuthorityUtils.createAuthorityList("SCOPE_message:read"));
515515
516516
client
517517
.mutateWith(mockOAuth2Login().oauth2User(oauth2User))

docs/modules/ROOT/pages/servlet/test/mockmvc/oauth2.adoc

+2-2
Original file line numberDiff line numberDiff line change
@@ -514,9 +514,9 @@ Java::
514514
[source,java,role="primary"]
515515
----
516516
OAuth2User oauth2User = new DefaultOAuth2User(
517-
AuthorityUtils.createAuthorityList("SCOPE_message:read"),
517+
"foo_user",
518518
Collections.singletonMap("user_name", "foo_user"),
519-
"user_name");
519+
AuthorityUtils.createAuthorityList("SCOPE_message:read"));
520520
521521
mvc
522522
.perform(get("/endpoint")

oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/jackson2/DefaultOAuth2UserMixin.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 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.
@@ -42,10 +42,16 @@
4242
@JsonIgnoreProperties(ignoreUnknown = true)
4343
abstract class DefaultOAuth2UserMixin {
4444

45+
@Deprecated
4546
@JsonCreator
4647
DefaultOAuth2UserMixin(@JsonProperty("authorities") Collection<? extends GrantedAuthority> authorities,
4748
@JsonProperty("attributes") Map<String, Object> attributes,
4849
@JsonProperty("nameAttributeKey") String nameAttributeKey) {
4950
}
5051

52+
@JsonCreator
53+
DefaultOAuth2UserMixin(@JsonProperty("name") String name,
54+
@JsonProperty("attributes") Map<String, Object> attributes,
55+
@JsonProperty("authorities") Collection<? extends GrantedAuthority> authorities) {
56+
}
5157
}

oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/userinfo/DefaultOAuth2UserService.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2Authentic
9696
OAuth2AccessToken token = userRequest.getAccessToken();
9797
Map<String, Object> attributes = this.attributesConverter.convert(userRequest).convert(response.getBody());
9898
Collection<GrantedAuthority> authorities = getAuthorities(token, attributes, userNameAttributeName);
99-
return new DefaultOAuth2User(authorities, attributes, userNameAttributeName);
99+
return new DefaultOAuth2User(attributes.get(userNameAttributeName).toString(), attributes, authorities);
100100
}
101101

102102
/**

oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/userinfo/DefaultReactiveOAuth2UserService.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public Mono<OAuth2User> loadUser(OAuth2UserRequest userRequest) throws OAuth2Aut
138138
authorities.add(new SimpleGrantedAuthority("SCOPE_" + scope));
139139
}
140140

141-
return new DefaultOAuth2User(authorities, attrs, userNameAttributeName);
141+
return new DefaultOAuth2User(attrs.get(userNameAttributeName).toString(), attrs, authorities);
142142
})
143143
.onErrorMap((ex) -> (ex instanceof UnsupportedMediaTypeException ||
144144
ex.getCause() instanceof UnsupportedMediaTypeException), (ex) -> {

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

+27-13
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,9 @@
3636
* The default implementation of an {@link OAuth2User}.
3737
*
3838
* <p>
39-
* User attribute names are <b>not</b> standardized between providers and therefore it is
40-
* required to supply the <i>key</i> for the user's &quot;name&quot; attribute to one of
41-
* the constructors. The <i>key</i> will be used for accessing the &quot;name&quot; of the
42-
* {@code Principal} (user) via {@link #getAttributes()} and returning it from
43-
* {@link #getName()}.
39+
* User attribute names are <b>not</b> standardized between providers, and therefore it is
40+
* required to supply the user's &quot;name&quot; or &quot;name&quot; attribute to one of
41+
* the constructors.
4442
*
4543
* @author Joe Grandja
4644
* @author Eddú Meléndez
@@ -56,7 +54,7 @@ public class DefaultOAuth2User implements OAuth2User, Serializable {
5654

5755
private final Map<String, Object> attributes;
5856

59-
private final String nameAttributeKey;
57+
private final String name;
6058

6159
/**
6260
* Constructs a {@code DefaultOAuth2User} using the provided parameters.
@@ -65,23 +63,32 @@ public class DefaultOAuth2User implements OAuth2User, Serializable {
6563
* @param nameAttributeKey the key used to access the user's &quot;name&quot; from
6664
* {@link #getAttributes()}
6765
*/
66+
@Deprecated
6867
public DefaultOAuth2User(Collection<? extends GrantedAuthority> authorities, Map<String, Object> attributes,
6968
String nameAttributeKey) {
70-
Assert.notEmpty(attributes, "attributes cannot be empty");
71-
Assert.hasText(nameAttributeKey, "nameAttributeKey cannot be empty");
72-
Assert.notNull(attributes.get(nameAttributeKey),
73-
"Attribute value for '" + nameAttributeKey + "' cannot be null");
69+
this(getNameFromAttributes(attributes, nameAttributeKey), attributes, authorities);
70+
}
7471

72+
/**
73+
* Constructs a {@code DefaultOAuth2User} using the provided parameters.
74+
* @param name the name of the user
75+
* @param authorities the authorities granted to the user
76+
* @param attributes the attributes about the user
77+
*/
78+
public DefaultOAuth2User(String name, Map<String, Object> attributes,
79+
Collection<? extends GrantedAuthority> authorities) {
80+
Assert.notNull(name, "name cannot be null");
81+
Assert.notEmpty(attributes, "attributes cannot be empty");
82+
this.attributes = Collections.unmodifiableMap(new LinkedHashMap<>(attributes));
7583
this.authorities = (authorities != null)
7684
? Collections.unmodifiableSet(new LinkedHashSet<>(this.sortAuthorities(authorities)))
7785
: Collections.unmodifiableSet(new LinkedHashSet<>(AuthorityUtils.NO_AUTHORITIES));
78-
this.attributes = Collections.unmodifiableMap(new LinkedHashMap<>(attributes));
79-
this.nameAttributeKey = nameAttributeKey;
86+
this.name = (name != null) ? name : (String) this.attributes.get("sub");
8087
}
8188

8289
@Override
8390
public String getName() {
84-
return this.getAttribute(this.nameAttributeKey).toString();
91+
return this.name;
8592
}
8693

8794
@Override
@@ -140,4 +147,11 @@ public String toString() {
140147
return sb.toString();
141148
}
142149

150+
private static String getNameFromAttributes(Map<String, Object> attributes, String nameAttributeKey) {
151+
Assert.hasText(nameAttributeKey, "nameAttributeKey cannot be empty");
152+
Assert.notNull(attributes.get(nameAttributeKey),
153+
"Attribute value for '" + nameAttributeKey + "' cannot be null");
154+
return attributes.get(nameAttributeKey).toString();
155+
}
156+
143157
}

test/src/main/java/org/springframework/security/test/web/reactive/server/SecurityMockServerConfigurers.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,8 @@ private Map<String, Object> defaultAttributes() {
848848
}
849849

850850
private OAuth2User defaultPrincipal() {
851-
return new DefaultOAuth2User(this.authorities.get(), this.attributes.get(), this.nameAttributeKey);
851+
String name = this.attributes.get().get(this.nameAttributeKey).toString();
852+
return new DefaultOAuth2User(name, this.attributes.get(), this.authorities.get());
852853
}
853854

854855
}

test/src/main/java/org/springframework/security/test/web/servlet/request/SecurityMockMvcRequestPostProcessors.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1390,7 +1390,8 @@ private Map<String, Object> defaultAttributes() {
13901390
}
13911391

13921392
private OAuth2User defaultPrincipal() {
1393-
return new DefaultOAuth2User(this.authorities.get(), this.attributes.get(), this.nameAttributeKey);
1393+
String name = this.attributes.get().get(this.nameAttributeKey).toString();
1394+
return new DefaultOAuth2User(name, this.attributes.get(), this.authorities.get());
13941395
}
13951396

13961397
}

0 commit comments

Comments
 (0)