|
@@ -35,6 +35,8 @@ import org.springframework.security.oauth2.core.http.converter.OAuth2ErrorHttpMe
|
|
|
import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationService;
|
|
|
import org.springframework.security.oauth2.server.authorization.authentication.OAuth2AccessTokenAuthenticationToken;
|
|
|
import org.springframework.security.oauth2.server.authorization.authentication.OAuth2AuthorizationCodeAuthenticationToken;
|
|
|
+import org.springframework.security.oauth2.server.authorization.authentication.OAuth2ClientAuthenticationToken;
|
|
|
+import org.springframework.security.oauth2.server.authorization.authentication.OAuth2ClientCredentialsAuthenticationToken;
|
|
|
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
|
|
|
import org.springframework.security.web.util.matcher.RequestMatcher;
|
|
|
import org.springframework.util.Assert;
|
|
@@ -48,6 +50,11 @@ import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import java.io.IOException;
|
|
|
import java.time.temporal.ChronoUnit;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.HashSet;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Set;
|
|
|
|
|
|
/**
|
|
|
* A {@code Filter} for the OAuth 2.0 Authorization Code Grant,
|
|
@@ -86,8 +93,8 @@ public class OAuth2TokenEndpointFilter extends OncePerRequestFilter {
|
|
|
private final AuthenticationManager authenticationManager;
|
|
|
private final OAuth2AuthorizationService authorizationService;
|
|
|
private final RequestMatcher tokenEndpointMatcher;
|
|
|
- private final Converter<HttpServletRequest, Authentication> authorizationGrantAuthenticationConverter =
|
|
|
- new AuthorizationCodeAuthenticationConverter();
|
|
|
+ private final Converter<HttpServletRequest, Authentication> authorizationGrantAuthenticationConverter;
|
|
|
+
|
|
|
private final HttpMessageConverter<OAuth2AccessTokenResponse> accessTokenHttpResponseConverter =
|
|
|
new OAuth2AccessTokenResponseHttpMessageConverter();
|
|
|
private final HttpMessageConverter<OAuth2Error> errorHttpResponseConverter =
|
|
@@ -119,6 +126,11 @@ public class OAuth2TokenEndpointFilter extends OncePerRequestFilter {
|
|
|
this.authenticationManager = authenticationManager;
|
|
|
this.authorizationService = authorizationService;
|
|
|
this.tokenEndpointMatcher = new AntPathRequestMatcher(tokenEndpointUri, HttpMethod.POST.name());
|
|
|
+
|
|
|
+ Map<AuthorizationGrantType, Converter<HttpServletRequest, Authentication>> converters = new HashMap<>();
|
|
|
+ converters.put(AuthorizationGrantType.AUTHORIZATION_CODE, new AuthorizationCodeAuthenticationConverter());
|
|
|
+ converters.put(AuthorizationGrantType.CLIENT_CREDENTIALS, new ClientCredentialsAuthenticationConverter());
|
|
|
+ this.authorizationGrantAuthenticationConverter = new DelegatingAuthorizationGrantAuthenticationConverter(converters);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -131,8 +143,16 @@ public class OAuth2TokenEndpointFilter extends OncePerRequestFilter {
|
|
|
}
|
|
|
|
|
|
try {
|
|
|
- Authentication authorizationGrantAuthentication =
|
|
|
- this.authorizationGrantAuthenticationConverter.convert(request);
|
|
|
+ String[] grantTypes = request.getParameterValues(OAuth2ParameterNames.GRANT_TYPE);
|
|
|
+ if (grantTypes == null || grantTypes.length == 0) {
|
|
|
+ throwError(OAuth2ErrorCodes.INVALID_REQUEST, "grant_type");
|
|
|
+ }
|
|
|
+
|
|
|
+ Authentication authorizationGrantAuthentication = this.authorizationGrantAuthenticationConverter.convert(request);
|
|
|
+ if (authorizationGrantAuthentication == null) {
|
|
|
+ throwError(OAuth2ErrorCodes.UNSUPPORTED_GRANT_TYPE, "grant_type");
|
|
|
+ }
|
|
|
+
|
|
|
OAuth2AccessTokenAuthenticationToken accessTokenAuthentication =
|
|
|
(OAuth2AccessTokenAuthenticationToken) this.authenticationManager.authenticate(authorizationGrantAuthentication);
|
|
|
sendAccessTokenResponse(response, accessTokenAuthentication.getAccessToken());
|
|
@@ -161,7 +181,7 @@ public class OAuth2TokenEndpointFilter extends OncePerRequestFilter {
|
|
|
this.errorHttpResponseConverter.write(error, null, httpResponse);
|
|
|
}
|
|
|
|
|
|
- private static OAuth2AuthenticationException throwError(String errorCode, String parameterName) {
|
|
|
+ private static void throwError(String errorCode, String parameterName) {
|
|
|
OAuth2Error error = new OAuth2Error(errorCode, "OAuth 2.0 Parameter: " + parameterName,
|
|
|
"https://tools.ietf.org/html/rfc6749#section-5.2");
|
|
|
throw new OAuth2AuthenticationException(error);
|
|
@@ -214,4 +234,29 @@ public class OAuth2TokenEndpointFilter extends OncePerRequestFilter {
|
|
|
new OAuth2AuthorizationCodeAuthenticationToken(code, clientId, redirectUri);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ private static class ClientCredentialsAuthenticationConverter implements Converter<HttpServletRequest, Authentication> {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Authentication convert(HttpServletRequest request) {
|
|
|
+ final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
|
|
+ final OAuth2ClientAuthenticationToken clientAuthenticationToken = (OAuth2ClientAuthenticationToken) authentication;
|
|
|
+
|
|
|
+ // grant_type (REQUIRED)
|
|
|
+ String grantType = request.getParameter(OAuth2ParameterNames.GRANT_TYPE);
|
|
|
+ if (!AuthorizationGrantType.CLIENT_CREDENTIALS.getValue().equals(grantType)) {
|
|
|
+ throwError(OAuth2ErrorCodes.UNSUPPORTED_GRANT_TYPE, OAuth2ParameterNames.GRANT_TYPE);
|
|
|
+ }
|
|
|
+
|
|
|
+ // scope (OPTIONAL)
|
|
|
+ // https://tools.ietf.org/html/rfc6749#section-4.4.2
|
|
|
+ String scopeParameter = request.getParameter(OAuth2ParameterNames.SCOPE);
|
|
|
+ if (StringUtils.isEmpty(scopeParameter)) {
|
|
|
+ return new OAuth2ClientCredentialsAuthenticationToken(clientAuthenticationToken);
|
|
|
+ }
|
|
|
+
|
|
|
+ Set<String> requestedScopes = new HashSet<>(Arrays.asList(StringUtils.delimitedListToStringArray(scopeParameter, " ")));
|
|
|
+ return new OAuth2ClientCredentialsAuthenticationToken(clientAuthenticationToken, requestedScopes);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|