package edu.internet2.middleware.shibboleth.idp.profile.saml1;
import java.io.IOException;
-import java.io.UnsupportedEncodingException;
-import java.net.URLDecoder;
import java.util.ArrayList;
-import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
-import javax.servlet.http.HttpSession;
-import org.apache.log4j.Logger;
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.saml1.core.AttributeStatement;
import org.opensaml.saml1.core.AuthenticationStatement;
import org.opensaml.saml1.core.Request;
import org.opensaml.saml1.core.Response;
import org.opensaml.saml2.metadata.EntityDescriptor;
import org.opensaml.saml2.metadata.IDPSSODescriptor;
import org.opensaml.saml2.metadata.SPSSODescriptor;
-import org.opensaml.saml2.metadata.provider.MetadataProvider;
-import org.opensaml.saml2.metadata.provider.MetadataProviderException;
-import org.opensaml.ws.message.encoder.MessageEncodingException;
+import org.opensaml.ws.message.decoder.MessageDecodingException;
import org.opensaml.ws.transport.http.HTTPInTransport;
import org.opensaml.ws.transport.http.HTTPOutTransport;
import org.opensaml.ws.transport.http.HttpServletRequestAdapter;
import org.opensaml.ws.transport.http.HttpServletResponseAdapter;
+import org.opensaml.xml.security.SecurityException;
import org.opensaml.xml.util.DatatypeHelper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import edu.internet2.middleware.shibboleth.common.ShibbolethConstants;
import edu.internet2.middleware.shibboleth.common.profile.ProfileException;
+import edu.internet2.middleware.shibboleth.common.profile.provider.BaseSAMLProfileRequestContext;
+import edu.internet2.middleware.shibboleth.common.relyingparty.ProfileConfiguration;
import edu.internet2.middleware.shibboleth.common.relyingparty.RelyingPartyConfiguration;
import edu.internet2.middleware.shibboleth.common.relyingparty.provider.saml1.ShibbolethSSOConfiguration;
-import edu.internet2.middleware.shibboleth.common.relyingparty.provider.saml2.SSOConfiguration;
import edu.internet2.middleware.shibboleth.common.util.HttpHelper;
import edu.internet2.middleware.shibboleth.idp.authn.LoginContext;
import edu.internet2.middleware.shibboleth.idp.authn.ShibbolethSSOLoginContext;
public class ShibbolethSSOProfileHandler extends AbstractSAML1ProfileHandler {
/** Class logger. */
- private final Logger log = Logger.getLogger(ShibbolethSSOProfileHandler.class);
+ private final Logger log = LoggerFactory.getLogger(ShibbolethSSOProfileHandler.class);
/** Builder of AuthenticationStatement objects. */
private SAMLObjectBuilder<AuthenticationStatement> authnStatementBuilder;
/** URL of the authentication manager servlet. */
private String authenticationManagerPath;
- /** URI of SAML 1 bindings supported for outgoing message encoding. */
- private ArrayList<String> supportedOutgoingBindings;
-
/**
* Constructor.
*
* @param authnManagerPath path to the authentication manager servlet
- * @param outgoingBindings URIs of SAML 1 bindings supported for outgoing message encoding
*
* @throws IllegalArgumentException thrown if either the authentication manager path or encoding binding URI are
* null or empty
*/
- public ShibbolethSSOProfileHandler(String authnManagerPath, List<String> outgoingBindings) {
+ public ShibbolethSSOProfileHandler(String authnManagerPath) {
if (DatatypeHelper.isEmpty(authnManagerPath)) {
throw new IllegalArgumentException("Authentication manager path may not be null");
}
authenticationManagerPath = authnManagerPath;
- if (outgoingBindings == null || outgoingBindings.isEmpty()) {
- throw new IllegalArgumentException("List of supported outgoing bindings may not be empty");
- }
- supportedOutgoingBindings = new ArrayList<String>(outgoingBindings);
-
authnStatementBuilder = (SAMLObjectBuilder<AuthenticationStatement>) getBuilderFactory().getBuilder(
AuthenticationStatement.DEFAULT_ELEMENT_NAME);
/** {@inheritDoc} */
public String getProfileId() {
- return "urn:mace:shibboleth:2.0:idp:profiles:shibboleth:request:sso";
+ return ShibbolethSSOConfiguration.PROFILE_ID;
}
/** {@inheritDoc} */
public void processRequest(HTTPInTransport inTransport, HTTPOutTransport outTransport) throws ProfileException {
- if (log.isDebugEnabled()) {
- log.debug("Processing incomming request");
- }
+ log.debug("Processing incoming request");
HttpServletRequest httpRequest = ((HttpServletRequestAdapter) inTransport).getWrappedRequest();
- HttpSession httpSession = httpRequest.getSession();
+ LoginContext loginContext = (LoginContext) httpRequest.getAttribute(LoginContext.LOGIN_CONTEXT_KEY);
- if (httpSession.getAttribute(LoginContext.LOGIN_CONTEXT_KEY) == null) {
- if (log.isDebugEnabled()) {
- log.debug("User session does not contain a login context, processing as first leg of request");
- }
+ if (loginContext == null) {
+ log.debug("Incoming request does not contain a login context, processing as first leg of request");
performAuthentication(inTransport, outTransport);
} else {
- if (log.isDebugEnabled()) {
- log.debug("User session contains a login context, processing as second leg of request");
- }
+ log.debug("Incoming request contains a login context, processing as second leg of request");
completeAuthenticationRequest(inTransport, outTransport);
}
}
HttpServletRequest httpRequest = ((HttpServletRequestAdapter) inTransport).getWrappedRequest();
HttpServletResponse httpResponse = ((HttpServletResponseAdapter) outTransport).getWrappedResponse();
- HttpSession httpSession = httpRequest.getSession(true);
- LoginContext loginContext = buildLoginContext(httpRequest);
- if (getRelyingPartyConfiguration(loginContext.getRelyingPartyId()) == null) {
+ ShibbolethSSORequestContext requestContext = decodeRequest(inTransport, outTransport);
+ ShibbolethSSOLoginContext loginContext = requestContext.getLoginContext();
+
+ RelyingPartyConfiguration rpConfig = getRelyingPartyConfiguration(loginContext.getRelyingPartyId());
+ ProfileConfiguration ssoConfig = rpConfig.getProfileConfiguration(ShibbolethSSOConfiguration.PROFILE_ID);
+ if (ssoConfig == null) {
log.error("Shibboleth SSO profile is not configured for relying party " + loginContext.getRelyingPartyId());
throw new ProfileException("Shibboleth SSO profile is not configured for relying party "
+ loginContext.getRelyingPartyId());
}
+ loginContext.getRequestedAuthenticationMethods().add(rpConfig.getDefaultAuthenticationMethod());
- httpSession.setAttribute(LoginContext.LOGIN_CONTEXT_KEY, loginContext);
+ httpRequest.setAttribute(LoginContext.LOGIN_CONTEXT_KEY, loginContext);
try {
RequestDispatcher dispatcher = httpRequest.getRequestDispatcher(authenticationManagerPath);
}
/**
+ * Decodes an incoming request and populates a created request context with the resultant information.
+ *
+ * @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
+ */
+ protected ShibbolethSSORequestContext decodeRequest(HTTPInTransport inTransport, HTTPOutTransport outTransport)
+ throws ProfileException {
+ log.debug("Decoding message with decoder binding {}", getInboundBinding());
+
+ HttpServletRequest httpRequest = ((HttpServletRequestAdapter) inTransport).getWrappedRequest();
+
+ ShibbolethSSORequestContext requestContext = new ShibbolethSSORequestContext();
+ requestContext.setMetadataProvider(getMetadataProvider());
+ requestContext.setSecurityPolicyResolver(getSecurityPolicyResolver());
+
+ requestContext.setCommunicationProfileId(ShibbolethSSOConfiguration.PROFILE_ID);
+ requestContext.setInboundMessageTransport(inTransport);
+ requestContext.setInboundSAMLProtocol(ShibbolethConstants.SHIB_SSO_PROFILE_URI);
+ requestContext.setPeerEntityRole(SPSSODescriptor.DEFAULT_ELEMENT_NAME);
+
+ requestContext.setOutboundMessageTransport(outTransport);
+ requestContext.setOutboundSAMLProtocol(SAMLConstants.SAML11P_NS);
+
+ SAMLMessageDecoder decoder = getMessageDecoders().get(getInboundBinding());
+ requestContext.setMessageDecoder(decoder);
+ try {
+ decoder.decode(requestContext);
+ } catch (MessageDecodingException e) {
+ log.error("Error decoding Shibboleth SSO request", e);
+ throw new ProfileException("Error decoding Shibboleth SSO request", e);
+ } catch (SecurityException e) {
+ log.error("Shibboleth SSO request does not meet security requirements", e);
+ throw new ProfileException("Shibboleth SSO request does not meet security requirements", e);
+ }
+
+ ShibbolethSSOLoginContext loginContext = new ShibbolethSSOLoginContext();
+ loginContext.setRelyingParty(requestContext.getInboundMessageIssuer());
+ loginContext.setSpAssertionConsumerService(requestContext.getSpAssertionConsumerService());
+ loginContext.setSpTarget(requestContext.getRelayState());
+ loginContext.setAuthenticationEngineURL(authenticationManagerPath);
+ loginContext.setProfileHandlerURL(HttpHelper.getRequestUriWithoutContext(httpRequest));
+
+ requestContext.setLoginContext(loginContext);
+
+ return requestContext;
+ }
+
+ /**
* Creates a response to the Shibboleth SSO and sends the user, with response in tow, back to the relying party
* after they've been authenticated.
*
protected void completeAuthenticationRequest(HTTPInTransport inTransport, HTTPOutTransport outTransport)
throws ProfileException {
HttpServletRequest httpRequest = ((HttpServletRequestAdapter) inTransport).getWrappedRequest();
- HttpSession httpSession = httpRequest.getSession(true);
-
- ShibbolethSSOLoginContext loginContext = (ShibbolethSSOLoginContext) httpSession
+ ShibbolethSSOLoginContext loginContext = (ShibbolethSSOLoginContext) httpRequest
.getAttribute(LoginContext.LOGIN_CONTEXT_KEY);
- httpSession.removeAttribute(LoginContext.LOGIN_CONTEXT_KEY);
ShibbolethSSORequestContext requestContext = buildRequestContext(loginContext, inTransport, outTransport);
Response samlResponse;
try {
- if (loginContext.getPrincipalName() == null) {
+ if (loginContext.getAuthenticationFailure() != null) {
+ log.error("User authentication failed with the following error: {}", loginContext
+ .getAuthenticationFailure().toString());
requestContext.setFailureStatus(buildStatus(StatusCode.RESPONDER, null, "User failed authentication"));
- throw new ProfileException("User failed authentication");
+ throw new ProfileException("Authentication failure", loginContext.getAuthenticationFailure());
}
resolveAttributes(requestContext);
ArrayList<Statement> statements = new ArrayList<Statement>();
statements.add(buildAuthenticationStatement(requestContext));
if (requestContext.getProfileConfiguration().includeAttributeStatement()) {
- statements.add(buildAttributeStatement(requestContext, "urn:oasis:names:tc:SAML:1.0:cm:bearer"));
+ AttributeStatement attributeStatement = buildAttributeStatement(requestContext,
+ "urn:oasis:names:tc:SAML:1.0:cm:bearer");
+ if (attributeStatement != null) {
+ requestContext.setRequestedAttributes(requestContext.getAttributes().keySet());
+ statements.add(attributeStatement);
+ }
}
samlResponse = buildResponse(requestContext, statements);
}
/**
- * Creates a login context from the incoming HTTP request.
- *
- * @param request current HTTP request
- *
- * @return the constructed login context
- *
- * @throws ProfileException thrown if the incomming request did not contain a providerId, shire, and target
- * parameter
- */
- protected ShibbolethSSOLoginContext buildLoginContext(HttpServletRequest request) throws ProfileException {
- ShibbolethSSOLoginContext loginContext = new ShibbolethSSOLoginContext();
-
- try {
- String providerId = DatatypeHelper.safeTrimOrNullString(request.getParameter("providerId"));
- if (providerId == null) {
- log.error("No providerId parameter in Shibboleth SSO request");
- throw new ProfileException("No providerId parameter in Shibboleth SSO request");
- }
- loginContext.setRelyingParty(URLDecoder.decode(providerId, "UTF-8"));
-
- RelyingPartyConfiguration rpConfig = getRelyingPartyConfiguration(providerId);
- if (rpConfig == null) {
- log.error("No relying party configuration available for " + providerId);
- throw new ProfileException("No relying party configuration available for " + providerId);
- }
- loginContext.getRequestedAuthenticationMethods().add(rpConfig.getDefaultAuthenticationMethod());
-
- String acs = DatatypeHelper.safeTrimOrNullString(request.getParameter("shire"));
- if (acs == null) {
- log.error("No shire parameter in Shibboleth SSO request");
- throw new ProfileException("No shire parameter in Shibboleth SSO request");
- }
- loginContext.setSpAssertionConsumerService(URLDecoder.decode(acs, "UTF-8"));
-
- String target = DatatypeHelper.safeTrimOrNullString(request.getParameter("target"));
- if (target == null) {
- log.error("No target parameter in Shibboleth SSO request");
- throw new ProfileException("No target parameter in Shibboleth SSO request");
- }
- loginContext.setSpTarget(URLDecoder.decode(target, "UTF-8"));
- } catch (UnsupportedEncodingException e) {
- // UTF-8 encoding required to be supported by all JVMs.
- }
-
- loginContext.setAuthenticationEngineURL(authenticationManagerPath);
- loginContext.setProfileHandlerURL(HttpHelper.getRequestUriWithoutContext(request));
- return loginContext;
- }
-
- /**
* Creates an authentication request context from the current environmental information.
*
* @param loginContext current login context
HTTPInTransport in, HTTPOutTransport out) throws ProfileException {
ShibbolethSSORequestContext requestContext = new ShibbolethSSORequestContext();
- try {
- requestContext.setLoginContext(loginContext);
- requestContext.setPrincipalName(loginContext.getPrincipalName());
- requestContext.setPrincipalAuthenticationMethod(loginContext.getAuthenticationMethod());
- requestContext.setUserSession(getUserSession(in));
- requestContext.setRelayState(loginContext.getSpTarget());
-
- requestContext.setMessageInTransport(in);
- requestContext.setInboundSAMLProtocol(ShibbolethConstants.SHIB_SSO_PROFILE_URI);
-
- MetadataProvider metadataProvider = getMetadataProvider();
- requestContext.setMetadataProvider(metadataProvider);
-
- String relyingPartyId = loginContext.getRelyingPartyId();
- requestContext.setRelyingPartyEntityId(relyingPartyId);
- EntityDescriptor relyingPartyMetadata = metadataProvider.getEntityDescriptor(relyingPartyId);
- requestContext.setPeerEntityMetadata(relyingPartyMetadata);
+ requestContext.setMessageDecoder(getMessageDecoders().get(getInboundBinding()));
+
+ requestContext.setLoginContext(loginContext);
+ requestContext.setRelayState(loginContext.getSpTarget());
+
+ requestContext.setInboundMessageTransport(in);
+ requestContext.setInboundSAMLProtocol(ShibbolethConstants.SHIB_SSO_PROFILE_URI);
+
+ requestContext.setOutboundMessageTransport(out);
+ requestContext.setOutboundSAMLProtocol(SAMLConstants.SAML20P_NS);
+
+ requestContext.setMetadataProvider(getMetadataProvider());
+
+ String relyingPartyId = loginContext.getRelyingPartyId();
+ requestContext.setInboundMessageIssuer(relyingPartyId);
+
+ populateRequestContext(requestContext);
+
+ return requestContext;
+ }
+
+ /** {@inheritDoc} */
+ protected void populateRelyingPartyInformation(BaseSAMLProfileRequestContext requestContext)
+ throws ProfileException {
+ super.populateRelyingPartyInformation(requestContext);
+
+ EntityDescriptor relyingPartyMetadata = requestContext.getPeerEntityMetadata();
+ if (relyingPartyMetadata != null) {
requestContext.setPeerEntityRole(SPSSODescriptor.DEFAULT_ELEMENT_NAME);
- requestContext.setPeerEntityRoleMetadata(relyingPartyMetadata
- .getSPSSODescriptor(SAMLConstants.SAML11P_NS));
- RelyingPartyConfiguration rpConfig = getRelyingPartyConfiguration(relyingPartyId);
- requestContext.setRelyingPartyConfiguration(rpConfig);
- requestContext.setPeerEntityEndpoint(selectEndpoint(requestContext));
-
- String assertingPartyId = rpConfig.getProviderId();
- requestContext.setAssertingPartyEntityId(assertingPartyId);
- EntityDescriptor assertingPartyMetadata = metadataProvider.getEntityDescriptor(assertingPartyId);
- requestContext.setLocalEntityMetadata(assertingPartyMetadata);
- requestContext.setLocalEntityRole(IDPSSODescriptor.DEFAULT_ELEMENT_NAME);
- requestContext.setLocalEntityRoleMetadata(assertingPartyMetadata
- .getIDPSSODescriptor(SAMLConstants.SAML20P_NS));
+ requestContext.setPeerEntityRoleMetadata(relyingPartyMetadata.getSPSSODescriptor(SAMLConstants.SAML11P_NS));
+ }
+ }
- requestContext.setMessageOutTransport(out);
- requestContext.setOutboundSAMLProtocol(SAMLConstants.SAML20P_NS);
- ShibbolethSSOConfiguration profileConfig = (ShibbolethSSOConfiguration) rpConfig
- .getProfileConfiguration(SSOConfiguration.PROFILE_ID);
- requestContext.setProfileConfiguration(profileConfig);
- if (profileConfig.getSigningCredential() != null) {
- requestContext.setOutboundSAMLMessageSigningCredential(profileConfig.getSigningCredential());
- } else if (rpConfig.getDefaultSigningCredential() != null) {
- requestContext.setOutboundSAMLMessageSigningCredential(rpConfig.getDefaultSigningCredential());
- }
+ /** {@inheritDoc} */
+ protected void populateAssertingPartyInformation(BaseSAMLProfileRequestContext requestContext)
+ throws ProfileException {
+ super.populateAssertingPartyInformation(requestContext);
- return requestContext;
- } catch (MetadataProviderException e) {
- log.error("Unable to locate metadata for asserting or relying party");
- requestContext.setFailureStatus(buildStatus(StatusCode.RESPONDER, null, "Error locating party metadata"));
- throw new ProfileException("Error locating party metadata");
+ EntityDescriptor localEntityDescriptor = requestContext.getLocalEntityMetadata();
+ if (localEntityDescriptor != null) {
+ requestContext.setLocalEntityRole(IDPSSODescriptor.DEFAULT_ELEMENT_NAME);
+ requestContext.setLocalEntityRoleMetadata(localEntityDescriptor
+ .getIDPSSODescriptor(SAMLConstants.SAML20P_NS));
}
}
+ /** {@inheritDoc} */
+ protected void populateSAMLMessageInformation(BaseSAMLProfileRequestContext requestContext)
+ throws ProfileException {
+ // nothing to do here
+ }
+
/**
* Selects the appropriate endpoint for the relying party and stores it in the request context.
*
*
* @return Endpoint selected from the information provided in the request context
*/
- protected Endpoint selectEndpoint(ShibbolethSSORequestContext requestContext) {
- ShibbolethSSOLoginContext loginContext = requestContext.getLoginContext();
-
- if (loginContext.getSpAssertionConsumerService() != null) {
- SAMLObjectBuilder<AssertionConsumerService> acsBuilder = (SAMLObjectBuilder<AssertionConsumerService>) getBuilderFactory()
- .getBuilder(AssertionConsumerService.DEFAULT_ELEMENT_NAME);
- AssertionConsumerService acsEndpoint = acsBuilder.buildObject();
- acsEndpoint.setBinding(getMessageEncoder().getBindingURI());
- acsEndpoint.setLocation(loginContext.getSpAssertionConsumerService());
- return acsEndpoint;
- }
+ protected Endpoint selectEndpoint(BaseSAMLProfileRequestContext requestContext) {
+ ShibbolethSSOLoginContext loginContext = ((ShibbolethSSORequestContext) requestContext).getLoginContext();
- BasicEndpointSelector endpointSelector = new BasicEndpointSelector();
+ ShibbolethSSOEndpointSelector endpointSelector = new ShibbolethSSOEndpointSelector();
+ endpointSelector.setSpAssertionConsumerService(loginContext.getSpAssertionConsumerService());
endpointSelector.setEndpointType(AssertionConsumerService.DEFAULT_ELEMENT_NAME);
endpointSelector.setMetadataProvider(getMetadataProvider());
- endpointSelector.setRelyingParty(requestContext.getPeerEntityMetadata());
- endpointSelector.setRelyingPartyRole(requestContext.getPeerEntityRoleMetadata());
+ endpointSelector.setEntityMetadata(requestContext.getPeerEntityMetadata());
+ endpointSelector.setEntityRoleMetadata(requestContext.getPeerEntityRoleMetadata());
endpointSelector.setSamlRequest(requestContext.getInboundSAMLMessage());
- endpointSelector.getSupportedIssuerBindings().addAll(supportedOutgoingBindings);
+ endpointSelector.getSupportedIssuerBindings().addAll(getSupportedOutboundBindings());
+
return endpointSelector.selectEndpoint();
}
protected SubjectLocality buildSubjectLocality(ShibbolethSSORequestContext requestContext) {
SubjectLocality subjectLocality = subjectLocalityBuilder.buildObject();
- HTTPInTransport inTransport = (HTTPInTransport) requestContext.getMessageInTransport();
+ HTTPInTransport inTransport = (HTTPInTransport) requestContext.getInboundMessageTransport();
subjectLocality.setIPAddress(inTransport.getPeerAddress());
- subjectLocality.setDNSAddress(inTransport.getPeerDomainName());
return subjectLocality;
}
- /**
- * Encodes the request's SAML response and writes it to the servlet response.
- *
- * @param requestContext current request context
- *
- * @throws ProfileException thrown if no message encoder is registered for this profiles binding
- */
- protected void encodeResponse(ShibbolethSSORequestContext requestContext) throws ProfileException {
- if (log.isDebugEnabled()) {
- log.debug("Encoding response to SAML request " + requestContext.getInboundSAMLMessageId()
- + " from relying party " + requestContext.getRelyingPartyEntityId());
- }
-
- try {
- getMessageEncoder().encode(requestContext);
- } catch (MessageEncodingException e) {
- throw new ProfileException("Unable to encode response to relying party: "
- + requestContext.getRelyingPartyEntityId(), e);
- }
- }
-
/** Represents the internal state of a Shibboleth SSO Request while it's being processed by the IdP. */
- protected class ShibbolethSSORequestContext extends
+ public class ShibbolethSSORequestContext extends
BaseSAML1ProfileRequestContext<Request, Response, ShibbolethSSOConfiguration> {
+ /** SP-provide assertion consumer service URL. */
+ private String spAssertionConsumerService;
+
/** Current login context. */
private ShibbolethSSOLoginContext loginContext;
public void setLoginContext(ShibbolethSSOLoginContext context) {
loginContext = context;
}
+
+ /**
+ * Gets the SP-provided assertion consumer service URL.
+ *
+ * @return SP-provided assertion consumer service URL
+ */
+ public String getSpAssertionConsumerService() {
+ return spAssertionConsumerService;
+ }
+
+ /**
+ * Sets the SP-provided assertion consumer service URL.
+ *
+ * @param acs SP-provided assertion consumer service URL
+ */
+ public void setSpAssertionConsumerService(String acs) {
+ spAssertionConsumerService = acs;
+ }
}
}
\ No newline at end of file