3 * Copyright (C) 2009-2024 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.authentication;
22 import com.google.common.base.Strings;
23 import com.google.common.reflect.TypeToken;
24 import com.google.gson.Gson;
25 import com.google.gson.GsonBuilder;
26 import java.io.UnsupportedEncodingException;
27 import java.lang.reflect.Type;
28 import java.util.HashMap;
30 import java.util.Optional;
31 import java.util.regex.Pattern;
32 import javax.annotation.Nullable;
33 import javax.servlet.http.HttpServletRequest;
34 import javax.servlet.http.HttpServletResponse;
36 import static java.net.URLDecoder.decode;
37 import static java.nio.charset.StandardCharsets.UTF_8;
38 import static java.util.Optional.empty;
39 import static org.sonar.server.authentication.AuthenticationRedirection.encodeMessage;
40 import static org.sonar.server.authentication.Cookies.findCookie;
41 import static org.sonar.server.authentication.Cookies.newCookieBuilder;
43 public class OAuth2AuthenticationParametersImpl implements OAuth2AuthenticationParameters {
45 private static final String AUTHENTICATION_COOKIE_NAME = "AUTH-PARAMS";
46 private static final int FIVE_MINUTES_IN_SECONDS = 5 * 60;
47 private static final Pattern VALID_RETURN_TO = Pattern.compile("^/\\w.*");
50 * The HTTP parameter that contains the path where the user should be redirect to.
51 * Please note that the web context is included.
53 private static final String RETURN_TO_PARAMETER = "return_to";
55 private static final Type JSON_MAP_TYPE = new TypeToken<HashMap<String, String>>() {
59 public void init(HttpServletRequest request, HttpServletResponse response) {
60 String returnTo = request.getParameter(RETURN_TO_PARAMETER);
61 Map<String, String> parameters = new HashMap<>();
62 Optional<String> sanitizeRedirectUrl = sanitizeRedirectUrl(returnTo);
63 sanitizeRedirectUrl.ifPresent(s -> parameters.put(RETURN_TO_PARAMETER, s));
64 if (parameters.isEmpty()) {
67 response.addCookie(newCookieBuilder(request)
68 .setName(AUTHENTICATION_COOKIE_NAME)
69 .setValue(toJson(parameters))
71 .setExpiry(FIVE_MINUTES_IN_SECONDS)
76 public Optional<String> getReturnTo(HttpServletRequest request) {
77 return getParameter(request, RETURN_TO_PARAMETER)
78 .flatMap(OAuth2AuthenticationParametersImpl::sanitizeRedirectUrl);
81 private static Optional<String> getParameter(HttpServletRequest request, String parameterKey) {
82 Optional<javax.servlet.http.Cookie> cookie = findCookie(AUTHENTICATION_COOKIE_NAME, request);
83 if (!cookie.isPresent()) {
87 Map<String, String> parameters = fromJson(cookie.get().getValue());
88 if (parameters.isEmpty()) {
91 return Optional.ofNullable(parameters.get(parameterKey));
95 public void delete(HttpServletRequest request, HttpServletResponse response) {
96 response.addCookie(newCookieBuilder(request)
97 .setName(AUTHENTICATION_COOKIE_NAME)
104 private static String toJson(Map<String, String> map) {
105 Gson gson = new GsonBuilder().create();
106 return encodeMessage(gson.toJson(map));
109 private static Map<String, String> fromJson(String json) {
110 Gson gson = new GsonBuilder().create();
112 return gson.fromJson(decode(json, UTF_8.name()), JSON_MAP_TYPE);
113 } catch (UnsupportedEncodingException e) {
114 throw new IllegalStateException(e);
119 * This sanitization has been inspired by 'IsUrlLocalToHost()' method in
120 * https://docs.microsoft.com/en-us/aspnet/mvc/overview/security/preventing-open-redirection-attacks
122 private static Optional<String> sanitizeRedirectUrl(@Nullable String url) {
123 if (Strings.isNullOrEmpty(url)) {
127 String sanitizedUrl = url.trim();
128 boolean isValidUrl = VALID_RETURN_TO.matcher(sanitizedUrl).matches();
133 return Optional.of(sanitizedUrl);