package edu.internet2.middleware.shibboleth.idp.profile.saml2;
import java.util.ArrayList;
-import java.util.Map;
-import javax.servlet.ServletRequest;
-import javax.servlet.ServletResponse;
-
-import org.apache.log4j.Logger;
-import org.opensaml.common.binding.BindingException;
-import org.opensaml.common.binding.decoding.MessageDecoder;
-import org.opensaml.common.binding.encoding.MessageEncoder;
-import org.opensaml.common.binding.security.SAMLSecurityPolicy;
+import org.opensaml.common.SAMLObjectBuilder;
+import org.opensaml.common.binding.BasicEndpointSelector;
+import org.opensaml.common.binding.decoding.SAMLMessageDecoder;
+import org.opensaml.common.xml.SAMLConstants;
import org.opensaml.saml2.core.AttributeQuery;
import org.opensaml.saml2.core.AttributeStatement;
-import org.opensaml.saml2.core.NameID;
import org.opensaml.saml2.core.Response;
import org.opensaml.saml2.core.Statement;
import org.opensaml.saml2.core.StatusCode;
import org.opensaml.saml2.core.Subject;
+import org.opensaml.saml2.metadata.AssertionConsumerService;
import org.opensaml.saml2.metadata.AttributeAuthorityDescriptor;
+import org.opensaml.saml2.metadata.Endpoint;
+import org.opensaml.saml2.metadata.EntityDescriptor;
import org.opensaml.saml2.metadata.SPSSODescriptor;
-import org.opensaml.ws.security.SecurityPolicyException;
+import org.opensaml.saml2.metadata.provider.MetadataProvider;
+import org.opensaml.ws.message.decoder.MessageDecodingException;
+import org.opensaml.ws.transport.http.HTTPInTransport;
+import org.opensaml.ws.transport.http.HTTPOutTransport;
+import org.opensaml.xml.security.SecurityException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
-import edu.internet2.middleware.shibboleth.common.attribute.AttributeRequestException;
-import edu.internet2.middleware.shibboleth.common.attribute.BaseAttribute;
-import edu.internet2.middleware.shibboleth.common.attribute.provider.SAML2AttributeAuthority;
-import edu.internet2.middleware.shibboleth.common.attribute.provider.ShibbolethSAMLAttributeRequestContext;
import edu.internet2.middleware.shibboleth.common.profile.ProfileException;
-import edu.internet2.middleware.shibboleth.common.profile.ProfileRequest;
-import edu.internet2.middleware.shibboleth.common.profile.ProfileResponse;
+import edu.internet2.middleware.shibboleth.common.profile.provider.BaseSAMLProfileRequestContext;
import edu.internet2.middleware.shibboleth.common.relyingparty.provider.saml2.AttributeQueryConfiguration;
-import edu.internet2.middleware.shibboleth.idp.session.ServiceInformation;
+import edu.internet2.middleware.shibboleth.idp.session.AuthenticationMethodInformation;
import edu.internet2.middleware.shibboleth.idp.session.Session;
-/**
- * SAML 2.0 Attribute Query profile handler.
- */
+/** SAML 2.0 Attribute Query profile handler. */
public class AttributeQueryProfileHandler extends AbstractSAML2ProfileHandler {
/** Class logger. */
- private static Logger log = Logger.getLogger(AttributeQueryProfileHandler.class);
+ private static Logger log = LoggerFactory.getLogger(AttributeQueryProfileHandler.class);
+
+ /** Builder of assertion consumer service endpoints. */
+ private SAMLObjectBuilder<AssertionConsumerService> acsEndpointBuilder;
- /** SAML binding URI. */
- private static final String BINDING = "urn:oasis:names:tc:SAML:2.0:bindings:SOAP";
+ /** Constructor. */
+ public AttributeQueryProfileHandler() {
+ super();
+
+ acsEndpointBuilder = (SAMLObjectBuilder<AssertionConsumerService>) getBuilderFactory().getBuilder(
+ AssertionConsumerService.DEFAULT_ELEMENT_NAME);
+ }
/** {@inheritDoc} */
public String getProfileId() {
- return "urn:mace:shibboleth:2.0:idp:profiles:saml2:query:attribute";
+ return AttributeQueryConfiguration.PROFILE_ID;
}
/** {@inheritDoc} */
- public void processRequest(ProfileRequest<ServletRequest> request, ProfileResponse<ServletResponse> response)
- throws ProfileException {
+ public void processRequest(HTTPInTransport inTransport, HTTPOutTransport outTransport) throws ProfileException {
+ Response samlResponse;
- AttributeQueryContext requestContext = new AttributeQueryContext(request, response);
+ AttributeQueryContext requestContext = null;
- Response samlResponse;
try {
- decodeRequest(requestContext);
-
- // populate request context with information from decoded message
- SAMLSecurityPolicy securityPolicy = requestContext.getMessageDecoder().getSecurityPolicy();
- requestContext.setRelyingPartyId(securityPolicy.getIssuer());
- requestContext
- .setRelyingPartyConfiguration(getRelyingPartyConfiguration(requestContext.getRelyingPartyId()));
- requestContext.setRelyingPartyRole(SPSSODescriptor.DEFAULT_ELEMENT_NAME);
- requestContext.setAssertingPartyId(requestContext.getRelyingPartyConfiguration().getProviderId());
- requestContext.setAssertingPartyRole(AttributeAuthorityDescriptor.DEFAULT_ELEMENT_NAME);
- requestContext.setProfileConfiguration((AttributeQueryConfiguration) getProfileConfiguration(requestContext
- .getRelyingPartyId(), AttributeQueryConfiguration.PROFILE_ID));
- requestContext.setSamlRequest((AttributeQuery) requestContext.getMessageDecoder().getSAMLMessage());
-
- // TODO principal
-
- // create the SAML attribute statement
- ArrayList<Statement> statements = new ArrayList<Statement>();
- statements.add(buildAttributeStatement(requestContext));
+ requestContext = decodeRequest(inTransport, outTransport);
- //TODO NameID
- Subject assertionSubject = buildSubject(requestContext, "urn:oasis:names:tc:SAML:2.0:cm:sender-vouches");
-
- // create the SAML response
- samlResponse = buildResponse(requestContext, assertionSubject, statements);
- } catch (SecurityPolicyException e) {
- samlResponse = buildErrorResponse(requestContext, StatusCode.REQUESTER_URI, StatusCode.REQUEST_DENIED_URI,
- e.getMessage());
- } catch (AttributeRequestException e) {
- samlResponse = buildErrorResponse(requestContext, StatusCode.RESPONDER_URI,
- StatusCode.INVALID_ATTR_NAME_VALUE_URI, e.getMessage());
+ if (requestContext.getProfileConfiguration() == null) {
+ log.error("SAML 2 Attribute Query profile is not configured for relying party "
+ + requestContext.getInboundMessageIssuer());
+ requestContext.setFailureStatus(buildStatus(StatusCode.RESPONDER_URI, StatusCode.REQUEST_DENIED_URI,
+ "SAML 2 Attribute Query profile is not configured for relying party "
+ + requestContext.getInboundMessageIssuer()));
+ samlResponse = buildErrorResponse(requestContext);
+ } else {
+ checkSamlVersion(requestContext);
+
+ // Resolve attribute query name id to principal name and place in context
+ resolvePrincipal(requestContext);
+
+ Session idpSession = getSessionManager().getSession(requestContext.getPrincipalName());
+ if (idpSession != null) {
+ AuthenticationMethodInformation authnInfo = idpSession.getAuthenticationMethods().get(
+ requestContext.getInboundMessageIssuer());
+ if (authnInfo != null) {
+ requestContext.setPrincipalAuthenticationMethod(authnInfo.getAuthenticationMethod());
+ }
+ }
+
+ resolveAttributes(requestContext);
+ requestContext.setReleasedAttributes(requestContext.getAttributes().keySet());
+
+ // Lookup principal name and attributes, create attribute statement from information
+ ArrayList<Statement> statements = new ArrayList<Statement>();
+ AttributeStatement attributeStatement = buildAttributeStatement(requestContext);
+ if (attributeStatement != null) {
+ statements.add(attributeStatement);
+ }
+
+ // create the SAML response
+ samlResponse = buildResponse(requestContext, "urn:oasis:names:tc:SAML:2.0:cm:sender-vouches",
+ statements);
+ }
+ } catch (ProfileException e) {
+ samlResponse = buildErrorResponse(requestContext);
}
- requestContext.setSamlResponse(samlResponse);
+ requestContext.setOutboundSAMLMessage(samlResponse);
+ requestContext.setOutboundSAMLMessageId(samlResponse.getID());
+ requestContext.setOutboundSAMLMessageIssueInstant(samlResponse.getIssueInstant());
encodeResponse(requestContext);
writeAuditLogEntry(requestContext);
}
/**
- * Decodes the message in the request and adds it to the request context.
+ * Decodes an incoming request and populates a created request context with the resultant information.
*
- * @param requestContext request context contianing the request to decode
+ * @param inTransport inbound message transport
+ * @param outTransport outbound message transport
+ *
+ * @return the created request context
*
* @throws ProfileException throw if there is a problem decoding the request
- * @throws SecurityPolicyException thrown if the message was decoded properly but did not meet the necessary
- * security policy requirements
*/
- protected void decodeRequest(AttributeQueryContext requestContext) throws ProfileException, SecurityPolicyException {
- MessageDecoder<ServletRequest> decoder = getMessageDecoderFactory().getMessageDecoder(BINDING);
- if (decoder == null) {
- throw new ProfileException("No request decoder was registered for binding type: " + BINDING);
- }
+ protected AttributeQueryContext decodeRequest(HTTPInTransport inTransport, HTTPOutTransport outTransport)
+ throws ProfileException {
+ log.debug("Decoding message with decoder binding {}", getInboundBinding());
+
+ AttributeQueryContext requestContext = new AttributeQueryContext();
+ requestContext.setCommunicationProfileId(getProfileId());
+
+ MetadataProvider metadataProvider = getMetadataProvider();
+ requestContext.setMetadataProvider(metadataProvider);
- super.populateMessageDecoder(decoder);
- decoder.setRequest(requestContext.getProfileRequest().getRawRequest());
- requestContext.setMessageDecoder(decoder);
+ requestContext.setInboundMessageTransport(inTransport);
+ requestContext.setInboundSAMLProtocol(SAMLConstants.SAML20P_NS);
+ requestContext.setSecurityPolicyResolver(getSecurityPolicyResolver());
+ requestContext.setPeerEntityRole(SPSSODescriptor.DEFAULT_ELEMENT_NAME);
+
+ requestContext.setOutboundMessageTransport(outTransport);
+ requestContext.setOutboundSAMLProtocol(SAMLConstants.SAML20P_NS);
try {
- decoder.decode();
- if (log.isDebugEnabled()) {
- log.debug("decoded http servlet request");
+ SAMLMessageDecoder decoder = getMessageDecoders().get(getInboundBinding());
+ requestContext.setMessageDecoder(decoder);
+ decoder.decode(requestContext);
+ log.debug("Decoded request");
+
+ if (!(requestContext.getInboundSAMLMessage() instanceof AttributeQuery)) {
+ log.error("Incoming message was not a AttributeQuery, it was a {}", requestContext
+ .getInboundSAMLMessage().getClass().getName());
+ requestContext.setFailureStatus(buildStatus(StatusCode.REQUESTER_URI, null,
+ "Invalid SAML AttributeQuery message."));
+ throw new ProfileException("Invalid SAML AttributeQuery message.");
}
- } catch (BindingException e) {
+
+ return requestContext;
+ } catch (MessageDecodingException e) {
log.error("Error decoding attribute query message", e);
+ requestContext.setFailureStatus(buildStatus(StatusCode.RESPONDER_URI, null, "Error decoding message"));
throw new ProfileException("Error decoding attribute query message");
+ } catch (SecurityException e) {
+ log.error("Message did not meet security requirements", e);
+ requestContext.setFailureStatus(buildStatus(StatusCode.RESPONDER_URI, StatusCode.REQUEST_DENIED_URI,
+ "Message did not meet security requirements"));
+ throw new ProfileException("Message did not meet security requirements", e);
+ } finally {
+ // Set as much information as can be retrieved from the decoded message
+ populateRequestContext(requestContext);
}
}
- /**
- * Executes a query for attributes and builds a SAML attribute statement from the results.
- *
- * @param requestContext current request context
- *
- * @return attribute statement resulting from the query
- *
- * @throws ProfileException thrown if there is a problem making the query
- * @throws AttributeRequestException thrown if there is a problem resolving attributes
- */
- protected AttributeStatement buildAttributeStatement(AttributeQueryContext requestContext) throws ProfileException,
- AttributeRequestException {
-
- try {
- AttributeQueryConfiguration profileConfiguration = requestContext.getProfileConfiguration();
- if (profileConfiguration == null) {
- log.error("No SAML 2 attribute query profile configuration is defined for relying party: "
- + requestContext.getRelyingPartyId());
- throw new AttributeRequestException("SAML 2 attribute query is not configured for this relying party");
- }
-
- SAML2AttributeAuthority attributeAuthority = profileConfiguration.getAttributeAuthority();
+ /** {@inheritDoc} */
+ protected void populateRelyingPartyInformation(BaseSAMLProfileRequestContext requestContext)
+ throws ProfileException {
+ super.populateRelyingPartyInformation(requestContext);
- Map<String, BaseAttribute> principalAttributes = attributeAuthority
- .getAttributes(buildAttributeRequestContext(requestContext));
+ EntityDescriptor relyingPartyMetadata = requestContext.getPeerEntityMetadata();
+ if (relyingPartyMetadata != null) {
+ requestContext.setPeerEntityRole(SPSSODescriptor.DEFAULT_ELEMENT_NAME);
+ requestContext.setPeerEntityRoleMetadata(relyingPartyMetadata.getSPSSODescriptor(SAMLConstants.SAML20P_NS));
+ }
+ }
- requestContext.setPrincipalAttributes(principalAttributes);
+ /** {@inheritDoc} */
+ protected void populateAssertingPartyInformation(BaseSAMLProfileRequestContext requestContext)
+ throws ProfileException {
+ super.populateAssertingPartyInformation(requestContext);
- return attributeAuthority.buildAttributeStatement(requestContext.getSamlRequest(), principalAttributes
- .values());
- } catch (AttributeRequestException e) {
- log.error("Error resolving attributes", e);
- throw e;
+ EntityDescriptor localEntityDescriptor = requestContext.getLocalEntityMetadata();
+ if (localEntityDescriptor != null) {
+ requestContext.setLocalEntityRole(AttributeAuthorityDescriptor.DEFAULT_ELEMENT_NAME);
+ requestContext.setLocalEntityRoleMetadata(localEntityDescriptor
+ .getAttributeAuthorityDescriptor(SAMLConstants.SAML20P_NS));
}
}
/**
- * Creates an attribute query context from the current profile request context.
+ * Populates the request context with information from the inbound SAML message.
*
- * @param requestContext current profile request
+ * This method requires the the following request context properties to be populated: inbound saml message
*
- * @return created query context
+ * This methods populates the following request context properties: subject name identifier
+ *
+ * @param requestContext current request context
+ *
+ * @throws ProfileException thrown if the inbound SAML message or subject identifier is null
*/
- protected ShibbolethSAMLAttributeRequestContext<NameID, AttributeQuery> buildAttributeRequestContext(
- AttributeQueryContext requestContext) {
-
- ShibbolethSAMLAttributeRequestContext<NameID, AttributeQuery> queryContext = new ShibbolethSAMLAttributeRequestContext<NameID, AttributeQuery>(
- getMetadataProvider(), requestContext.getRelyingPartyConfiguration(), requestContext.getSamlRequest());
-
- Session userSession = getSessionManager().getSession(getUserSessionId(requestContext.getProfileRequest()));
- if (userSession != null) {
- queryContext.setUserSession(userSession);
- ServiceInformation serviceInfo = userSession.getServiceInformation(requestContext.getRelyingPartyId());
- if (serviceInfo != null) {
- String principalAuthenticationMethod = serviceInfo.getAuthenticationMethod().getAuthenticationMethod();
-
- requestContext.setPrincipalAuthenticationMethod(principalAuthenticationMethod);
- queryContext.setPrincipalAuthenticationMethod(principalAuthenticationMethod);
+ protected void populateSAMLMessageInformation(BaseSAMLProfileRequestContext requestContext) throws ProfileException {
+ AttributeQuery query = (AttributeQuery) requestContext.getInboundSAMLMessage();
+ if (query != null) {
+ Subject subject = query.getSubject();
+ if (subject == null) {
+ log.error("Attribute query did not contain a proper subject");
+ ((AttributeQueryContext) requestContext).setFailureStatus(buildStatus(StatusCode.REQUESTER_URI, null,
+ "Attribute query did not contain a proper subject"));
+ throw new ProfileException("Attribute query did not contain a proper subject");
}
+ requestContext.setSubjectNameIdentifier(subject.getNameID());
}
-
- queryContext.setProfileConfiguration(requestContext.getProfileConfiguration());
- queryContext.setRequest(requestContext.getProfileRequest());
-
- return queryContext;
}
/**
- * Encodes the request's SAML response and writes it to the servlet response.
+ * Selects the appropriate endpoint for the relying party and stores it in the request context.
*
* @param requestContext current request context
*
- * @throws ProfileException thrown if no message encoder is registered for this profiles binding
+ * @return Endpoint selected from the information provided in the request context
*/
- protected void encodeResponse(AttributeQueryContext requestContext) throws ProfileException {
- MessageEncoder<ServletResponse> encoder = getMessageEncoderFactory().getMessageEncoder(BINDING);
- if (encoder == null) {
- throw new ProfileException("No response encoder was registered for binding type: " + BINDING);
+ protected Endpoint selectEndpoint(BaseSAMLProfileRequestContext requestContext) {
+ Endpoint endpoint;
+
+ if (getInboundBinding().equals(SAMLConstants.SAML2_SOAP11_BINDING_URI)) {
+ endpoint = acsEndpointBuilder.buildObject();
+ endpoint.setBinding(SAMLConstants.SAML2_SOAP11_BINDING_URI);
+ } else {
+ BasicEndpointSelector endpointSelector = new BasicEndpointSelector();
+ endpointSelector.setEndpointType(AssertionConsumerService.DEFAULT_ELEMENT_NAME);
+ endpointSelector.setMetadataProvider(getMetadataProvider());
+ endpointSelector.setEntityMetadata(requestContext.getPeerEntityMetadata());
+ endpointSelector.setEntityRoleMetadata(requestContext.getPeerEntityRoleMetadata());
+ endpointSelector.setSamlRequest(requestContext.getInboundSAMLMessage());
+ endpointSelector.getSupportedIssuerBindings().addAll(getSupportedOutboundBindings());
+ endpoint = endpointSelector.selectEndpoint();
}
- super.populateMessageEncoder(encoder);
- encoder.setResponse(requestContext.getProfileResponse().getRawResponse());
- encoder.setSamlMessage(requestContext.getSamlResponse());
- requestContext.setMessageEncoder(encoder);
+ return endpoint;
}
/** Basic data structure used to accumulate information as a request is being processed. */
protected class AttributeQueryContext extends
- SAML2ProfileRequestContext<AttributeQuery, Response, AttributeQueryConfiguration> {
-
- /**
- * Constructor.
- *
- * @param request current profile request
- * @param response current profile response
- */
- public AttributeQueryContext(ProfileRequest<ServletRequest> request, ProfileResponse<ServletResponse> response) {
- super(request, response);
- }
+ BaseSAML2ProfileRequestContext<AttributeQuery, Response, AttributeQueryConfiguration> {
+
}
}
\ No newline at end of file