@Override
  public void doFilter(HttpRequest request, HttpResponse response, FilterChain filterChain) throws IOException {
    try {
      oAuthCsrfVerifier.verifyState(request, response, samlIdentityProvider, "CSRFToken");
    } catch (AuthenticationException exception) {
      AuthenticationError.handleError(request, response, exception.getMessage());
      return;
    }

    if (!userSession.hasSession() || !userSession.isSystemAdministrator()) {
      AuthenticationError.handleError(request, response, "User needs to be logged in as system administrator to access this page.");
      return;
    }

    HttpServletRequest httpRequest = new HttpServletRequestWrapper(((JavaxHttpRequest) request).getDelegate()) {
      @Override
      public StringBuffer getRequestURL() {
        return new StringBuffer(oAuth2ContextFactory.generateCallbackUrl(SamlIdentityProvider.KEY));
      }
    };

    response.setContentType("text/html");

    String htmlResponse = samlAuthenticator.getAuthenticationStatusPage(new JavaxHttpRequest(httpRequest), response);
    String nonce = SamlValidationCspHeaders.addCspHeadersWithNonceToResponse(response);
    htmlResponse = htmlResponse.replace("%NONCE%", nonce);
    response.getWriter().print(htmlResponse);
  }
@Test
  public void do_filter_admin() throws IOException {
    HttpServletRequest servletRequest = spy(HttpServletRequest.class);
    HttpServletResponse servletResponse = mock(HttpServletResponse.class);
    StringWriter stringWriter = new StringWriter();
    doReturn(new PrintWriter(stringWriter)).when(servletResponse).getWriter();
    FilterChain filterChain = mock(FilterChain.class);

    doReturn(true).when(userSession).hasSession();
    doReturn(true).when(userSession).isSystemAdministrator();
    final String mockedHtmlContent = "mocked html content";
    doReturn(mockedHtmlContent).when(samlAuthenticator).getAuthenticationStatusPage(any(), any());

    underTest.doFilter(new JavaxHttpRequest(servletRequest), new JavaxHttpResponse(servletResponse), filterChain);

    verify(samlAuthenticator).getAuthenticationStatusPage(any(), any());
    verify(servletResponse).getWriter();
    CSP_HEADERS.forEach(h -> verify(servletResponse).setHeader(eq(h), anyString()));
    assertEquals(mockedHtmlContent, stringWriter.toString());
  }