|
| 1 | +/* |
| 2 | + * Copyright 2020 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.security.oauth2.server.authorization.web; |
| 18 | + |
| 19 | +import javax.servlet.http.HttpServletRequest; |
| 20 | + |
| 21 | +import org.springframework.core.convert.converter.Converter; |
| 22 | +import org.springframework.security.core.Authentication; |
| 23 | +import org.springframework.security.core.context.SecurityContextHolder; |
| 24 | +import org.springframework.security.oauth2.core.AuthorizationGrantType; |
| 25 | +import org.springframework.security.oauth2.core.OAuth2AuthenticationException; |
| 26 | +import org.springframework.security.oauth2.core.OAuth2Error; |
| 27 | +import org.springframework.security.oauth2.core.OAuth2ErrorCodes; |
| 28 | +import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames; |
| 29 | +import org.springframework.security.oauth2.server.authorization.authentication.OAuth2AuthorizationCodeAuthenticationToken; |
| 30 | +import org.springframework.util.MultiValueMap; |
| 31 | +import org.springframework.util.StringUtils; |
| 32 | + |
| 33 | +class AuthorizationCodeAuthenticationConverter implements Converter<HttpServletRequest, Authentication> { |
| 34 | + |
| 35 | + @Override |
| 36 | + public Authentication convert(HttpServletRequest request) { |
| 37 | + MultiValueMap<String, String> parameters = OAuth2EndpointUtils.getParameters(request); |
| 38 | + |
| 39 | + // grant_type (REQUIRED) |
| 40 | + String grantType = parameters.getFirst(OAuth2ParameterNames.GRANT_TYPE); |
| 41 | + if (!StringUtils.hasText(grantType) || |
| 42 | + parameters.get(OAuth2ParameterNames.GRANT_TYPE).size() != 1) { |
| 43 | + throwError(OAuth2ErrorCodes.INVALID_REQUEST, OAuth2ParameterNames.GRANT_TYPE); |
| 44 | + } |
| 45 | + if (!AuthorizationGrantType.AUTHORIZATION_CODE.getValue().equals(grantType)) { |
| 46 | + throwError(OAuth2ErrorCodes.UNSUPPORTED_GRANT_TYPE, OAuth2ParameterNames.GRANT_TYPE); |
| 47 | + } |
| 48 | + |
| 49 | + // client_id (REQUIRED) |
| 50 | + String clientId = parameters.getFirst(OAuth2ParameterNames.CLIENT_ID); |
| 51 | + Authentication clientPrincipal = null; |
| 52 | + if (StringUtils.hasText(clientId)) { |
| 53 | + if (parameters.get(OAuth2ParameterNames.CLIENT_ID).size() != 1) { |
| 54 | + throwError(OAuth2ErrorCodes.INVALID_REQUEST, OAuth2ParameterNames.CLIENT_ID); |
| 55 | + } |
| 56 | + } else { |
| 57 | + clientPrincipal = SecurityContextHolder.getContext().getAuthentication(); |
| 58 | + } |
| 59 | + |
| 60 | + // code (REQUIRED) |
| 61 | + String code = parameters.getFirst(OAuth2ParameterNames.CODE); |
| 62 | + if (!StringUtils.hasText(code) || |
| 63 | + parameters.get(OAuth2ParameterNames.CODE).size() != 1) { |
| 64 | + throwError(OAuth2ErrorCodes.INVALID_REQUEST, OAuth2ParameterNames.CODE); |
| 65 | + } |
| 66 | + |
| 67 | + // redirect_uri (REQUIRED) |
| 68 | + // Required only if the "redirect_uri" parameter was included in the authorization request |
| 69 | + String redirectUri = parameters.getFirst(OAuth2ParameterNames.REDIRECT_URI); |
| 70 | + if (StringUtils.hasText(redirectUri) && |
| 71 | + parameters.get(OAuth2ParameterNames.REDIRECT_URI).size() != 1) { |
| 72 | + throwError(OAuth2ErrorCodes.INVALID_REQUEST, OAuth2ParameterNames.REDIRECT_URI); |
| 73 | + } |
| 74 | + |
| 75 | + return clientPrincipal != null ? |
| 76 | + new OAuth2AuthorizationCodeAuthenticationToken(code, clientPrincipal, redirectUri) : |
| 77 | + new OAuth2AuthorizationCodeAuthenticationToken(code, clientId, redirectUri); |
| 78 | + } |
| 79 | + |
| 80 | + private void throwError(String errorCode, String parameterName) { |
| 81 | + OAuth2Error error = new OAuth2Error(errorCode, "OAuth 2.0 Parameter: " + parameterName, |
| 82 | + "https://tools.ietf.org/html/rfc6749#section-5.2"); |
| 83 | + throw new OAuth2AuthenticationException(error); |
| 84 | + } |
| 85 | +} |
0 commit comments