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.profile.saml1;
    18
    19  import java.util.ArrayList;
    20
    21  import org.opensaml.common.SAMLObjectBuilder;
    22  import org.opensaml.common.binding.BasicEndpointSelector;
    23  import org.opensaml.common.binding.decoding.SAMLMessageDecoder;
    24  import org.opensaml.common.xml.SAMLConstants;
    25  import org.opensaml.saml1.core.AttributeQuery;
    26  import org.opensaml.saml1.core.AttributeStatement;
    27  import org.opensaml.saml1.core.Request;
    28  import org.opensaml.saml1.core.Response;
    29  import org.opensaml.saml1.core.Statement;
    30  import org.opensaml.saml1.core.StatusCode;
    31  import org.opensaml.saml1.core.Subject;
    32  import org.opensaml.saml2.metadata.AssertionConsumerService;
    33  import org.opensaml.saml2.metadata.AttributeAuthorityDescriptor;
    34  import org.opensaml.saml2.metadata.Endpoint;
    35  import org.opensaml.saml2.metadata.EntityDescriptor;
    36  import org.opensaml.saml2.metadata.SPSSODescriptor;
    37  import org.opensaml.saml2.metadata.provider.MetadataProvider;
    38  import org.opensaml.ws.message.decoder.MessageDecodingException;
    39  import org.opensaml.ws.transport.http.HTTPInTransport;
    40  import org.opensaml.ws.transport.http.HTTPOutTransport;
    41  import org.opensaml.xml.security.SecurityException;
    42  import org.slf4j.Logger;
    43  import org.slf4j.LoggerFactory;
    44
    45  import edu.internet2.middleware.shibboleth.common.profile.ProfileException;
    46  import edu.internet2.middleware.shibboleth.common.profile.provider.BaseSAMLProfileRequestContext;
    47  import edu.internet2.middleware.shibboleth.common.relyingparty.provider.saml1.AttributeQueryConfiguration;
    48  import edu.internet2.middleware.shibboleth.idp.session.AuthenticationMethodInformation;
    49  import edu.internet2.middleware.shibboleth.idp.session.Session;
    50
    51  /**
    52   * SAML 1 Attribute Query profile handler.
    53   */
    54  public class AttributeQueryProfileHandler extends AbstractSAML1ProfileHandler {
    55
    56      /** Class logger. */
    57      private final Logger log = LoggerFactory.getLogger(AttributeQueryProfileHandler.class);
    58
    59      /** Builder of assertion consumer service endpoints. */
    60      private SAMLObjectBuilder<AssertionConsumerService> acsEndpointBuilder;
    61
    62      /** Constructor. */
    63      public AttributeQueryProfileHandler() {
    64          super();
    65
    66          acsEndpointBuilder = (SAMLObjectBuilder<AssertionConsumerService>) getBuilderFactory().getBuilder(
    67                  AssertionConsumerService.DEFAULT_ELEMENT_NAME);
    68      }
    69
    70      /** {@inheritDoc} */
    71      public String getProfileId() {
    72          return AttributeQueryConfiguration.PROFILE_ID;
    73      }
    74
    75      /** {@inheritDoc} */
    76      public void processRequest(HTTPInTransport inTransport, HTTPOutTransport outTransport) throws ProfileException {
    77          AttributeQueryContext requestContext = null;
    78          Response samlResponse;
    79          try {
    80              requestContext = decodeRequest(inTransport, outTransport);
    81
    82              if (requestContext.getProfileConfiguration() == null) {
    83                  log.error("SAML 1 Attribute Query profile is not configured for relying party "
    84                          + requestContext.getInboundMessageIssuer());
    85                  requestContext.setFailureStatus(buildStatus(StatusCode.RESPONDER, StatusCode.REQUEST_DENIED,
    86                          "SAML 1 Attribute Query profile is not configured for relying party "
    87                                  + requestContext.getInboundMessageIssuer()));
    88                  samlResponse = buildErrorResponse(requestContext);
    89              } else {
    90                  resolvePrincipal(requestContext);
    91
    92                  Session idpSession = getSessionManager().getSession(requestContext.getPrincipalName());
    93                  if (idpSession != null) {
    94                      AuthenticationMethodInformation authnInfo = idpSession.getAuthenticationMethods().get(
    95                              requestContext.getInboundMessageIssuer());
    96                      if (authnInfo != null) {
    97                          requestContext.setPrincipalAuthenticationMethod(authnInfo.getAuthenticationMethod());
    98                      }
    99                  }
    100
    101                 resolveAttributes(requestContext);
    102                 requestContext.setReleasedAttributes(requestContext.getAttributes().keySet());
    103
    104                 ArrayList<Statement> statements = new ArrayList<Statement>();
    105                 AttributeStatement attributeStatement = buildAttributeStatement(requestContext,
    106                         "urn:oasis:names:tc:SAML:1.0:cm:sender-vouches");
    107                 if (attributeStatement != null) {
    108                     statements.add(attributeStatement);
    109                 }
    110
    111                 samlResponse = buildResponse(requestContext, statements);
    112             }
    113         } catch (ProfileException e) {
    114             samlResponse = buildErrorResponse(requestContext);
    115         }
    116
    117         requestContext.setOutboundSAMLMessage(samlResponse);
    118         requestContext.setOutboundSAMLMessageId(samlResponse.getID());
    119         requestContext.setOutboundSAMLMessageIssueInstant(samlResponse.getIssueInstant());
    120         encodeResponse(requestContext);
    121         writeAuditLogEntry(requestContext);
    122     }
    123
    124     /**
    125      * Decodes an incoming request and populates a created request context with the resultant information.
    126      *
    127      * @param inTransport inbound message transport
    128      * @param outTransport outbound message transport
    129      *
    130      * @return the created request context
    131      *
    132      * @throws ProfileException throw if there is a problem decoding the request
    133      */
    134     protected AttributeQueryContext decodeRequest(HTTPInTransport inTransport, HTTPOutTransport outTransport)
    135             throws ProfileException {
    136         log.debug("Decoding message with decoder binding {}", getInboundBinding());
    137
    138         AttributeQueryContext requestContext = new AttributeQueryContext();
    139         requestContext.setCommunicationProfileId(getProfileId());
    140
    141         MetadataProvider metadataProvider = getMetadataProvider();
    142         requestContext.setMetadataProvider(metadataProvider);
    143
    144         requestContext.setInboundMessageTransport(inTransport);
    145         requestContext.setInboundSAMLProtocol(SAMLConstants.SAML11P_NS);
    146         requestContext.setSecurityPolicyResolver(getSecurityPolicyResolver());
    147         requestContext.setPeerEntityRole(SPSSODescriptor.DEFAULT_ELEMENT_NAME);
    148
    149         requestContext.setOutboundMessageTransport(outTransport);
    150         requestContext.setOutboundSAMLProtocol(SAMLConstants.SAML11P_NS);
    151
    152         try {
    153             SAMLMessageDecoder decoder = getMessageDecoders().get(getInboundBinding());
    154             if (decoder == null) {
    155                 throw new ProfileException("No message decoder configured for inbound binding " + getInboundBinding());
    156             }
    157             requestContext.setMessageDecoder(decoder);
    158             decoder.decode(requestContext);
    159             log.debug("Decoded request");
    160
    161             Request request = requestContext.getInboundSAMLMessage();
    162             if (request == null || !(request instanceof Request) || request.getAttributeQuery() == null) {
    163                 log.error("Incoming message was not an Attribute request");
    164                 requestContext.setFailureStatus(buildStatus(StatusCode.REQUESTER, null,
    165                         "Invalid SAML Attribute Request message."));
    166                 throw new ProfileException("Invalid SAML Attribute Request message.");
    167             }
    168
    169             return requestContext;
    170         } catch (MessageDecodingException e) {
    171             log.error("Error decoding attribute query message", e);
    172             requestContext.setFailureStatus(buildStatus(StatusCode.RESPONDER, null, "Error decoding message"));
    173             throw new ProfileException("Error decoding attribute query message", e);
    174         } catch (SecurityException e) {
    175             log.error("Message did not meet security requirements", e);
    176             requestContext.setFailureStatus(buildStatus(StatusCode.RESPONDER, StatusCode.REQUEST_DENIED,
    177                     "Message did not meet security requirements"));
    178             throw new ProfileException("Message did not meet security policy requirements", e);
    179         } finally {
    180             // Set as much information as can be retrieved from the decoded message
    181             populateRequestContext(requestContext);
    182         }
    183     }
    184
    185     /** {@inheritDoc} */
    186     protected void populateRelyingPartyInformation(BaseSAMLProfileRequestContext requestContext)
    187             throws ProfileException {
    188         super.populateRelyingPartyInformation(requestContext);
    189
    190         EntityDescriptor relyingPartyMetadata = requestContext.getPeerEntityMetadata();
    191         if (relyingPartyMetadata != null) {
    192             requestContext.setPeerEntityRole(SPSSODescriptor.DEFAULT_ELEMENT_NAME);
    193             requestContext.setPeerEntityRoleMetadata(relyingPartyMetadata.getSPSSODescriptor(SAMLConstants.SAML11P_NS));
    194         }
    195     }
    196
    197     /** {@inheritDoc} */
    198     protected void populateAssertingPartyInformation(BaseSAMLProfileRequestContext requestContext)
    199             throws ProfileException {
    200         super.populateAssertingPartyInformation(requestContext);
    201
    202         EntityDescriptor localEntityDescriptor = requestContext.getLocalEntityMetadata();
    203         if (localEntityDescriptor != null) {
    204             requestContext.setLocalEntityRole(AttributeAuthorityDescriptor.DEFAULT_ELEMENT_NAME);
    205             requestContext.setLocalEntityRoleMetadata(localEntityDescriptor
    206                     .getAttributeAuthorityDescriptor(SAMLConstants.SAML11P_NS));
    207         }
    208     }
    209
    210     /**
    211      * Populates the request context with information from the inbound SAML message.
    212      *
    213      * This method requires the the following request context properties to be populated: inbound saml message
    214      *
    215      * This methods populates the following request context properties: subject name identifier
    216      *
    217      * @param requestContext current request context
    218      *
    219      * @throws ProfileException thrown if the inbound SAML message or subject identifier is null
    220      */
    221     protected void populateSAMLMessageInformation(BaseSAMLProfileRequestContext requestContext) throws ProfileException {
    222         Request request = (Request) requestContext.getInboundSAMLMessage();
    223         if (request == null) {
    224             log.error("Decoder did not contain an attribute query, an error occured decoding the message");
    225             throw new ProfileException("Unable to decode message.");
    226         }
    227
    228         AttributeQuery query = request.getAttributeQuery();
    229         if (query != null) {
    230             Subject subject = query.getSubject();
    231             if (subject == null) {
    232                 log.error("Attribute query did not contain a proper subject");
    233                 ((AttributeQueryContext) requestContext).setFailureStatus(buildStatus(StatusCode.REQUESTER, null,
    234                         "Attribute query did not contain a proper subject"));
    235                 throw new ProfileException("Attribute query did not contain a proper subject");
    236             }
    237             requestContext.setSubjectNameIdentifier(subject.getNameIdentifier());
    238         }
    239     }
    240
    241     /**
    242      * Selects the appropriate endpoint for the relying party and stores it in the request context.
    243      *
    244      * @param requestContext current request context
    245      *
    246      * @return Endpoint selected from the information provided in the request context
    247      */
    248     protected Endpoint selectEndpoint(BaseSAMLProfileRequestContext requestContext) {
    249         Endpoint endpoint;
    250
    251         if (getInboundBinding().equals(SAMLConstants.SAML1_SOAP11_BINDING_URI)) {
    252             endpoint = acsEndpointBuilder.buildObject();
    253             endpoint.setBinding(SAMLConstants.SAML1_SOAP11_BINDING_URI);
    254         } else {
    255             BasicEndpointSelector endpointSelector = new BasicEndpointSelector();
    256             endpointSelector.setEndpointType(AssertionConsumerService.DEFAULT_ELEMENT_NAME);
    257             endpointSelector.setMetadataProvider(getMetadataProvider());
    258             endpointSelector.setEntityMetadata(requestContext.getPeerEntityMetadata());
    259             endpointSelector.setEntityRoleMetadata(requestContext.getPeerEntityRoleMetadata());
    260             endpointSelector.setSamlRequest(requestContext.getInboundSAMLMessage());
    261             endpointSelector.getSupportedIssuerBindings().addAll(getSupportedOutboundBindings());
    262             endpoint = endpointSelector.selectEndpoint();
    263         }
    264
    265         return endpoint;
    266     }
    267
    268     /** Basic data structure used to accumulate information as a request is being processed. */
    269     protected class AttributeQueryContext extends
    270             BaseSAML1ProfileRequestContext<Request, Response, AttributeQueryConfiguration> {}
    271 }