Skip to content

Commit ced9228

Browse files
committed
Add builders for DefaultOAuth2User and DefaultOidcUser
1 parent 8528283 commit ced9228

File tree

8 files changed

+183
-50
lines changed

8 files changed

+183
-50
lines changed

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

+8-4
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.springframework.security.oauth2.core.oidc.user.DefaultOidcUser;
3030
import org.springframework.security.oauth2.core.oidc.user.OidcUser;
3131
import org.springframework.security.oauth2.core.oidc.user.OidcUserAuthority;
32+
import org.springframework.security.oauth2.core.user.DefaultOAuth2User;
3233
import org.springframework.util.CollectionUtils;
3334
import org.springframework.util.StringUtils;
3435

@@ -91,12 +92,15 @@ static OidcUser getUser(OidcUserRequest userRequest, OidcUserInfo userInfo) {
9192
for (String scope : token.getScopes()) {
9293
authorities.add(new SimpleGrantedAuthority("SCOPE_" + scope));
9394
}
95+
DefaultOidcUser.Builder userBuilder = new DefaultOidcUser.Builder();
9496
if (StringUtils.hasText(userNameAttributeName)) {
95-
Map<String, Object> attributes = OidcUserAuthority.collectClaims(userRequest.getIdToken(), userInfo);
96-
String name = (String) attributes.get(userNameAttributeName);
97-
return new DefaultOidcUser(name, userRequest.getIdToken(), userInfo, authorities);
97+
userBuilder.nameAttributeKey(userNameAttributeName);
9898
}
99-
return new DefaultOidcUser(userRequest.getIdToken(), userInfo, authorities);
99+
return userBuilder
100+
.idToken(userRequest.getIdToken())
101+
.userInfo(userInfo)
102+
.authorities(authorities)
103+
.build();
100104
}
101105

102106
private OidcUserRequestUtils() {

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,11 @@ 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(attributes.get(userNameAttributeName).toString(), attributes, authorities);
99+
return new DefaultOAuth2User.Builder()
100+
.nameAttributeKey(userNameAttributeName)
101+
.attributes(attributes)
102+
.authorities(authorities)
103+
.build();
100104
}
101105

