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.saml.ws;
23 import java.io.IOException;
24 import javax.servlet.FilterChain;
25 import javax.servlet.ServletException;
26 import javax.servlet.ServletRequest;
27 import javax.servlet.ServletResponse;
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpServletRequestWrapper;
30 import javax.servlet.http.HttpServletResponse;
31 import org.sonar.api.web.ServletFilter;
32 import org.sonar.auth.saml.SamlAuthenticator;
33 import org.sonar.auth.saml.SamlIdentityProvider;
34 import org.sonar.server.authentication.AuthenticationError;
35 import org.sonar.server.authentication.OAuth2ContextFactory;
36 import org.sonar.server.user.ThreadLocalUserSession;
38 import static org.sonar.server.authentication.SamlValidationRedirectionFilter.SAML_VALIDATION_URL;
40 public class SamlValidationCallbackFilter extends ServletFilter {
42 private final ThreadLocalUserSession userSession;
43 private final SamlAuthenticator samlAuthenticator;
44 private final OAuth2ContextFactory oAuth2ContextFactory;
46 public SamlValidationCallbackFilter(ThreadLocalUserSession userSession, SamlAuthenticator samlAuthenticator, OAuth2ContextFactory oAuth2ContextFactory) {
47 this.samlAuthenticator = samlAuthenticator;
48 this.userSession = userSession;
49 this.oAuth2ContextFactory = oAuth2ContextFactory;
53 public UrlPattern doGetPattern() {
54 return UrlPattern.create(SAML_VALIDATION_URL);
58 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
59 HttpServletResponse httpResponse = (HttpServletResponse) response;
60 HttpServletRequest httpRequest = (HttpServletRequest) request;
61 if (!userSession.hasSession() || !userSession.isSystemAdministrator()) {
62 AuthenticationError.handleError(httpRequest, httpResponse, "User needs to be logged in as system administrator to access this page.");
66 httpRequest = new HttpServletRequestWrapper(httpRequest) {
68 public StringBuffer getRequestURL() {
69 return new StringBuffer(oAuth2ContextFactory.generateCallbackUrl(SamlIdentityProvider.KEY));
73 httpResponse.setContentType("text/html");
74 httpResponse.getWriter().print(samlAuthenticator.getAuthenticationStatusPage(httpRequest, httpResponse));