3 * Copyright (C) 2009-2022 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.
21 package org.sonar.server.authentication;
23 import com.google.common.io.Resources;
24 import java.io.IOException;
26 import java.nio.charset.StandardCharsets;
27 import javax.servlet.FilterChain;
28 import javax.servlet.FilterConfig;
29 import javax.servlet.ServletException;
30 import javax.servlet.ServletRequest;
31 import javax.servlet.ServletResponse;
32 import javax.servlet.http.HttpServletRequest;
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_URL = "/saml/validation_callback";
45 private String redirectionPageTemplate;
46 private final Server server;
48 public SamlValidationRedirectionFilter(Server server) {
53 public UrlPattern doGetPattern() {
54 return UrlPattern.create(CALLBACK_PATH + "saml");
58 public void init(FilterConfig filterConfig) throws ServletException {
59 super.init(filterConfig);
60 this.redirectionPageTemplate = extractTemplate("validation-redirection.html");
63 String extractTemplate(String templateLocation) {
65 URL url = Resources.getResource(templateLocation);
66 return Resources.toString(url, StandardCharsets.UTF_8);
67 } catch (IOException | IllegalArgumentException e) {
68 throw new IllegalStateException("Cannot read the template " + templateLocation, e);
73 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
74 HttpServletRequest httpRequest = (HttpServletRequest) request;
75 if (isSamlValidation(httpRequest)) {
76 HttpServletResponse httpResponse = (HttpServletResponse) response;
78 String samlResponse = StringEscapeUtils.escapeHtml(request.getParameter("SAMLResponse"));
80 String template = StringUtils.replaceEachRepeatedly(redirectionPageTemplate,
81 new String[]{"%VALIDATION_URL%", "%SAML_RESPONSE%"},
82 new String[]{server.getContextPath() + SAML_VALIDATION_URL, samlResponse});
84 httpResponse.setContentType("text/html");
85 httpResponse.getWriter().print(template);
88 chain.doFilter(request, response);
91 private static boolean isSamlValidation(HttpServletRequest request) {
92 return VALIDATION_RELAY_STATE.equals(request.getParameter("RelayState"));