View Javadoc

    1   /*
    2    * Copyright [2006] [University Corporation for Advanced Internet Development, Inc.]
    3    *
    4    * Licensed under the Apache License, Version 2.0 (the "License");
    5    * you may not use this file except in compliance with the License.
    6    * You may obtain a copy of the License at
    7    *
    8    * http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16
    17  package edu.internet2.middleware.shibboleth.idp.authn.provider;
    18
    19  import java.io.IOException;
    20
    21  import javax.servlet.http.HttpServletRequest;
    22  import javax.servlet.http.HttpServletResponse;
    23
    24  import org.opensaml.util.URLBuilder;
    25  import org.slf4j.Logger;
    26  import org.slf4j.LoggerFactory;
    27
    28  /**
    29   * Authentication Handler that redirects to servlet protected by a Web Single-Sign-On system.
    30   */
    31  public class RemoteUserLoginHandler extends AbstractLoginHandler {
    32
    33      /** Class logger. */
    34      private final Logger log = LoggerFactory.getLogger(RemoteUserLoginHandler.class);
    35
    36      /** The URL of the SSO-protected servlet. */
    37      private String servletURL;
    38
    39      /**
    40       * Set the SSO-protected servlet's URL.
    41       *
    42       * @param url The URL of the SSO-protected servlet.
    43       */
    44      public void setServletURL(String url) {
    45          servletURL = url;
    46      }
    47
    48      /**
    49       * Get the URL of the SSO-protected servlet.
    50       *
    51       * @return The URL of the SSO-protected servlet.
    52       */
    53      public String getServletURL() {
    54          return servletURL;
    55      }
    56
    57      /** {@inheritDoc} */
    58      public void login(HttpServletRequest httpRequest, HttpServletResponse httpResponse) {
    59
    60          // forward control to the servlet.
    61          try {
    62              StringBuilder pathBuilder = new StringBuilder();
    63              pathBuilder.append(httpRequest.getContextPath());
    64              if (!servletURL.startsWith("/")) {
    65                  pathBuilder.append("/");
    66              }
    67              pathBuilder.append(servletURL);
    68
    69              URLBuilder urlBuilder = new URLBuilder();
    70              urlBuilder.setScheme(httpRequest.getScheme());
    71              urlBuilder.setHost(httpRequest.getServerName());
    72              urlBuilder.setPort(httpRequest.getServerPort());
    73              urlBuilder.setPath(pathBuilder.toString());
    74
    75              log.debug("Redirecting to {}", urlBuilder.buildURL());
    76              httpResponse.sendRedirect(urlBuilder.buildURL());
    77              return;
    78          } catch (IOException ex) {
    79              log.error("Unable to redirect to remote user authentication servlet.", ex);
    80          }
    81      }
    82  }