|
@@ -18,17 +18,16 @@ package org.springframework.security.oauth2.client.user;
|
|
|
import org.springframework.security.core.GrantedAuthority;
|
|
|
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationException;
|
|
|
import org.springframework.security.oauth2.client.authentication.OAuth2ClientAuthenticationToken;
|
|
|
+import org.springframework.security.oauth2.client.registration.ClientRegistration;
|
|
|
import org.springframework.security.oauth2.client.user.nimbus.NimbusUserInfoRetriever;
|
|
|
import org.springframework.security.oauth2.core.user.DefaultOAuth2User;
|
|
|
import org.springframework.security.oauth2.core.user.OAuth2User;
|
|
|
import org.springframework.security.oauth2.core.user.OAuth2UserAuthority;
|
|
|
import org.springframework.security.oauth2.oidc.client.authentication.OidcClientAuthenticationToken;
|
|
|
import org.springframework.util.Assert;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
|
|
|
-import java.net.URI;
|
|
|
-import java.util.Collections;
|
|
|
import java.util.HashSet;
|
|
|
-import java.util.LinkedHashMap;
|
|
|
import java.util.Map;
|
|
|
import java.util.Set;
|
|
|
|
|
@@ -36,8 +35,8 @@ import java.util.Set;
|
|
|
* An implementation of an {@link OAuth2UserService} that supports standard <i>OAuth 2.0 Provider's</i>.
|
|
|
* <p>
|
|
|
* For standard <i>OAuth 2.0 Provider's</i>, the attribute name (from the <i>UserInfo Response</i>)
|
|
|
- * for the <i>"user's name"</i> is required. This is supplied via the constructor,
|
|
|
- * mapped by <code>URI</code>, which represents the <i>UserInfo Endpoint</i> address.
|
|
|
+ * for the <i>"user's name"</i> is required and therefore must be supplied via
|
|
|
+ * {@link ClientRegistration.ProviderDetails.UserInfoEndpoint#getUserNameAttributeName()}.
|
|
|
* <p>
|
|
|
* <b>NOTE:</b> Attribute names are <b><i>not</i></b> standardized between providers and therefore will vary.
|
|
|
* Please consult the provider's API documentation for the set of supported user attribute names.
|
|
@@ -52,12 +51,9 @@ import java.util.Set;
|
|
|
* @see UserInfoRetriever
|
|
|
*/
|
|
|
public class DefaultOAuth2UserService implements OAuth2UserService {
|
|
|
- private final Map<URI, String> userNameAttributeNames;
|
|
|
private UserInfoRetriever userInfoRetriever = new NimbusUserInfoRetriever();
|
|
|
|
|
|
- public DefaultOAuth2UserService(Map<URI, String> userNameAttributeNames) {
|
|
|
- Assert.notEmpty(userNameAttributeNames, "userNameAttributeNames cannot be empty");
|
|
|
- this.userNameAttributeNames = Collections.unmodifiableMap(new LinkedHashMap<>(userNameAttributeNames));
|
|
|
+ public DefaultOAuth2UserService() {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -66,12 +62,12 @@ public class DefaultOAuth2UserService implements OAuth2UserService {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
- URI userInfoUri = URI.create(clientAuthentication.getClientRegistration().getProviderDetails().getUserInfoEndpoint().getUri());
|
|
|
- if (!this.getUserNameAttributeNames().containsKey(userInfoUri)) {
|
|
|
+ String userNameAttributeName = clientAuthentication.getClientRegistration().getProviderDetails().getUserInfoEndpoint().getUserNameAttributeName();
|
|
|
+ if (!StringUtils.hasText(userNameAttributeName)) {
|
|
|
throw new IllegalArgumentException(
|
|
|
- "Missing required \"user name\" attribute name for UserInfo Endpoint: " + userInfoUri.toString());
|
|
|
+ "Missing required \"user name\" attribute name in UserInfoEndpoint for Client Registration: " +
|
|
|
+ clientAuthentication.getClientRegistration().getRegistrationId());
|
|
|
}
|
|
|
- String userNameAttributeName = this.getUserNameAttributeNames().get(userInfoUri);
|
|
|
|
|
|
Map<String, Object> userAttributes = this.getUserInfoRetriever().retrieve(clientAuthentication);
|
|
|
GrantedAuthority authority = new OAuth2UserAuthority(userAttributes);
|
|
@@ -81,10 +77,6 @@ public class DefaultOAuth2UserService implements OAuth2UserService {
|
|
|
return new DefaultOAuth2User(authorities, userAttributes, userNameAttributeName);
|
|
|
}
|
|
|
|
|
|
- protected Map<URI, String> getUserNameAttributeNames() {
|
|
|
- return this.userNameAttributeNames;
|
|
|
- }
|
|
|
-
|
|
|
protected UserInfoRetriever getUserInfoRetriever() {
|
|
|
return this.userInfoRetriever;
|
|
|
}
|