All Downloads are FREE. Search and download functionalities are using the official Maven repository.

se.swedenconnect.spring.saml.idp.response.DefaultResponsePage Maven / Gradle / Ivy

There is a newer version: 2.2.1
Show newest version
/*
 * Copyright 2023-2024 Sweden Connect
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package se.swedenconnect.spring.saml.idp.response;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

/**
 * A helper for creating the HTML page that posts the response back to the Service Provider.
 *
 * @author Martin Lindström
 */
public class DefaultResponsePage implements ResponsePage {

  private static final MediaType TEXT_HTML_UTF8 = new MediaType("text", "html", StandardCharsets.UTF_8);
  private static final String NEWLINE = System.lineSeparator();

  /** {@inheritDoc} */
  @Override
  public void sendResponse(final HttpServletRequest httpServletRequest, final HttpServletResponse httpServletResponse,
      final String destination, final String samlResponse, final String relayState) throws IOException {
    final String responsePage = generateResponsePage(destination, samlResponse, relayState);

    httpServletResponse.setContentType(TEXT_HTML_UTF8.toString());
    httpServletResponse.setContentLength(responsePage.getBytes(StandardCharsets.UTF_8).length);
    httpServletResponse.setHeader("Cache-control", "no-cache, no-store");
    httpServletResponse.setHeader("Pragma", "no-cache");
    httpServletResponse.getWriter().write(responsePage);
  }

  /**
   * Generates an HTML page for posting the SAML response message.
   *
   * @param destination the destination URL
   * @param samlResponse the Base64-encoded SAML response message
   * @param relayState the relay state (may be null)
   * @return a String containing the contents of the HTML page
   */
  public static String generateResponsePage(
      final String destination, final String samlResponse, final String relayState) {

    final StringBuilder builder = new StringBuilder().append(NEWLINE);

    builder.append("").append(NEWLINE);
    builder.append("").append(NEWLINE);
    builder.append("").append(NEWLINE);
    builder.append("  ").append(NEWLINE);
    builder.append("  ")
        .append(NEWLINE);
    builder.append("  SAML Response").append(NEWLINE);
    builder.append("").append(NEWLINE);
    builder.append("").append(NEWLINE);
    builder.append("  
").append(NEWLINE); builder.append(" ") .append(NEWLINE); if (StringUtils.hasText(relayState)) { builder.append(" ") .append(NEWLINE); } builder.append(" ").append(NEWLINE); builder.append("
").append(NEWLINE); builder.append("").append(NEWLINE); builder.append("").append(NEWLINE); return builder.toString(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy