|
| 1 | +/* |
| 2 | + * Copyright 2002-2018 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 | + * http://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.web; |
| 18 | + |
| 19 | +import java.util.regex.Matcher; |
| 20 | +import java.util.regex.Pattern; |
| 21 | + |
| 22 | +import javax.servlet.http.HttpServletRequest; |
| 23 | + |
| 24 | +import org.springframework.security.oauth2.server.resource.BearerTokenAuthenticationException; |
| 25 | +import org.springframework.security.oauth2.server.resource.BearerTokenError; |
| 26 | +import org.springframework.security.oauth2.server.resource.BearerTokenErrorCodes; |
| 27 | +import org.springframework.util.StringUtils; |
| 28 | + |
| 29 | +/** |
| 30 | + * The default {@link BearerTokenResolver} implementation based on RFC 6750. |
| 31 | + * |
| 32 | + * @author Vedran Pavic |
| 33 | + * @since 5.1 |
| 34 | + * @see <a href="https://tools.ietf.org/html/rfc6750#section-2" target="_blank">RFC 6750 Section 2: Authenticated Requests</a> |
| 35 | + */ |
| 36 | +public final class DefaultBearerTokenResolver implements BearerTokenResolver { |
| 37 | + |
| 38 | + private static final Pattern authorizationPattern = Pattern.compile("^Bearer (?<token>[^\\s]+)*$"); |
| 39 | + |
| 40 | + private boolean useFormEncodedBodyParameter = false; |
| 41 | + |
| 42 | + private boolean useUriQueryParameter = false; |
| 43 | + |
| 44 | + @Override |
| 45 | + public String resolve(HttpServletRequest request) { |
| 46 | + String authorizationHeaderToken = resolveFromAuthorizationHeader(request); |
| 47 | + String parameterToken = request.getParameter("access_token"); |
| 48 | + if (authorizationHeaderToken != null) { |
| 49 | + if (parameterToken != null) { |
| 50 | + BearerTokenError error = new BearerTokenError(BearerTokenErrorCodes.INVALID_REQUEST); |
| 51 | + throw new BearerTokenAuthenticationException(error, error.toString()); |
| 52 | + } |
| 53 | + return authorizationHeaderToken; |
| 54 | + } |
| 55 | + else if (parameterToken != null && isParameterTokenSupportedForRequest(request)) { |
| 56 | + return parameterToken; |
| 57 | + } |
| 58 | + return null; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Set if transport of access token using form-encoded body parameter is supported. Defaults to {@code false}. |
| 63 | + * @param useFormEncodedBodyParameter if the form-encoded body parameter is supported |
| 64 | + */ |
| 65 | + public void setUseFormEncodedBodyParameter(boolean useFormEncodedBodyParameter) { |
| 66 | + this.useFormEncodedBodyParameter = useFormEncodedBodyParameter; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Set if transport of access token using URI query parameter is supported. Defaults to {@code false}. |
| 71 | + * @param useUriQueryParameter if the URI query parameter is supported |
| 72 | + */ |
| 73 | + public void setUseUriQueryParameter(boolean useUriQueryParameter) { |
| 74 | + this.useUriQueryParameter = useUriQueryParameter; |
| 75 | + } |
| 76 | + |
| 77 | + private static String resolveFromAuthorizationHeader(HttpServletRequest request) { |
| 78 | + String authorization = request.getHeader("Authorization"); |
| 79 | + if (StringUtils.hasText(authorization)) { |
| 80 | + Matcher matcher = authorizationPattern.matcher(authorization); |
| 81 | + if (matcher.matches()) { |
| 82 | + return matcher.group("token"); |
| 83 | + } |
| 84 | + } |
| 85 | + return null; |
| 86 | + } |
| 87 | + |
| 88 | + private boolean isParameterTokenSupportedForRequest(HttpServletRequest request) { |
| 89 | + return ((this.useFormEncodedBodyParameter && "POST".equals(request.getMethod())) |
| 90 | + || (this.useUriQueryParameter && "GET".equals(request.getMethod()))); |
| 91 | + } |
| 92 | + |
| 93 | +} |
0 commit comments