3 * Copyright (C) 2009-2023 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.io.Resources;
23 import java.io.IOException;
26 import java.nio.charset.StandardCharsets;
27 import javax.annotation.Nullable;
28 import javax.servlet.FilterChain;
29 import javax.servlet.FilterConfig;
30 import javax.servlet.ServletException;
31 import javax.servlet.ServletRequest;
32 import javax.servlet.ServletResponse;
33 import javax.servlet.http.HttpServletResponse;
34 import org.apache.commons.lang.StringUtils;
35 import org.sonar.api.internal.apachecommons.lang.StringEscapeUtils;
36 import org.sonar.api.platform.Server;
37 import org.sonar.api.web.ServletFilter;
39 import static org.sonar.server.authentication.AuthenticationFilter.CALLBACK_PATH;
41 public class SamlValidationRedirectionFilter extends ServletFilter {
43 public static final String VALIDATION_RELAY_STATE = "validation-query";
44 public static final String SAML_VALIDATION_CONTROLLER_CONTEXT = "saml";
45 public static final String SAML_VALIDATION_KEY = "validation";
46 private static final String RELAY_STATE_PARAMETER = "RelayState";
47 private static final String SAML_RESPONSE_PARAMETER = "SAMLResponse";
48 private String redirectionPageTemplate;
49 private final Server server;
51 public SamlValidationRedirectionFilter(Server server) {
56 public UrlPattern doGetPattern() {
57 return UrlPattern.create(CALLBACK_PATH + "saml");
61 public void init(FilterConfig filterConfig) throws ServletException {
62 super.init(filterConfig);
63 this.redirectionPageTemplate = extractTemplate("validation-redirection.html");
66 String extractTemplate(String templateLocation) {
68 URL url = Resources.getResource(templateLocation);
69 return Resources.toString(url, StandardCharsets.UTF_8);
70 } catch (IOException | IllegalArgumentException e) {
71 throw new IllegalStateException("Cannot read the template " + templateLocation, e);
76 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
77 String relayState = request.getParameter(RELAY_STATE_PARAMETER);
79 if (isSamlValidation(relayState)) {
80 HttpServletResponse httpResponse = (HttpServletResponse) response;
82 URI redirectionEndpointUrl = URI.create(server.getContextPath() + "/")
83 .resolve(SAML_VALIDATION_CONTROLLER_CONTEXT + "/")
84 .resolve(SAML_VALIDATION_KEY);
85 String samlResponse = StringEscapeUtils.escapeHtml(request.getParameter(SAML_RESPONSE_PARAMETER));
86 String csrfToken = getCsrfTokenFromRelayState(relayState);
88 String template = StringUtils.replaceEachRepeatedly(redirectionPageTemplate,
89 new String[]{"%VALIDATION_URL%", "%SAML_RESPONSE%", "%CSRF_TOKEN%"},
90 new String[]{redirectionEndpointUrl.toString(), samlResponse, csrfToken});
92 httpResponse.setContentType("text/html");
93 httpResponse.getWriter().print(template);
96 chain.doFilter(request, response);
99 private static boolean isSamlValidation(@Nullable String relayState) {
100 if (relayState != null) {
101 return VALIDATION_RELAY_STATE.equals(relayState.split("/")[0]) && !getCsrfTokenFromRelayState(relayState).isEmpty();
106 private static String getCsrfTokenFromRelayState(@Nullable String relayState) {
107 if (relayState != null && relayState.contains("/")) {
108 return StringEscapeUtils.escapeHtml(relayState.split("/")[1]);