|
| 1 | +/* |
| 2 | + * Copyright 2002-2024 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.resource.authentication; |
| 18 | + |
| 19 | +import java.util.regex.Matcher; |
| 20 | +import java.util.regex.Pattern; |
| 21 | + |
| 22 | +import jakarta.servlet.http.HttpServletRequest; |
| 23 | + |
| 24 | +import org.springframework.http.HttpHeaders; |
| 25 | +import org.springframework.http.HttpMethod; |
| 26 | +import org.springframework.http.MediaType; |
| 27 | +import org.springframework.security.authentication.AuthenticationDetailsSource; |
| 28 | +import org.springframework.security.core.Authentication; |
| 29 | +import org.springframework.security.oauth2.core.OAuth2AuthenticationException; |
| 30 | +import org.springframework.security.oauth2.server.resource.BearerTokenError; |
| 31 | +import org.springframework.security.oauth2.server.resource.BearerTokenErrors; |
| 32 | +import org.springframework.security.web.authentication.AuthenticationConverter; |
| 33 | +import org.springframework.security.web.authentication.WebAuthenticationDetailsSource; |
| 34 | +import org.springframework.util.Assert; |
| 35 | +import org.springframework.util.StringUtils; |
| 36 | + |
| 37 | +/** |
| 38 | + * Implementation of {@link AuthenticationConverter}, that converts bearer token to |
| 39 | + * {@link BearerTokenAuthenticationToken} |
| 40 | + * |
| 41 | + * @author Max Batischev |
| 42 | + * @since 6.3 |
| 43 | + */ |
| 44 | +public final class BearerTokenAuthenticationConverter implements AuthenticationConverter { |
| 45 | + |
| 46 | + private AuthenticationDetailsSource<HttpServletRequest, ?> authenticationDetailsSource = new WebAuthenticationDetailsSource(); |
| 47 | + |
| 48 | + private static final Pattern authorizationPattern = Pattern.compile("^Bearer (?<token>[a-zA-Z0-9-._~+/]+=*)$", |
| 49 | + Pattern.CASE_INSENSITIVE); |
| 50 | + |
| 51 | + private static final String ACCESS_TOKEN_PARAMETER_NAME = "access_token"; |
| 52 | + |
| 53 | + private boolean allowFormEncodedBodyParameter = false; |
| 54 | + |
| 55 | + private boolean allowUriQueryParameter = false; |
| 56 | + |
| 57 | + private String bearerTokenHeaderName = HttpHeaders.AUTHORIZATION; |
| 58 | + |
| 59 | + @Override |
| 60 | + public Authentication convert(HttpServletRequest request) { |
| 61 | + String token = token(request); |
| 62 | + if (StringUtils.hasText(token)) { |
| 63 | + BearerTokenAuthenticationToken authenticationToken = new BearerTokenAuthenticationToken(token); |
| 64 | + authenticationToken.setDetails(this.authenticationDetailsSource.buildDetails(request)); |
| 65 | + |
| 66 | + return authenticationToken; |
| 67 | + } |
| 68 | + return null; |
| 69 | + } |
| 70 | + |
| 71 | + private String token(HttpServletRequest request) { |
| 72 | + final String authorizationHeaderToken = resolveFromAuthorizationHeader(request); |
| 73 | + final String parameterToken = isParameterTokenSupportedForRequest(request) |
| 74 | + ? resolveFromRequestParameters(request) : null; |
| 75 | + if (authorizationHeaderToken != null) { |
| 76 | + if (parameterToken != null) { |
| 77 | + final BearerTokenError error = BearerTokenErrors |
| 78 | + .invalidRequest("Found multiple bearer tokens in the request"); |
| 79 | + throw new OAuth2AuthenticationException(error); |
| 80 | + } |
| 81 | + return authorizationHeaderToken; |
| 82 | + } |
| 83 | + if (parameterToken != null && isParameterTokenEnabledForRequest(request)) { |
| 84 | + return parameterToken; |
| 85 | + } |
| 86 | + return null; |
| 87 | + } |
| 88 | + |
| 89 | + private String resolveFromAuthorizationHeader(HttpServletRequest request) { |
| 90 | + String authorization = request.getHeader(this.bearerTokenHeaderName); |
| 91 | + if (!StringUtils.startsWithIgnoreCase(authorization, "bearer")) { |
| 92 | + return null; |
| 93 | + } |
| 94 | + Matcher matcher = authorizationPattern.matcher(authorization); |
| 95 | + if (!matcher.matches()) { |
| 96 | + BearerTokenError error = BearerTokenErrors.invalidToken("Bearer token is malformed"); |
| 97 | + throw new OAuth2AuthenticationException(error); |
| 98 | + } |
| 99 | + return matcher.group("token"); |
| 100 | + } |
| 101 | + |
| 102 | + private boolean isParameterTokenEnabledForRequest(HttpServletRequest request) { |
| 103 | + return ((this.allowFormEncodedBodyParameter && isFormEncodedRequest(request) && !isGetRequest(request) |
| 104 | + && !hasAccessTokenInQueryString(request)) || (this.allowUriQueryParameter && isGetRequest(request))); |
| 105 | + } |
| 106 | + |
| 107 | + private static String resolveFromRequestParameters(HttpServletRequest request) { |
| 108 | + String[] values = request.getParameterValues(ACCESS_TOKEN_PARAMETER_NAME); |
| 109 | + if (values == null || values.length == 0) { |
| 110 | + return null; |
| 111 | + } |
| 112 | + if (values.length == 1) { |
| 113 | + return values[0]; |
| 114 | + } |
| 115 | + BearerTokenError error = BearerTokenErrors.invalidRequest("Found multiple bearer tokens in the request"); |
| 116 | + throw new OAuth2AuthenticationException(error); |
| 117 | + } |
| 118 | + |
| 119 | + private boolean isParameterTokenSupportedForRequest(final HttpServletRequest request) { |
| 120 | + return isFormEncodedRequest(request) || isGetRequest(request); |
| 121 | + } |
| 122 | + |
| 123 | + private boolean isGetRequest(HttpServletRequest request) { |
| 124 | + return HttpMethod.GET.name().equals(request.getMethod()); |
| 125 | + } |
| 126 | + |
| 127 | + private boolean isFormEncodedRequest(HttpServletRequest request) { |
| 128 | + return MediaType.APPLICATION_FORM_URLENCODED_VALUE.equals(request.getContentType()); |
| 129 | + } |
| 130 | + |
| 131 | + private static boolean hasAccessTokenInQueryString(HttpServletRequest request) { |
| 132 | + return (request.getQueryString() != null) && request.getQueryString().contains(ACCESS_TOKEN_PARAMETER_NAME); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Set if transport of access token using URI query parameter is supported. Defaults |
| 137 | + * to {@code false}. |
| 138 | + * |
| 139 | + * The spec recommends against using this mechanism for sending bearer tokens, and |
| 140 | + * even goes as far as stating that it was only included for completeness. |
| 141 | + * @param allowUriQueryParameter if the URI query parameter is supported |
| 142 | + */ |
| 143 | + public void setAllowUriQueryParameter(boolean allowUriQueryParameter) { |
| 144 | + this.allowUriQueryParameter = allowUriQueryParameter; |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Set this value to configure what header is checked when resolving a Bearer Token. |
| 149 | + * This value is defaulted to {@link HttpHeaders#AUTHORIZATION}. |
| 150 | + * |
| 151 | + * This allows other headers to be used as the Bearer Token source such as |
| 152 | + * {@link HttpHeaders#PROXY_AUTHORIZATION} |
| 153 | + * @param bearerTokenHeaderName the header to check when retrieving the Bearer Token. |
| 154 | + */ |
| 155 | + public void setBearerTokenHeaderName(String bearerTokenHeaderName) { |
| 156 | + this.bearerTokenHeaderName = bearerTokenHeaderName; |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * Set if transport of access token using form-encoded body parameter is supported. |
| 161 | + * Defaults to {@code false}. |
| 162 | + * @param allowFormEncodedBodyParameter if the form-encoded body parameter is |
| 163 | + * supported |
| 164 | + */ |
| 165 | + public void setAllowFormEncodedBodyParameter(boolean allowFormEncodedBodyParameter) { |
| 166 | + this.allowFormEncodedBodyParameter = allowFormEncodedBodyParameter; |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * Set the {@link AuthenticationDetailsSource} to use. Defaults to |
| 171 | + * {@link WebAuthenticationDetailsSource}. |
| 172 | + * @param authenticationDetailsSource the {@code AuthenticationDetailsSource} to use |
| 173 | + */ |
| 174 | + public void setAuthenticationDetailsSource( |
| 175 | + AuthenticationDetailsSource<HttpServletRequest, ?> authenticationDetailsSource) { |
| 176 | + Assert.notNull(authenticationDetailsSource, "authenticationDetailsSource cannot be null"); |
| 177 | + this.authenticationDetailsSource = authenticationDetailsSource; |
| 178 | + } |
| 179 | + |
| 180 | +} |
0 commit comments