3 * Copyright (C) 2009-2021 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";
56 * This parameter is used to allow the update of login
58 private static final String ALLOW_LOGIN_UPDATE_PARAMETER = "allowUpdateLogin";
60 private static final Type JSON_MAP_TYPE = new TypeToken<HashMap<String, String>>() {
64 public void init(HttpServletRequest request, HttpServletResponse response) {
65 String returnTo = request.getParameter(RETURN_TO_PARAMETER);
66 Map<String, String> parameters = new HashMap<>();
67 Optional<String> sanitizeRedirectUrl = sanitizeRedirectUrl(returnTo);
68 sanitizeRedirectUrl.ifPresent(s -> parameters.put(RETURN_TO_PARAMETER, s));
69 if (parameters.isEmpty()) {
72 response.addCookie(newCookieBuilder(request)
73 .setName(AUTHENTICATION_COOKIE_NAME)
74 .setValue(toJson(parameters))
76 .setExpiry(FIVE_MINUTES_IN_SECONDS)
81 public Optional<String> getReturnTo(HttpServletRequest request) {
82 return getParameter(request, RETURN_TO_PARAMETER)
83 .flatMap(OAuth2AuthenticationParametersImpl::sanitizeRedirectUrl);
86 private static Optional<String> getParameter(HttpServletRequest request, String parameterKey) {
87 Optional<javax.servlet.http.Cookie> cookie = findCookie(AUTHENTICATION_COOKIE_NAME, request);
88 if (!cookie.isPresent()) {
92 Map<String, String> parameters = fromJson(cookie.get().getValue());
93 if (parameters.isEmpty()) {
96 return Optional.ofNullable(parameters.get(parameterKey));
100 public void delete(HttpServletRequest request, HttpServletResponse response) {
101 response.addCookie(newCookieBuilder(request)
102 .setName(AUTHENTICATION_COOKIE_NAME)
109 private static String toJson(Map<String, String> map) {
110 Gson gson = new GsonBuilder().create();
111 return encodeMessage(gson.toJson(map));
114 private static Map<String, String> fromJson(String json) {
115 Gson gson = new GsonBuilder().create();
117 return gson.fromJson(decode(json, UTF_8.name()), JSON_MAP_TYPE);
118 } catch (UnsupportedEncodingException e) {
119 throw new IllegalStateException(e);
124 * This sanitization has been inspired by 'IsUrlLocalToHost()' method in
125 * https://docs.microsoft.com/en-us/aspnet/mvc/overview/security/preventing-open-redirection-attacks
127 private static Optional<String> sanitizeRedirectUrl(@Nullable String url) {
128 if (Strings.isNullOrEmpty(url)) {
132 String sanitizedUrl = url.trim();
133 boolean isValidUrl = VALID_RETURN_TO.matcher(sanitizedUrl).matches();
138 return Optional.of(sanitizedUrl);