|
16 | 16 | package org.springframework.security.oauth2.server.authorization.web; |
17 | 17 |
|
18 | 18 | import org.springframework.core.convert.converter.Converter; |
| 19 | +import org.springframework.http.HttpMethod; |
19 | 20 | import org.springframework.security.authentication.AuthenticationManager; |
20 | | -import org.springframework.security.core.Authentication; |
| 21 | +import org.springframework.security.oauth2.core.AuthorizationGrantType; |
| 22 | +import org.springframework.security.oauth2.core.OAuth2AuthenticationException; |
| 23 | +import org.springframework.security.oauth2.core.OAuth2Error; |
| 24 | +import org.springframework.security.oauth2.core.OAuth2ErrorCodes; |
| 25 | +import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames; |
| 26 | +import org.springframework.security.oauth2.server.authorization.OAuth2Authorization; |
21 | 27 | import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationService; |
| 28 | +import org.springframework.security.oauth2.server.authorization.TokenType; |
| 29 | +import org.springframework.security.oauth2.server.authorization.authentication.OAuth2AccessTokenAuthenticationToken; |
| 30 | +import org.springframework.security.oauth2.server.authorization.authentication.OAuth2AuthorizationCodeAuthenticationToken; |
| 31 | +import org.springframework.security.oauth2.server.authorization.authentication.OAuth2ClientAuthenticationManager; |
| 32 | +import org.springframework.security.oauth2.server.authorization.converter.OAuth2AuthorizationCodeAuthenticationTokenConverter; |
| 33 | +import org.springframework.security.web.util.matcher.AntPathRequestMatcher; |
| 34 | +import org.springframework.security.web.util.matcher.RequestMatcher; |
| 35 | +import org.springframework.util.Assert; |
| 36 | +import org.springframework.util.StringUtils; |
22 | 37 | import org.springframework.web.filter.OncePerRequestFilter; |
23 | 38 |
|
24 | 39 | import javax.servlet.FilterChain; |
|
29 | 44 |
|
30 | 45 | /** |
31 | 46 | * @author Joe Grandja |
| 47 | + * @author Madhu Bhat |
32 | 48 | */ |
33 | 49 | public class OAuth2TokenEndpointFilter extends OncePerRequestFilter { |
34 | | - private Converter<HttpServletRequest, Authentication> authorizationGrantConverter; |
| 50 | + private final String DEFAULT_TOKEN_URI = "/oauth2/token"; |
| 51 | + private Converter<HttpServletRequest, OAuth2AuthorizationCodeAuthenticationToken> authorizationGrantConverter; |
35 | 52 | private AuthenticationManager authenticationManager; |
36 | 53 | private OAuth2AuthorizationService authorizationService; |
| 54 | + private RequestMatcher uriMatcher; |
| 55 | + |
| 56 | + /** |
| 57 | + * Constructs an {@code OAuth2TokenEndpointFilter} using the provided parameter. |
| 58 | + * |
| 59 | + * @param authorizationService the authorization service implementation |
| 60 | + */ |
| 61 | + public OAuth2TokenEndpointFilter(OAuth2AuthorizationService authorizationService) { |
| 62 | + Assert.notNull(authorizationService, "authorizationService cannot be null"); |
| 63 | + this.authorizationGrantConverter = new OAuth2AuthorizationCodeAuthenticationTokenConverter(); |
| 64 | + this.authenticationManager = new OAuth2ClientAuthenticationManager(); |
| 65 | + this.uriMatcher = new AntPathRequestMatcher(DEFAULT_TOKEN_URI, HttpMethod.POST.name()); |
| 66 | + this.authorizationService = authorizationService; |
| 67 | + } |
37 | 68 |
|
38 | 69 | @Override |
39 | 70 | protected void doFilterInternal(HttpServletRequest request, |
40 | 71 | HttpServletResponse response, FilterChain filterChain) |
41 | 72 | throws ServletException, IOException { |
| 73 | + if (uriMatcher.matches(request)) { |
| 74 | + if (isValidAccessTokenRequest(request)) { |
| 75 | + OAuth2AuthorizationCodeAuthenticationToken authCodeAuthToken = authorizationGrantConverter.convert(request); |
| 76 | + OAuth2AccessTokenAuthenticationToken accessTokenAuthenticationToken = |
| 77 | + (OAuth2AccessTokenAuthenticationToken) authenticationManager.authenticate(authCodeAuthToken); |
| 78 | + OAuth2Authorization authorization = authorizationService |
| 79 | + .findByTokenAndTokenType(authCodeAuthToken.getCode(), TokenType.AUTHORIZATION_CODE); |
| 80 | + authorization.setAccessToken(accessTokenAuthenticationToken.getAccessToken()); |
| 81 | + authorizationService.save(authorization); |
| 82 | + } |
| 83 | + } |
| 84 | + filterChain.doFilter(request, response); |
| 85 | + } |
42 | 86 |
|
| 87 | + private boolean isValidAccessTokenRequest(HttpServletRequest request) { |
| 88 | + if (!AuthorizationGrantType.AUTHORIZATION_CODE.getValue().equals(request.getParameter(OAuth2ParameterNames.GRANT_TYPE))) { |
| 89 | + throw new OAuth2AuthenticationException(new OAuth2Error(OAuth2ErrorCodes.UNSUPPORTED_GRANT_TYPE)); |
| 90 | + } else if (StringUtils.isEmpty(request.getParameter(OAuth2ParameterNames.CODE)) |
| 91 | + || StringUtils.isEmpty(request.getParameter(OAuth2ParameterNames.REDIRECT_URI))) { |
| 92 | + throw new OAuth2AuthenticationException(new OAuth2Error(OAuth2ErrorCodes.INVALID_REQUEST)); |
| 93 | + } |
| 94 | + return true; |
43 | 95 | } |
44 | 96 | } |
0 commit comments