3 * Copyright (C) 2009-2019 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 javax.annotation.Nullable;
32 import javax.servlet.http.HttpServletRequest;
33 import javax.servlet.http.HttpServletResponse;
35 import static java.net.URLDecoder.decode;
36 import static java.nio.charset.StandardCharsets.UTF_8;
37 import static java.util.Optional.empty;
38 import static org.apache.commons.lang.StringUtils.isNotBlank;
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;
49 * The HTTP parameter that contains the path where the user should be redirect to.
50 * Please note that the web context is included.
52 private static final String RETURN_TO_PARAMETER = "return_to";
55 * This parameter is used to allow the shift of email from an existing user to the authenticating user
57 private static final String ALLOW_EMAIL_SHIFT_PARAMETER = "allowEmailShift";
60 * This parameter is used to allow the update of login
62 private static final String ALLOW_LOGIN_UPDATE_PARAMETER = "allowUpdateLogin";
64 private static final Type JSON_MAP_TYPE = new TypeToken<HashMap<String, String>>() {
68 public void init(HttpServletRequest request, HttpServletResponse response) {
69 String returnTo = request.getParameter(RETURN_TO_PARAMETER);
70 String allowEmailShift = request.getParameter(ALLOW_EMAIL_SHIFT_PARAMETER);
71 String allowLoginUpdate = request.getParameter(ALLOW_LOGIN_UPDATE_PARAMETER);
72 Map<String, String> parameters = new HashMap<>();
73 Optional<String> sanitizeRedirectUrl = sanitizeRedirectUrl(returnTo);
74 sanitizeRedirectUrl.ifPresent(s -> parameters.put(RETURN_TO_PARAMETER, s));
75 if (isNotBlank(allowEmailShift)) {
76 parameters.put(ALLOW_EMAIL_SHIFT_PARAMETER, allowEmailShift);
78 if (isNotBlank(allowLoginUpdate)) {
79 parameters.put(ALLOW_LOGIN_UPDATE_PARAMETER, allowLoginUpdate);
81 if (parameters.isEmpty()) {
84 response.addCookie(newCookieBuilder(request)
85 .setName(AUTHENTICATION_COOKIE_NAME)
86 .setValue(toJson(parameters))
88 .setExpiry(FIVE_MINUTES_IN_SECONDS)
93 public Optional<String> getReturnTo(HttpServletRequest request) {
94 return getParameter(request, RETURN_TO_PARAMETER);
98 public Optional<Boolean> getAllowEmailShift(HttpServletRequest request) {
99 Optional<String> parameter = getParameter(request, ALLOW_EMAIL_SHIFT_PARAMETER);
100 return parameter.map(Boolean::parseBoolean);
104 public Optional<Boolean> getAllowUpdateLogin(HttpServletRequest request) {
105 Optional<String> parameter = getParameter(request, ALLOW_LOGIN_UPDATE_PARAMETER);
106 return parameter.map(Boolean::parseBoolean);
109 private static Optional<String> getParameter(HttpServletRequest request, String parameterKey) {
110 Optional<javax.servlet.http.Cookie> cookie = findCookie(AUTHENTICATION_COOKIE_NAME, request);
111 if (!cookie.isPresent()) {
115 Map<String, String> parameters = fromJson(cookie.get().getValue());
116 if (parameters.isEmpty()) {
119 return Optional.ofNullable(parameters.get(parameterKey));
123 public void delete(HttpServletRequest request, HttpServletResponse response) {
124 response.addCookie(newCookieBuilder(request)
125 .setName(AUTHENTICATION_COOKIE_NAME)
132 private static String toJson(Map<String, String> map) {
133 Gson gson = new GsonBuilder().create();
134 return encodeMessage(gson.toJson(map));
137 private static Map<String, String> fromJson(String json) {
138 Gson gson = new GsonBuilder().create();
140 return gson.fromJson(decode(json, UTF_8.name()), JSON_MAP_TYPE);
141 } catch (UnsupportedEncodingException e) {
142 throw new IllegalStateException(e);
147 * This sanitization has been inspired by 'IsUrlLocalToHost()' method in
148 * https://docs.microsoft.com/en-us/aspnet/mvc/overview/security/preventing-open-redirection-attacks
150 private static Optional<String> sanitizeRedirectUrl(@Nullable String url) {
151 if (Strings.isNullOrEmpty(url)) {
154 if (url.startsWith("//") || url.startsWith("/\\")) {
157 if (!url.startsWith("/")) {
160 return Optional.of(url);