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.apache.commons.lang.StringUtils.isNotBlank;
40 import static org.sonar.server.authentication.AuthenticationRedirection.encodeMessage;
41 import static org.sonar.server.authentication.Cookies.findCookie;
42 import static org.sonar.server.authentication.Cookies.newCookieBuilder;
44 public class OAuth2AuthenticationParametersImpl implements OAuth2AuthenticationParameters {
46 private static final String AUTHENTICATION_COOKIE_NAME = "AUTH-PARAMS";
47 private static final int FIVE_MINUTES_IN_SECONDS = 5 * 60;
48 private static final Pattern VALID_RETURN_TO = Pattern.compile("^/\\w.*");
51 * The HTTP parameter that contains the path where the user should be redirect to.
52 * Please note that the web context is included.
54 private static final String RETURN_TO_PARAMETER = "return_to";
57 * This parameter is used to allow the shift of email from an existing user to the authenticating user
59 private static final String ALLOW_EMAIL_SHIFT_PARAMETER = "allowEmailShift";
62 * This parameter is used to allow the update of login
64 private static final String ALLOW_LOGIN_UPDATE_PARAMETER = "allowUpdateLogin";
66 private static final Type JSON_MAP_TYPE = new TypeToken<HashMap<String, String>>() {
70 public void init(HttpServletRequest request, HttpServletResponse response) {
71 String returnTo = request.getParameter(RETURN_TO_PARAMETER);
72 String allowEmailShift = request.getParameter(ALLOW_EMAIL_SHIFT_PARAMETER);
73 Map<String, String> parameters = new HashMap<>();
74 Optional<String> sanitizeRedirectUrl = sanitizeRedirectUrl(returnTo);
75 sanitizeRedirectUrl.ifPresent(s -> parameters.put(RETURN_TO_PARAMETER, s));
76 if (isNotBlank(allowEmailShift)) {
77 parameters.put(ALLOW_EMAIL_SHIFT_PARAMETER, allowEmailShift);
79 if (parameters.isEmpty()) {
82 response.addCookie(newCookieBuilder(request)
83 .setName(AUTHENTICATION_COOKIE_NAME)
84 .setValue(toJson(parameters))
86 .setExpiry(FIVE_MINUTES_IN_SECONDS)
91 public Optional<String> getReturnTo(HttpServletRequest request) {
92 return getParameter(request, RETURN_TO_PARAMETER)
93 .flatMap(OAuth2AuthenticationParametersImpl::sanitizeRedirectUrl);
97 public Optional<Boolean> getAllowEmailShift(HttpServletRequest request) {
98 Optional<String> parameter = getParameter(request, ALLOW_EMAIL_SHIFT_PARAMETER);
99 return parameter.map(Boolean::parseBoolean);
103 public Optional<Boolean> getAllowUpdateLogin(HttpServletRequest request) {
104 Optional<String> parameter = getParameter(request, ALLOW_LOGIN_UPDATE_PARAMETER);
105 return parameter.map(Boolean::parseBoolean);
108 private static Optional<String> getParameter(HttpServletRequest request, String parameterKey) {
109 Optional<javax.servlet.http.Cookie> cookie = findCookie(AUTHENTICATION_COOKIE_NAME, request);
110 if (!cookie.isPresent()) {
114 Map<String, String> parameters = fromJson(cookie.get().getValue());
115 if (parameters.isEmpty()) {
118 return Optional.ofNullable(parameters.get(parameterKey));
122 public void delete(HttpServletRequest request, HttpServletResponse response) {
123 response.addCookie(newCookieBuilder(request)
124 .setName(AUTHENTICATION_COOKIE_NAME)
131 private static String toJson(Map<String, String> map) {
132 Gson gson = new GsonBuilder().create();
133 return encodeMessage(gson.toJson(map));
136 private static Map<String, String> fromJson(String json) {
137 Gson gson = new GsonBuilder().create();
139 return gson.fromJson(decode(json, UTF_8.name()), JSON_MAP_TYPE);
140 } catch (UnsupportedEncodingException e) {
141 throw new IllegalStateException(e);
146 * This sanitization has been inspired by 'IsUrlLocalToHost()' method in
147 * https://docs.microsoft.com/en-us/aspnet/mvc/overview/security/preventing-open-redirection-attacks
149 private static Optional<String> sanitizeRedirectUrl(@Nullable String url) {
150 if (Strings.isNullOrEmpty(url)) {
154 String sanitizedUrl = url.trim();
155 boolean isValidUrl = VALID_RETURN_TO.matcher(sanitizedUrl).matches();
160 return Optional.of(sanitizedUrl);