View Javadoc

    1   /*
    2    * Copyright [2007] [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.profile.saml1;
    18
    19  import org.joda.time.DateTime;
    20  import org.joda.time.chrono.ISOChronology;
    21  import org.opensaml.common.binding.SAMLMessageContext;
    22  import org.opensaml.common.binding.decoding.SAMLMessageDecoder;
    23  import org.opensaml.saml1.binding.decoding.BaseSAML1MessageDecoder;
    24  import org.opensaml.ws.message.MessageContext;
    25  import org.opensaml.ws.message.decoder.MessageDecodingException;
    26  import org.opensaml.ws.transport.http.HTTPInTransport;
    27  import org.opensaml.ws.transport.http.HttpServletRequestAdapter;
    28  import org.opensaml.xml.util.DatatypeHelper;
    29  import org.slf4j.Logger;
    30  import org.slf4j.LoggerFactory;
    31
    32  import edu.internet2.middleware.shibboleth.idp.profile.saml1.ShibbolethSSOProfileHandler.ShibbolethSSORequestContext;
    33
    34  /**
    35   * Shibboleth 1.0 SSO authentication request message decoder.
    36   */
    37  public class ShibbolethSSODecoder extends BaseSAML1MessageDecoder implements SAMLMessageDecoder {
    38
    39      /** Class logger. */
    40      private final Logger log = LoggerFactory.getLogger(ShibbolethSSODecoder.class);
    41
    42      /** Constructor. */
    43      public ShibbolethSSODecoder(){
    44          super();
    45      }
    46
    47      /** {@inheritDoc} */
    48      public String getBindingURI() {
    49          return "urn:mace:shibboleth:1.0:profiles:AuthnRequest";
    50      }
    51
    52      /** {@inheritDoc} */
    53      protected void doDecode(MessageContext messageContext) throws MessageDecodingException {
    54          if (!(messageContext instanceof ShibbolethSSORequestContext)) {
    55              log.warn("Invalid message context type, this decoder only support ShibbolethSSORequestContext");
    56              throw new MessageDecodingException(
    57                      "Invalid message context type, this decoder only support ShibbolethSSORequestContext");
    58          }
    59
    60          if (!(messageContext.getInboundMessageTransport() instanceof HTTPInTransport)) {
    61              log.warn("Invalid inbound message transport type, this decoder only support HTTPInTransport");
    62              throw new MessageDecodingException(
    63                      "Invalid inbound message transport type, this decoder only support HTTPInTransport");
    64          }
    65
    66          ShibbolethSSORequestContext requestContext = (ShibbolethSSORequestContext) messageContext;
    67          HTTPInTransport transport = (HTTPInTransport) messageContext.getInboundMessageTransport();
    68
    69          String providerId = DatatypeHelper.safeTrimOrNullString(transport.getParameterValue("providerId"));
    70          if (providerId == null) {
    71              log.warn("No providerId parameter given in Shibboleth SSO authentication request.");
    72              throw new MessageDecodingException(
    73                      "No providerId parameter given in Shibboleth SSO authentication request.");
    74          }
    75          requestContext.setInboundMessageIssuer(providerId);
    76          requestContext.setPeerEntityId(providerId);
    77
    78          String shire = DatatypeHelper.safeTrimOrNullString(transport.getParameterValue("shire"));
    79          if (shire == null) {
    80              log.warn("No shire parameter given in Shibboleth SSO authentication request.");
    81              throw new MessageDecodingException("No shire parameter given in Shibboleth SSO authentication request.");
    82          }
    83          requestContext.setSpAssertionConsumerService(shire);
    84
    85          String target = DatatypeHelper.safeTrimOrNullString(transport.getParameterValue("target"));
    86          if (target == null) {
    87              log.warn("No target parameter given in Shibboleth SSO authentication request.");
    88              throw new MessageDecodingException("No target parameter given in Shibboleth SSO authentication request.");
    89          }
    90          requestContext.setRelayState(target);
    91
    92          String timeStr = DatatypeHelper.safeTrimOrNullString(transport.getParameterValue("time"));
    93          if (timeStr != null) {
    94              long time = Long.parseLong(timeStr) * 1000;
    95              requestContext.setInboundSAMLMessageIssueInstant(new DateTime(time, ISOChronology.getInstanceUTC()));
    96
    97              // If a timestamp is provided, construct a pseudo message ID by combining the timestamp
    98              // and a client-specific ID (the Java session ID).
    99              String sessionID = ((HttpServletRequestAdapter) transport).getWrappedRequest().getRequestedSessionId();
    100             if (sessionID != null) {
    101                 requestContext.setInboundSAMLMessageId(sessionID + '!' + timeStr);
    102             }
    103         }
    104
    105         populateRelyingPartyMetadata(requestContext);
    106     }
    107
    108     /** {@inheritDoc} */
    109     protected boolean isIntendedDestinationEndpointURIRequired(SAMLMessageContext samlMsgCtx) {
    110         return false;
    111     }
    112
    113     /** {@inheritDoc} */
    114     protected String getIntendedDestinationEndpointURI(SAMLMessageContext samlMsgCtx) throws MessageDecodingException {
    115         // Not relevant in this binding/profile, there is neither SAML message
    116         // nor binding parameter with this information
    117         return null;
    118     }
    119
    120 }