102106
/**

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

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

141-
return new DefaultOAuth2User(attrs.get(userNameAttributeName).toString(), attrs, authorities);
141+
return new DefaultOAuth2User.Builder()
142+
.nameAttributeKey(userNameAttributeName)
143+
.attributes(attrs)
144+
.authorities(authorities)
145+
.build();
142146
})
143147
.onErrorMap((ex) -> (ex instanceof UnsupportedMediaTypeException ||
144148
ex.getCause() instanceof UnsupportedMediaTypeException), (ex) -> {

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

+78-31
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,6 @@ public DefaultOidcUser(Collection<? extends GrantedAuthority> authorities, OidcI
5656
this(authorities, idToken, IdTokenClaimNames.SUB);
5757
}
5858

59-
/**
60-
* Constructs a {@code DefaultOidcUser} using the provided parameters.
61-
* @param idToken the {@link OidcIdToken ID Token} containing claims about the user
62-
* @param authorities the authorities granted to the user
63-
*/
64-
public DefaultOidcUser(OidcIdToken idToken, Collection<? extends GrantedAuthority> authorities) {
65-
this(null, idToken, authorities);
66-
}
67-
6859
/**
6960
* Constructs a {@code DefaultOidcUser} using the provided parameters.
7061
* @param authorities the authorities granted to the user
@@ -78,16 +69,6 @@ public DefaultOidcUser(Collection<? extends GrantedAuthority> authorities, OidcI
7869
this(authorities, idToken, null, nameAttributeKey);
7970
}
8071

81-
/**
82-
* Constructs a {@code DefaultOidcUser} using the provided parameters.
83-
* @param name the name of the user
84-
* @param idToken the {@link OidcIdToken ID Token} containing claims about the user
85-
* @param authorities the authorities granted to the user
86-
*/
87-
public DefaultOidcUser(String name, OidcIdToken idToken, Collection<? extends GrantedAuthority> authorities) {
88-
this(name, idToken, null, authorities);
89-
}
90-
9172
/**
9273
* Constructs a {@code DefaultOidcUser} using the provided parameters.
9374
* @param authorities the authorities granted to the user
@@ -101,18 +82,6 @@ public DefaultOidcUser(Collection<? extends GrantedAuthority> authorities, OidcI
10182
this(authorities, idToken, userInfo, IdTokenClaimNames.SUB);
10283
}
10384

104-
/**
105-
* Constructs a {@code DefaultOidcUser} using the provided parameters.
106-
* @param authorities the authorities granted to the user
107-
* @param idToken the {@link OidcIdToken ID Token} containing claims about the user
108-
* @param userInfo the {@link OidcUserInfo UserInfo} containing claims about the user,
109-
* may be {@code null}
110-
*/
111-
public DefaultOidcUser(OidcIdToken idToken, OidcUserInfo userInfo,
112-
Collection<? extends GrantedAuthority> authorities) {
113-
this(null, idToken, userInfo, authorities);
114-
}
115-
11685
/**
11786
* Constructs a {@code DefaultOidcUser} using the provided parameters.
11887
* @param authorities the authorities granted to the user
@@ -160,4 +129,82 @@ public OidcUserInfo getUserInfo() {
160129
return this.userInfo;
161130
}
162131

132+
public static class Builder {
133+
134+
private String name;
135+
136+
private String nameAttributeKey;
137+
138+
private OidcIdToken idToken;
139+
140+
private OidcUserInfo userInfo;
141+
142+
private Collection<? extends GrantedAuthority> authorities;
143+
144+
/**
145+
* Sets the name of the user.
146+
* @param name the name of the user
147+
* @return the {@link Builder}
148+
*/
149+
public Builder name(String name) {
150+
this.name = name;
151+
return this;
152+
}
153+
154+
/**
155+
* Sets the key used to access the user's &quot;name&quot; from the user attributes if no &quot;name&quot; is
156+
* provided.
157+
* @param nameAttributeKey the key used to access the user's &quot;name&quot; from the user attributes.
158+
* @return the {@link Builder}
159+
*/
160+
public Builder nameAttributeKey(String nameAttributeKey) {
161+
this.nameAttributeKey = nameAttributeKey;
162+
return this;
163+
}
164+
165+
/**
166+
* Sets the {@link OidcIdToken ID Token} containing claims about the user.
167+
* @param idToken the {@link OidcIdToken ID Token} containing claims about the user.
168+
* @return the {@link Builder}
169+
*/
170+
public Builder idToken(OidcIdToken idToken) {
171+
this.idToken = idToken;
172+
return this;
173+
}
174+
175+
/**
176+
* Sets the {@link OidcUserInfo UserInfo} containing claims about the user.
177+
* @param userInfo the {@link OidcUserInfo UserInfo} containing claims about the user.
178+
* @return the {@link Builder}
179+
*/
180+
public Builder userInfo(OidcUserInfo userInfo) {
181+
this.userInfo = userInfo;
182+
return this;
183+
}
184+
185+
/**
186+
* Sets the authorities granted to the user.
187+
* @param authorities the authorities granted to the user
188+
* @return the {@link Builder}
189+
*/
190+
public Builder authorities(Collection<? extends GrantedAuthority> authorities) {
191+
this.authorities = authorities;
192+
return this;
193+
}
194+
195+
/**
196+
* Builds a new {@link DefaultOidcUser}.
197+
* @return a {@link DefaultOidcUser}
198+
*/
199+
public DefaultOidcUser build() {
200+
String name = this.name;
201+
if (name == null) {
202+
Map<String, Object> attributes = OidcUserAuthority.collectClaims(this.idToken, userInfo);
203+
name = getNameFromAttributes(attributes, this.nameAttributeKey);
204+
}
205+
return new DefaultOidcUser(name, idToken, userInfo, authorities);
206+
}
207+
208+
}
209+
163210
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public int hashCode() {
145145
return result;
146146
}
147147

