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.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 import edu.internet2.middleware.shibboleth.common.log.AuditLogEntry;
37 import edu.internet2.middleware.shibboleth.common.profile.ProfileException;
38 import edu.internet2.middleware.shibboleth.common.profile.provider.AbstractShibbolethProfileHandler;
39 import edu.internet2.middleware.shibboleth.common.profile.provider.BaseSAMLProfileRequestContext;
40 import edu.internet2.middleware.shibboleth.common.relyingparty.RelyingPartySecurityPolicyResolver;
41 import edu.internet2.middleware.shibboleth.common.relyingparty.provider.SAMLMDRelyingPartyConfigurationManager;
42 import edu.internet2.middleware.shibboleth.idp.session.Session;
45 * Base class for SAML profile handlers.
47 public abstract class AbstractSAMLProfileHandler extends
48 AbstractShibbolethProfileHandler<SAMLMDRelyingPartyConfigurationManager, Session> {
50 /** SAML message audit log. */
51 private final Logger auditLog = LoggerFactory.getLogger(AuditLogEntry.AUDIT_LOGGER_NAME);
54 private final Logger log = LoggerFactory.getLogger(AbstractSAMLProfileHandler.class);
56 /** Generator of IDs which may be used for SAML assertions, requests, etc. */
57 private IdentifierGenerator idGenerator;
59 /** All the SAML message decoders configured for the IdP. */
60 private Map<String, SAMLMessageDecoder> messageDecoders;
62 /** All the SAML message encoders configured for the IdP. */
63 private Map<String, SAMLMessageEncoder> messageEncoders;
65 /** SAML message binding used by inbound messages. */
66 private String inboundBinding;
68 /** SAML message bindings that may be used by outbound messages. */
69 private List<String> supportedOutboundBindings;
71 /** Resolver used to determine active security policy for an incoming request. */
72 private SecurityPolicyResolver securityPolicyResolver;
75 protected AbstractSAMLProfileHandler() {
81 * Gets the resolver used to determine active security policy for an incoming request.
83 * @return resolver used to determine active security policy for an incoming request
85 public SecurityPolicyResolver getSecurityPolicyResolver() {
86 if(securityPolicyResolver == null){
87 setSecurityPolicyResolver(new RelyingPartySecurityPolicyResolver(getRelyingPartyConfigurationManager()));
90 return securityPolicyResolver;
94 * Sets the resolver used to determine active security policy for an incoming request.
96 * @param resolver resolver used to determine active security policy for an incoming request
98 public void setSecurityPolicyResolver(SecurityPolicyResolver resolver) {
99 securityPolicyResolver = resolver;
103 * Gets the audit log for this handler.
105 * @return audit log for this handler
107 protected Logger getAduitLog() {
112 * Gets an ID generator which may be used for SAML assertions, requests, etc.
114 * @return ID generator
116 public IdentifierGenerator getIdGenerator() {
121 * Gets the SAML message binding used by inbound messages.
123 * @return SAML message binding used by inbound messages
125 public String getInboundBinding() {
126 return inboundBinding;
130 * Gets all the SAML message decoders configured for the IdP indexed by SAML binding URI.
132 * @return SAML message decoders configured for the IdP indexed by SAML binding URI
134 public Map<String, SAMLMessageDecoder> getMessageDecoders() {
135 return messageDecoders;
139 * Gets all the SAML message encoders configured for the IdP indexed by SAML binding URI.
141 * @return SAML message encoders configured for the IdP indexed by SAML binding URI
143 public Map<String, SAMLMessageEncoder> getMessageEncoders() {
144 return messageEncoders;
148 * A convenience method for retrieving the SAML metadata provider from the relying party manager.
150 * @return the metadata provider or null
152 public MetadataProvider getMetadataProvider() {
153 SAMLMDRelyingPartyConfigurationManager rpcManager = getRelyingPartyConfigurationManager();
154 if (rpcManager != null) {
155 return rpcManager.getMetadataProvider();
162 * Gets the SAML message bindings that may be used by outbound messages.
164 * @return SAML message bindings that may be used by outbound messages
166 public List<String> getSupportedOutboundBindings() {
167 return supportedOutboundBindings;
171 * Gets the user's session, if there is one.
173 * @param inTransport current inbound transport
175 * @return user's session
177 protected Session getUserSession(InTransport inTransport) {
178 String sessionId = getUserSessionId(inTransport);
179 return getSessionManager().getSession(sessionId);
183 * Gets the user's session ID from the current request.
185 * @param inTransport current inbound transport
187 * @return user's session ID
189 protected String getUserSessionId(InTransport inTransport) {
190 HttpServletRequest rawRequest = ((HttpServletRequestAdapter) inTransport).getWrappedRequest();
192 if (rawRequest != null) {
193 return (String) rawRequest.getSession().getAttribute(Session.HTTP_SESSION_BINDING_ATTRIBUTE);
200 * Gets an ID generator which may be used for SAML assertions, requests, etc.
202 * @param generator an ID generator which may be used for SAML assertions, requests, etc
204 public void setIdGenerator(IdentifierGenerator generator) {
205 idGenerator = generator;
209 * Sets the SAML message binding used by inbound messages.
211 * @param binding SAML message binding used by inbound messages
213 public void setInboundBinding(String binding) {
214 inboundBinding = binding;
218 * Sets all the SAML message decoders configured for the IdP indexed by SAML binding URI.
220 * @param decoders SAML message decoders configured for the IdP indexed by SAML binding URI
222 public void setMessageDecoders(Map<String, SAMLMessageDecoder> decoders) {
223 messageDecoders = decoders;
227 * Sets all the SAML message encoders configured for the IdP indexed by SAML binding URI.
229 * @param encoders SAML message encoders configured for the IdP indexed by SAML binding URI
231 public void setMessageEncoders(Map<String, SAMLMessageEncoder> encoders) {
232 messageEncoders = encoders;
236 * Sets the SAML message bindings that may be used by outbound messages.
238 * @param bindings SAML message bindings that may be used by outbound messages
240 public void setSupportedOutboundBindings(List<String> bindings) {
241 supportedOutboundBindings = bindings;
245 * Encodes the request's SAML response and writes it to the servlet response.
247 * @param requestContext current request context
249 * @throws ProfileException thrown if no message encoder is registered for this profiles binding
251 protected void encodeResponse(BaseSAMLProfileRequestContext requestContext) throws ProfileException {
253 Endpoint peerEndpoint = requestContext.getPeerEntityEndpoint();
254 if (peerEndpoint == null) {
255 log.error("No return endpoint available for relying party {}", requestContext
256 .getInboundMessageIssuer());
257 throw new ProfileException("No peer endpoint available to which to send SAML response");
260 SAMLMessageEncoder encoder = getMessageEncoders().get(requestContext.getPeerEntityEndpoint().getBinding());
261 if (encoder == null) {
262 log.error("No outbound message encoder configured for binding {}", requestContext
263 .getPeerEntityEndpoint().getBinding());
264 throw new ProfileException("No outbound message encoder configured for binding "
265 + requestContext.getPeerEntityEndpoint().getBinding());
267 log.debug("Encoding response to SAML request {} from relying party {}", requestContext
268 .getInboundSAMLMessageId(), requestContext.getInboundMessageIssuer());
270 requestContext.setMessageEncoder(encoder);
271 encoder.encode(requestContext);
272 } catch (MessageEncodingException e) {
273 throw new ProfileException("Unable to encode response to relying party: "
274 + requestContext.getInboundMessageIssuer(), e);
279 * Writes an aduit log entry indicating the successful response to the attribute request.
281 * @param context current request context
283 protected void writeAuditLogEntry(BaseSAMLProfileRequestContext context) {
284 AuditLogEntry auditLogEntry = new AuditLogEntry();
285 auditLogEntry.setMessageProfile(getProfileId());
286 auditLogEntry.setPrincipalAuthenticationMethod(context.getPrincipalAuthenticationMethod());
287 auditLogEntry.setPrincipalName(context.getPrincipalName());
288 auditLogEntry.setAssertingPartyId(context.getLocalEntityId());
289 auditLogEntry.setRelyingPartyId(context.getInboundMessageIssuer());
290 auditLogEntry.setRequestBinding(context.getMessageDecoder().getBindingURI());
291 auditLogEntry.setRequestId(context.getInboundSAMLMessageId());
292 auditLogEntry.setResponseBinding(context.getMessageEncoder().getBindingURI());
293 auditLogEntry.setResponseId(context.getOutboundSAMLMessageId());
294 if (context.getReleasedAttributes() != null) {
295 auditLogEntry.getReleasedAttributes().addAll(context.getReleasedAttributes());
298 getAduitLog().info(auditLogEntry.toString());