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;
27 import java.nio.charset.StandardCharsets;
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.HttpServletRequest;
34 import javax.servlet.http.HttpServletResponse;
35 import org.apache.commons.lang.StringUtils;
36 import org.sonar.api.internal.apachecommons.lang.StringEscapeUtils;
37 import org.sonar.api.platform.Server;
38 import org.sonar.api.web.ServletFilter;
40 import static org.sonar.server.authentication.AuthenticationFilter.CALLBACK_PATH;
42 public class SamlValidationRedirectionFilter extends ServletFilter {
44 public static final String VALIDATION_RELAY_STATE = "validation-query";
45 public static final String SAML_VALIDATION_CONTROLLER_CONTEXT = "saml";
46 public static final String SAML_VALIDATION_KEY = "validation";
47 private String redirectionPageTemplate;
48 private final Server server;
50 public SamlValidationRedirectionFilter(Server server) {
55 public UrlPattern doGetPattern() {
56 return UrlPattern.create(CALLBACK_PATH + "saml");
60 public void init(FilterConfig filterConfig) throws ServletException {
61 super.init(filterConfig);
62 this.redirectionPageTemplate = extractTemplate("validation-redirection.html");
65 String extractTemplate(String templateLocation) {
67 URL url = Resources.getResource(templateLocation);
68 return Resources.toString(url, StandardCharsets.UTF_8);
69 } catch (IOException | IllegalArgumentException e) {
70 throw new IllegalStateException("Cannot read the template " + templateLocation, e);
75 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
76 HttpServletRequest httpRequest = (HttpServletRequest) request;
77 if (isSamlValidation(httpRequest)) {
78 HttpServletResponse httpResponse = (HttpServletResponse) response;
80 String samlResponse = StringEscapeUtils.escapeHtml(request.getParameter("SAMLResponse"));
81 URI redirectionEndpointUrl = URI.create(server.getContextPath() + "/")
82 .resolve(SAML_VALIDATION_CONTROLLER_CONTEXT + "/")
83 .resolve(SAML_VALIDATION_KEY);
85 String template = StringUtils.replaceEachRepeatedly(redirectionPageTemplate,
86 new String[]{"%VALIDATION_URL%", "%SAML_RESPONSE%"},
87 new String[]{redirectionEndpointUrl.toString(), samlResponse});
89 httpResponse.setContentType("text/html");
90 httpResponse.getWriter().print(template);
93 chain.doFilter(request, response);
96 private static boolean isSamlValidation(HttpServletRequest request) {
97 return VALIDATION_RELAY_STATE.equals(request.getParameter("RelayState"));