Skip to content

Commit 8aae3d6

Browse files
ahmd-nabilAhmed Nabil
authored and
Ahmed Nabil
committed
Polish DefaultOAuth2UserService
Signed-off-by: ahmd-nabil <[email protected]>
1 parent f50fc63 commit 8aae3d6

File tree

1 file changed

+37
-27
lines changed

1 file changed

+37
-27
lines changed

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

+37-27
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616

1717
package org.springframework.security.oauth2.client.userinfo;
1818

19+
import java.util.Collection;
1920
import java.util.LinkedHashSet;
2021
import java.util.Map;
21-
import java.util.Set;
2222

2323
import org.springframework.core.ParameterizedTypeReference;
2424
import org.springframework.core.convert.converter.Converter;
@@ -90,35 +90,13 @@ public DefaultOAuth2UserService() {
9090
@Override
9191
public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2AuthenticationException {
9292
Assert.notNull(userRequest, "userRequest cannot be null");
93-
if (!StringUtils
94-
.hasText(userRequest.getClientRegistration().getProviderDetails().getUserInfoEndpoint().getUri())) {
95-
OAuth2Error oauth2Error = new OAuth2Error(MISSING_USER_INFO_URI_ERROR_CODE,
96-
"Missing required UserInfo Uri in UserInfoEndpoint for Client Registration: "
97-
+ userRequest.getClientRegistration().getRegistrationId(),
98-
null);
99-
throw new OAuth2AuthenticationException(oauth2Error, oauth2Error.toString());
100-
}
101-
String userNameAttributeName = userRequest.getClientRegistration()
102-
.getProviderDetails()
103-
.getUserInfoEndpoint()
104-
.getUserNameAttributeName();
105-
if (!StringUtils.hasText(userNameAttributeName)) {
106-
OAuth2Error oauth2Error = new OAuth2Error(MISSING_USER_NAME_ATTRIBUTE_ERROR_CODE,
107-
"Missing required \"user name\" attribute name in UserInfoEndpoint for Client Registration: "
108-
+ userRequest.getClientRegistration().getRegistrationId(),
109-
null);
110-
throw new OAuth2AuthenticationException(oauth2Error, oauth2Error.toString());
111-
}
93+
String userNameAttributeName = getUserNameAttributeName(userRequest);
11294
RequestEntity<?> request = this.requestEntityConverter.convert(userRequest);
11395
ResponseEntity<Map<String, Object>> response = getResponse(userRequest, request);
114-
Map<String, Object> userAttributes = this.attributesConverter.convert(userRequest).convert(response.getBody());
115-
Set<GrantedAuthority> authorities = new LinkedHashSet<>();
116-
authorities.add(new OAuth2UserAuthority(userAttributes));
11796
OAuth2AccessToken token = userRequest.getAccessToken();
118-
for (String authority : token.getScopes()) {
119-
authorities.add(new SimpleGrantedAuthority("SCOPE_" + authority));
120-
}
121-
return new DefaultOAuth2User(authorities, userAttributes, userNameAttributeName);
97+
Map<String, Object> attributes = this.attributesConverter.convert(userRequest).convert(response.getBody());
98+
Collection<GrantedAuthority> authorities = getAuthorities(token, attributes);
99+
return new DefaultOAuth2User(authorities, attributes, userNameAttributeName);
122100
}
123101

124102
/**
@@ -186,6 +164,38 @@ private ResponseEntity<Map<String, Object>> getResponse(OAuth2UserRequest userRe
186164
}
187165
}
188166

167+
private String getUserNameAttributeName(OAuth2UserRequest userRequest) {
168+
if (!StringUtils
169+
.hasText(userRequest.getClientRegistration().getProviderDetails().getUserInfoEndpoint().getUri())) {
170+
OAuth2Error oauth2Error = new OAuth2Error(MISSING_USER_INFO_URI_ERROR_CODE,
171+
"Missing required UserInfo Uri in UserInfoEndpoint for Client Registration: "
172+
+ userRequest.getClientRegistration().getRegistrationId(),
173+
null);
174+
throw new OAuth2AuthenticationException(oauth2Error, oauth2Error.toString());
175+
}
176+
String userNameAttributeName = userRequest.getClientRegistration()
177+
.getProviderDetails()
178+
.getUserInfoEndpoint()
179+
.getUserNameAttributeName();
180+
if (!StringUtils.hasText(userNameAttributeName)) {
181+
OAuth2Error oauth2Error = new OAuth2Error(MISSING_USER_NAME_ATTRIBUTE_ERROR_CODE,
182+
"Missing required \"user name\" attribute name in UserInfoEndpoint for Client Registration: "
183+
+ userRequest.getClientRegistration().getRegistrationId(),
184+
null);
185+
throw new OAuth2AuthenticationException(oauth2Error, oauth2Error.toString());
186+
}
187+
return userNameAttributeName;
188+
}
189+
190+
private Collection<GrantedAuthority> getAuthorities(OAuth2AccessToken token, Map<String, Object> attributes) {
191+
Collection<GrantedAuthority> authorities = new LinkedHashSet<>();
192+
authorities.add(new OAuth2UserAuthority(attributes));
193+
for (String authority : token.getScopes()) {
194+
authorities.add(new SimpleGrantedAuthority("SCOPE_" + authority));
195+
}
196+
return authorities;
197+
}
198+
189199
/**
190200
* Sets the {@link Converter} used for converting the {@link OAuth2UserRequest} to a
191201
* {@link RequestEntity} representation of the UserInfo Request.

0 commit comments

Comments
 (0)