2 * Copyright [2007] [University Corporation for Advanced Internet Development, Inc.]
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package edu.internet2.middleware.shibboleth.idp.profile;
19 import java.util.List;
22 import javax.servlet.http.HttpServletRequest;
24 import org.opensaml.common.IdentifierGenerator;
25 import org.opensaml.common.binding.decoding.SAMLMessageDecoder;
26 import org.opensaml.common.binding.encoding.SAMLMessageEncoder;
27 import org.opensaml.saml2.metadata.Endpoint;
28 import org.opensaml.saml2.metadata.provider.MetadataProvider;
29 import org.opensaml.ws.message.encoder.MessageEncodingException;
30 import org.opensaml.ws.security.SecurityPolicyResolver;
31 import org.opensaml.ws.transport.InTransport;
32 import org.opensaml.ws.transport.http.HttpServletRequestAdapter;
33 import org.opensaml.xml.security.credential.Credential;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 import edu.internet2.middleware.shibboleth.common.log.AuditLogEntry;
38 import edu.internet2.middleware.shibboleth.common.profile.ProfileException;
39 import edu.internet2.middleware.shibboleth.common.profile.provider.AbstractShibbolethProfileHandler;
40 import edu.internet2.middleware.shibboleth.common.profile.provider.BaseSAMLProfileRequestContext;
41 import edu.internet2.middleware.shibboleth.common.relyingparty.RelyingPartySecurityPolicyResolver;
42 import edu.internet2.middleware.shibboleth.common.relyingparty.provider.AbstractSAMLProfileConfiguration;
43 import edu.internet2.middleware.shibboleth.common.relyingparty.provider.CryptoOperationRequirementLevel;
44 import edu.internet2.middleware.shibboleth.common.relyingparty.provider.SAMLMDRelyingPartyConfigurationManager;
45 import edu.internet2.middleware.shibboleth.idp.session.Session;
48 * Base class for SAML profile handlers.
50 public abstract class AbstractSAMLProfileHandler extends
51 AbstractShibbolethProfileHandler<SAMLMDRelyingPartyConfigurationManager, Session> {
53 /** SAML message audit log. */
54 private final Logger auditLog = LoggerFactory.getLogger(AuditLogEntry.AUDIT_LOGGER_NAME);
57 private final Logger log = LoggerFactory.getLogger(AbstractSAMLProfileHandler.class);
59 /** Generator of IDs which may be used for SAML assertions, requests, etc. */
60 private IdentifierGenerator idGenerator;
62 /** All the SAML message decoders configured for the IdP. */
63 private Map<String, SAMLMessageDecoder> messageDecoders;
65 /** All the SAML message encoders configured for the IdP. */
66 private Map<String, SAMLMessageEncoder> messageEncoders;
68 /** SAML message binding used by inbound messages. */
69 private String inboundBinding;
71 /** SAML message bindings that may be used by outbound messages. */
72 private List<String> supportedOutboundBindings;
74 /** Resolver used to determine active security policy for an incoming request. */
75 private SecurityPolicyResolver securityPolicyResolver;
78 protected AbstractSAMLProfileHandler() {
83 * Gets the resolver used to determine active security policy for an incoming request.
85 * @return resolver used to determine active security policy for an incoming request
87 public SecurityPolicyResolver getSecurityPolicyResolver() {
88 if (securityPolicyResolver == null) {
89 setSecurityPolicyResolver(new RelyingPartySecurityPolicyResolver(getRelyingPartyConfigurationManager()));
92 return securityPolicyResolver;
96 * Sets the resolver used to determine active security policy for an incoming request.
98 * @param resolver resolver used to determine active security policy for an incoming request
100 public void setSecurityPolicyResolver(SecurityPolicyResolver resolver) {
101 securityPolicyResolver = resolver;
105 * Gets the audit log for this handler.
107 * @return audit log for this handler
109 protected Logger getAduitLog() {
114 * Gets an ID generator which may be used for SAML assertions, requests, etc.
116 * @return ID generator
118 public IdentifierGenerator getIdGenerator() {
123 * Gets the SAML message binding used by inbound messages.
125 * @return SAML message binding used by inbound messages
127 public String getInboundBinding() {
128 return inboundBinding;
132 * Gets all the SAML message decoders configured for the IdP indexed by SAML binding URI.
134 * @return SAML message decoders configured for the IdP indexed by SAML binding URI
136 public Map<String, SAMLMessageDecoder> getMessageDecoders() {
137 return messageDecoders;
141 * Gets all the SAML message encoders configured for the IdP indexed by SAML binding URI.
143 * @return SAML message encoders configured for the IdP indexed by SAML binding URI
145 public Map<String, SAMLMessageEncoder> getMessageEncoders() {
146 return messageEncoders;
150 * A convenience method for retrieving the SAML metadata provider from the relying party manager.
152 * @return the metadata provider or null
154 public MetadataProvider getMetadataProvider() {
155 SAMLMDRelyingPartyConfigurationManager rpcManager = getRelyingPartyConfigurationManager();
156 if (rpcManager != null) {
157 return rpcManager.getMetadataProvider();
164 * Gets the SAML message bindings that may be used by outbound messages.
166 * @return SAML message bindings that may be used by outbound messages
168 public List<String> getSupportedOutboundBindings() {
169 return supportedOutboundBindings;
173 * Gets the user's session, if there is one.
175 * @param inTransport current inbound transport
177 * @return user's session
179 protected Session getUserSession(InTransport inTransport) {
180 HttpServletRequest rawRequest = ((HttpServletRequestAdapter) inTransport).getWrappedRequest();
181 return (Session) rawRequest.getAttribute(Session.HTTP_SESSION_BINDING_ATTRIBUTE);
185 * Gets an ID generator which may be used for SAML assertions, requests, etc.
187 * @param generator an ID generator which may be used for SAML assertions, requests, etc
189 public void setIdGenerator(IdentifierGenerator generator) {
190 idGenerator = generator;
194 * Sets the SAML message binding used by inbound messages.
196 * @param binding SAML message binding used by inbound messages
198 public void setInboundBinding(String binding) {
199 inboundBinding = binding;
203 * Sets all the SAML message decoders configured for the IdP indexed by SAML binding URI.
205 * @param decoders SAML message decoders configured for the IdP indexed by SAML binding URI
207 public void setMessageDecoders(Map<String, SAMLMessageDecoder> decoders) {
208 messageDecoders = decoders;
212 * Sets all the SAML message encoders configured for the IdP indexed by SAML binding URI.
214 * @param encoders SAML message encoders configured for the IdP indexed by SAML binding URI
216 public void setMessageEncoders(Map<String, SAMLMessageEncoder> encoders) {
217 messageEncoders = encoders;
221 * Sets the SAML message bindings that may be used by outbound messages.
223 * @param bindings SAML message bindings that may be used by outbound messages
225 public void setSupportedOutboundBindings(List<String> bindings) {
226 supportedOutboundBindings = bindings;
230 * Encodes the request's SAML response and writes it to the servlet response.
232 * @param requestContext current request context
234 * @throws ProfileException thrown if no message encoder is registered for this profiles binding
236 protected void encodeResponse(BaseSAMLProfileRequestContext requestContext) throws ProfileException {
239 Endpoint peerEndpoint = requestContext.getPeerEntityEndpoint();
240 if (peerEndpoint == null) {
242 .error("No return endpoint available for relying party {}", requestContext
243 .getInboundMessageIssuer());
244 throw new ProfileException("No peer endpoint available to which to send SAML response");
247 SAMLMessageEncoder encoder = getMessageEncoders().get(requestContext.getPeerEntityEndpoint().getBinding());
248 if (encoder == null) {
249 log.error("No outbound message encoder configured for binding {}", requestContext
250 .getPeerEntityEndpoint().getBinding());
251 throw new ProfileException("No outbound message encoder configured for binding "
252 + requestContext.getPeerEntityEndpoint().getBinding());
255 AbstractSAMLProfileConfiguration profileConfig = (AbstractSAMLProfileConfiguration) requestContext
256 .getProfileConfiguration();
257 if (profileConfig.getSignResponses() == CryptoOperationRequirementLevel.always
258 || (profileConfig.getSignResponses() == CryptoOperationRequirementLevel.conditional && !encoder
259 .providesMessageIntegrity(requestContext))) {
260 Credential signingCredential = null;
261 if (profileConfig.getSigningCredential() != null) {
262 signingCredential = profileConfig.getSigningCredential();
263 } else if (requestContext.getRelyingPartyConfiguration().getDefaultSigningCredential() != null) {
264 signingCredential = requestContext.getRelyingPartyConfiguration().getDefaultSigningCredential();
267 if (signingCredential == null) {
268 throw new ProfileException(
269 "Signing of responses is required but no signing credential is available");
272 requestContext.setOutboundSAMLMessageSigningCredential(signingCredential);
275 log.debug("Encoding response to SAML request {} from relying party {}", requestContext
276 .getInboundSAMLMessageId(), requestContext.getInboundMessageIssuer());
278 requestContext.setMessageEncoder(encoder);
279 encoder.encode(requestContext);
280 } catch (MessageEncodingException e) {
281 throw new ProfileException("Unable to encode response to relying party: "
282 + requestContext.getInboundMessageIssuer(), e);
287 * Writes an aduit log entry indicating the successful response to the attribute request.
289 * @param context current request context
291 protected void writeAuditLogEntry(BaseSAMLProfileRequestContext context) {
292 AuditLogEntry auditLogEntry = new AuditLogEntry();
293 auditLogEntry.setMessageProfile(getProfileId());
294 auditLogEntry.setPrincipalAuthenticationMethod(context.getPrincipalAuthenticationMethod());
295 auditLogEntry.setPrincipalName(context.getPrincipalName());
296 auditLogEntry.setAssertingPartyId(context.getLocalEntityId());
297 auditLogEntry.setRelyingPartyId(context.getInboundMessageIssuer());
298 auditLogEntry.setRequestBinding(context.getMessageDecoder().getBindingURI());
299 auditLogEntry.setRequestId(context.getInboundSAMLMessageId());
300 auditLogEntry.setResponseBinding(context.getMessageEncoder().getBindingURI());
301 auditLogEntry.setResponseId(context.getOutboundSAMLMessageId());
302 if (context.getReleasedAttributes() != null) {
303 auditLogEntry.getReleasedAttributes().addAll(context.getReleasedAttributes());
306 getAduitLog().info(auditLogEntry.toString());