148-
public static Map<String, Object> collectClaims(OidcIdToken idToken, OidcUserInfo userInfo) {
148+
static Map<String, Object> collectClaims(OidcIdToken idToken, OidcUserInfo userInfo) {
149149
Assert.notNull(idToken, "idToken cannot be null");
150150
Map<String, Object> claims = new HashMap<>();
151151
if (userInfo != null) {

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

+66-6
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,6 @@
3535
/**
3636
* The default implementation of an {@link OAuth2User}.
3737
*
38-
* <p>
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.
42-
*
4338
* @author Joe Grandja
4439
* @author Eddú Meléndez
4540
* @author Park Hyojong
@@ -146,11 +141,76 @@ public String toString() {
146141
return sb.toString();
147142
}
148143

149-
private static String getNameFromAttributes(Map<String, Object> attributes, String nameAttributeKey) {
144+
protected static String getNameFromAttributes(Map<String, Object> attributes, String nameAttributeKey) {
150145
Assert.hasText(nameAttributeKey, "nameAttributeKey cannot be empty");
151146
Assert.notNull(attributes.get(nameAttributeKey),
152147
"Attribute value for '" + nameAttributeKey + "' cannot be null");
153148
return attributes.get(nameAttributeKey).toString();
154149
}
155150

151+
/**
152+
* A builder for {@link DefaultOAuth2User}.
153+
*/
154+
public static class Builder {
155+
156+
private String name;
157+
158+
private String nameAttributeKey;
159+
160+
private Map<String, Object> attributes;
161+
162+
private Collection<? extends GrantedAuthority> authorities;
163+
164+
/**
165+
* Sets the name of the user.
166+
* @param name the name of the user
167+
* @return the {@link Builder}
168+
*/
169+
public Builder name(String name) {
170+
this.name = name;
171+
return this;
172+
}
173+
174+
/**
175+
* Sets the key used to access the user's &quot;name&quot; from the user attributes if no &quot;name&quot; is
176+
* provided.
177+
* @param nameAttributeKey the key used to access the user's &quot;name&quot; from the user attributes.
178+
* @return the {@link Builder}
179+
*/
180+
public Builder nameAttributeKey(String nameAttributeKey) {
181+
this.nameAttributeKey = nameAttributeKey;
182+
return this;
183+
}
184+
185+
/**
186+
* Sets the attributes about the user.
187+
* @param attributes the attributes about the user
188+
* @return the {@link Builder}
189+
*/
190+
public Builder attributes(Map<String, Object> attributes) {
191+
this.attributes = attributes;
192+
return this;
193+
}
194+
195+
/**
196+
* Sets the authorities granted to the user.
197+
* @param authorities the authorities granted to the user
198+
* @return the {@link Builder}
199+
*/
200+
public Builder authorities(Collection<? extends GrantedAuthority> authorities) {
201+
this.authorities = authorities;
202+
return this;
203+
}
204+
205+
/**
206+
* Builds a new {@link DefaultOAuth2User}.
207+
* @return a {@link DefaultOAuth2User}
208+
*/
209+
public DefaultOAuth2User build() {
210+
String name = this.name != null ? this.name : getNameFromAttributes(this.attributes, this.nameAttributeKey);
211+
return new DefaultOAuth2User(name, this.attributes, this.authorities);
212+
}
213+
214+
}
215+
156216
}

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

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

850850
private OAuth2User defaultPrincipal() {
851-
String name = this.attributes.get().get(this.nameAttributeKey).toString();
852-
return new DefaultOAuth2User(name, this.attributes.get(), this.authorities.get());
851+
return new DefaultOAuth2User.Builder()
852+
.nameAttributeKey(this.nameAttributeKey)
853+
.attributes(this.attributes.get())
854+
.authorities(this.authorities.get())
855+
.build();
853856
}
854857

855858
}
@@ -1024,7 +1027,11 @@ private OidcUserInfo getOidcUserInfo() {
10241027
}
10251028

10261029
private OidcUser defaultPrincipal() {
1027-
return new DefaultOidcUser(getOidcIdToken(), this.userInfo, getAuthorities());
1030+
return new DefaultOidcUser.Builder()
1031+
.idToken(getOidcIdToken())
1032+
.userInfo(this.userInfo)
1033+
.authorities(getAuthorities())
1034+
.build();
10281035
}
10291036

10301037
}

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

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

13921392
private OAuth2User defaultPrincipal() {
1393-
String name = this.attributes.get().get(this.nameAttributeKey).toString();
1394-
return new DefaultOAuth2User(name, this.attributes.get(), this.authorities.get());
1393+
return new DefaultOAuth2User.Builder()
1394+
.nameAttributeKey(this.nameAttributeKey)
1395+
.attributes(this.attributes.get())
1396+
.authorities(this.authorities.get())
1397+
.build();
13951398
}
13961399

13971400
}
@@ -1534,7 +1537,11 @@ private OidcUserInfo getOidcUserInfo() {
15341537
}
15351538

15361539
private OidcUser defaultPrincipal() {
1537-
return new DefaultOidcUser(getOidcIdToken(), this.userInfo, getAuthorities());
1540+
return new DefaultOidcUser.Builder()
1541+
.idToken(getOidcIdToken())
1542+
.userInfo(this.userInfo)
1543+
.authorities(getAuthorities())
1544+
.build();
15381545
}
15391546

15401547
}

0 commit comments

Comments
 (0)