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.saml1;
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.List;
24 import javax.xml.namespace.QName;
26 import org.joda.time.DateTime;
27 import org.opensaml.Configuration;
28 import org.opensaml.common.SAMLObject;
29 import org.opensaml.common.SAMLObjectBuilder;
30 import org.opensaml.common.SAMLVersion;
31 import org.opensaml.saml1.core.Assertion;
32 import org.opensaml.saml1.core.AttributeQuery;
33 import org.opensaml.saml1.core.AttributeStatement;
34 import org.opensaml.saml1.core.Audience;
35 import org.opensaml.saml1.core.AudienceRestrictionCondition;
36 import org.opensaml.saml1.core.Conditions;
37 import org.opensaml.saml1.core.ConfirmationMethod;
38 import org.opensaml.saml1.core.NameIdentifier;
39 import org.opensaml.saml1.core.RequestAbstractType;
40 import org.opensaml.saml1.core.Response;
41 import org.opensaml.saml1.core.ResponseAbstractType;
42 import org.opensaml.saml1.core.Statement;
43 import org.opensaml.saml1.core.Status;
44 import org.opensaml.saml1.core.StatusCode;
45 import org.opensaml.saml1.core.StatusMessage;
46 import org.opensaml.saml1.core.Subject;
47 import org.opensaml.saml1.core.SubjectConfirmation;
48 import org.opensaml.saml2.metadata.AttributeAuthorityDescriptor;
49 import org.opensaml.saml2.metadata.AuthnAuthorityDescriptor;
50 import org.opensaml.saml2.metadata.NameIDFormat;
51 import org.opensaml.saml2.metadata.PDPDescriptor;
52 import org.opensaml.saml2.metadata.RoleDescriptor;
53 import org.opensaml.saml2.metadata.SPSSODescriptor;
54 import org.opensaml.saml2.metadata.SSODescriptor;
55 import org.opensaml.xml.XMLObjectBuilder;
56 import org.opensaml.xml.io.Marshaller;
57 import org.opensaml.xml.io.MarshallingException;
58 import org.opensaml.xml.security.SecurityException;
59 import org.opensaml.xml.security.SecurityHelper;
60 import org.opensaml.xml.security.credential.Credential;
61 import org.opensaml.xml.signature.Signature;
62 import org.opensaml.xml.signature.Signer;
63 import org.slf4j.Logger;
64 import org.slf4j.LoggerFactory;
66 import edu.internet2.middleware.shibboleth.common.attribute.AttributeRequestException;
67 import edu.internet2.middleware.shibboleth.common.attribute.BaseAttribute;
68 import edu.internet2.middleware.shibboleth.common.attribute.encoding.AttributeEncoder;
69 import edu.internet2.middleware.shibboleth.common.attribute.encoding.AttributeEncodingException;
70 import edu.internet2.middleware.shibboleth.common.attribute.encoding.SAML1NameIdentifierEncoder;
71 import edu.internet2.middleware.shibboleth.common.attribute.provider.SAML1AttributeAuthority;
72 import edu.internet2.middleware.shibboleth.common.profile.ProfileException;
73 import edu.internet2.middleware.shibboleth.common.relyingparty.provider.saml1.AbstractSAML1ProfileConfiguration;
74 import edu.internet2.middleware.shibboleth.idp.profile.AbstractSAMLProfileHandler;
76 /** Common implementation details for profile handlers. */
77 public abstract class AbstractSAML1ProfileHandler extends AbstractSAMLProfileHandler {
79 /** SAML Version for this profile handler. */
80 public static final SAMLVersion SAML_VERSION = SAMLVersion.VERSION_11;
83 private static Logger log = LoggerFactory.getLogger(AbstractSAML1ProfileHandler.class);
85 /** Builder of Response objects. */
86 private SAMLObjectBuilder<Response> responseBuilder;
88 /** Builder of Assertion objects. */
89 private SAMLObjectBuilder<Assertion> assertionBuilder;
91 /** Builder of Conditions objects. */
92 private SAMLObjectBuilder<Conditions> conditionsBuilder;
94 /** Builder of AudienceRestrictionCondition objects. */
95 private SAMLObjectBuilder<AudienceRestrictionCondition> audienceRestrictionConditionBuilder;
97 /** Builder of AudienceRestrictionCondition objects. */
98 private SAMLObjectBuilder<Audience> audienceBuilder;
100 /** Builder of SubjectConfirmation objects. */
101 private SAMLObjectBuilder<SubjectConfirmation> subjectConfirmationBuilder;
103 /** Builder of ConfirmationMethod objects. */
104 private SAMLObjectBuilder<ConfirmationMethod> confirmationMethodBuilder;
106 /** Builder of Subject objects. */
107 private SAMLObjectBuilder<Subject> subjectBuilder;
109 /** Builder for Status objects. */
110 private SAMLObjectBuilder<Status> statusBuilder;
112 /** Builder for StatusCode objects. */
113 private SAMLObjectBuilder<StatusCode> statusCodeBuilder;
115 /** Builder for StatusMessage objects. */
116 private SAMLObjectBuilder<StatusMessage> statusMessageBuilder;
118 /** For building signature. */
119 private XMLObjectBuilder<Signature> signatureBuilder;
122 * Default constructor.
124 @SuppressWarnings("unchecked")
125 public AbstractSAML1ProfileHandler() {
127 responseBuilder = (SAMLObjectBuilder<Response>) getBuilderFactory().getBuilder(Response.DEFAULT_ELEMENT_NAME);
128 assertionBuilder = (SAMLObjectBuilder<Assertion>) getBuilderFactory()
129 .getBuilder(Assertion.DEFAULT_ELEMENT_NAME);
130 conditionsBuilder = (SAMLObjectBuilder<Conditions>) getBuilderFactory().getBuilder(
131 Conditions.DEFAULT_ELEMENT_NAME);
132 audienceRestrictionConditionBuilder = (SAMLObjectBuilder<AudienceRestrictionCondition>) getBuilderFactory()
133 .getBuilder(AudienceRestrictionCondition.DEFAULT_ELEMENT_NAME);
134 audienceBuilder = (SAMLObjectBuilder<Audience>) getBuilderFactory().getBuilder(Audience.DEFAULT_ELEMENT_NAME);
135 subjectConfirmationBuilder = (SAMLObjectBuilder<SubjectConfirmation>) getBuilderFactory().getBuilder(
136 SubjectConfirmation.DEFAULT_ELEMENT_NAME);
137 confirmationMethodBuilder = (SAMLObjectBuilder<ConfirmationMethod>) getBuilderFactory().getBuilder(
138 ConfirmationMethod.DEFAULT_ELEMENT_NAME);
139 subjectBuilder = (SAMLObjectBuilder<Subject>) getBuilderFactory().getBuilder(Subject.DEFAULT_ELEMENT_NAME);
140 statusBuilder = (SAMLObjectBuilder<Status>) getBuilderFactory().getBuilder(Status.DEFAULT_ELEMENT_NAME);
141 statusCodeBuilder = (SAMLObjectBuilder<StatusCode>) getBuilderFactory().getBuilder(
142 StatusCode.DEFAULT_ELEMENT_NAME);
143 statusMessageBuilder = (SAMLObjectBuilder<StatusMessage>) getBuilderFactory().getBuilder(
144 StatusMessage.DEFAULT_ELEMENT_NAME);
145 signatureBuilder = (XMLObjectBuilder<Signature>) getBuilderFactory().getBuilder(Signature.DEFAULT_ELEMENT_NAME);
149 * Checks that the SAML major version for a request is 1.
151 * @param requestContext current request context containing the SAML message
153 * @throws ProfileException thrown if the major version of the SAML request is not 1
155 protected void checkSamlVersion(BaseSAML1ProfileRequestContext<?, ?, ?> requestContext) throws ProfileException {
156 SAMLObject samlObject = requestContext.getInboundSAMLMessage();
158 if (samlObject instanceof RequestAbstractType) {
159 RequestAbstractType request = (RequestAbstractType) samlObject;
160 if (request.getMajorVersion() < 1) {
161 requestContext.setFailureStatus(buildStatus(StatusCode.REQUESTER, StatusCode.REQUEST_VERSION_TOO_LOW,
163 throw new ProfileException("SAML request major version too low");
164 } else if (request.getMajorVersion() > 1) {
165 requestContext.setFailureStatus(buildStatus(StatusCode.REQUESTER, StatusCode.REQUEST_VERSION_TOO_HIGH,
167 throw new ProfileException("SAML request major version too low");
173 * Builds a response to the attribute query within the request context.
175 * @param requestContext current request context
176 * @param statements the statements to include in the response
178 * @return the built response
180 * @throws ProfileException thrown if there is a problem creating the SAML response
182 protected Response buildResponse(BaseSAML1ProfileRequestContext<?, ?, ?> requestContext, List<Statement> statements)
183 throws ProfileException {
185 DateTime issueInstant = new DateTime();
187 // create the assertion and add the attribute statement
188 Assertion assertion = buildAssertion(requestContext, issueInstant);
189 if (statements != null) {
190 assertion.getStatements().addAll(statements);
193 // create the SAML response and add the assertion
194 Response samlResponse = responseBuilder.buildObject();
195 samlResponse.setIssueInstant(issueInstant);
196 populateStatusResponse(requestContext, samlResponse);
198 samlResponse.getAssertions().add(assertion);
200 // sign the assertion if it should be signed
201 signAssertion(requestContext, assertion);
203 Status status = buildStatus(StatusCode.SUCCESS, null, null);
204 samlResponse.setStatus(status);
210 * Builds a basic assertion with its id, issue instant, SAML version, issuer, subject, and conditions populated.
212 * @param requestContext current request context
213 * @param issueInstant time to use as assertion issue instant
215 * @return the built assertion
217 protected Assertion buildAssertion(BaseSAML1ProfileRequestContext<?, ?, ?> requestContext, DateTime issueInstant) {
218 Assertion assertion = assertionBuilder.buildObject();
219 assertion.setID(getIdGenerator().generateIdentifier());
220 assertion.setIssueInstant(issueInstant);
221 assertion.setVersion(SAMLVersion.VERSION_11);
222 assertion.setIssuer(requestContext.getLocalEntityId());
224 Conditions conditions = buildConditions(requestContext, issueInstant);
225 assertion.setConditions(conditions);
231 * Builds a SAML assertion condition set. The following fields are set; not before, not on or after, audience
232 * restrictions, and proxy restrictions.
234 * @param requestContext current request context
235 * @param issueInstant timestamp the assertion was created
237 * @return constructed conditions
239 protected Conditions buildConditions(BaseSAML1ProfileRequestContext<?, ?, ?> requestContext, DateTime issueInstant) {
240 AbstractSAML1ProfileConfiguration profileConfig = requestContext.getProfileConfiguration();
242 Conditions conditions = conditionsBuilder.buildObject();
243 conditions.setNotBefore(issueInstant);
244 conditions.setNotOnOrAfter(issueInstant.plus(profileConfig.getAssertionLifetime()));
246 Collection<String> audiences;
248 // add audience restrictions
249 audiences = profileConfig.getAssertionAudiences();
250 if (audiences != null && audiences.size() > 0) {
251 AudienceRestrictionCondition audienceRestriction = audienceRestrictionConditionBuilder.buildObject();
252 for (String audienceUri : audiences) {
253 Audience audience = audienceBuilder.buildObject();
254 audience.setUri(audienceUri);
255 audienceRestriction.getAudiences().add(audience);
257 conditions.getAudienceRestrictionConditions().add(audienceRestriction);
264 * Builds the SAML subject for the user for the service provider.
266 * @param requestContext current request context
267 * @param confirmationMethod subject confirmation method used for the subject
269 * @return SAML subject for the user for the service provider
271 * @throws ProfileException thrown if a NameID can not be created either because there was a problem encoding the
272 * name ID attribute or because there are no supported name formats
274 protected Subject buildSubject(BaseSAML1ProfileRequestContext<?, ?, ?> requestContext, String confirmationMethod)
275 throws ProfileException {
276 NameIdentifier nameID = buildNameId(requestContext);
277 requestContext.setSubjectNameIdentifier(nameID);
279 ConfirmationMethod method = confirmationMethodBuilder.buildObject();
280 method.setConfirmationMethod(confirmationMethod);
282 SubjectConfirmation subjectConfirmation = subjectConfirmationBuilder.buildObject();
283 subjectConfirmation.getConfirmationMethods().add(method);
285 Subject subject = subjectBuilder.buildObject();
286 subject.setNameIdentifier(nameID);
287 subject.setSubjectConfirmation(subjectConfirmation);
293 * Builds a NameIdentifier appropriate for this request. NameIdentifier are built by inspecting the SAML request and
294 * metadata, picking a name format that was requested by the relying party or is mutually supported by both the
295 * relying party and asserting party as described in their metadata entries. Once a set of supported name formats is
296 * determined the principals attributes are inspected for an attribute supported an attribute encoder whose category
297 * is one of the supported name formats.
299 * @param requestContext current request context
301 * @return the NameIdentifier appropriate for this request
303 * @throws ProfileException thrown if a NameIdentifier can not be created either because there was a problem
304 * encoding the name ID attribute or because there are no supported name formats
306 protected NameIdentifier buildNameId(BaseSAML1ProfileRequestContext<?, ?, ?> requestContext)
307 throws ProfileException {
308 log.debug("Building assertion NameIdentifier to relying party {} for principal {}", requestContext
309 .getInboundMessageIssuer(), requestContext.getPrincipalName());
310 Map<String, BaseAttribute> principalAttributes = requestContext.getPrincipalAttributes();
311 List<String> supportedNameFormats = getNameFormats(requestContext);
313 log.debug("Supported name formats: {}", supportedNameFormats);
314 if (principalAttributes == null || supportedNameFormats == null) {
315 log.error("No attributes for principal " + requestContext.getPrincipalName()
316 + " support constructions of NameIdentifier");
317 requestContext.setFailureStatus(buildStatus(StatusCode.RESPONDER, null,
318 "Unable to construct NameIdentifier"));
319 throw new ProfileException("No principal attributes support NameIdentifier construction");
323 SAML1NameIdentifierEncoder nameIdEncoder;
325 for (BaseAttribute<?> attribute : principalAttributes.values()) {
326 for (AttributeEncoder encoder : attribute.getEncoders()) {
327 if (encoder instanceof SAML1NameIdentifierEncoder) {
328 nameIdEncoder = (SAML1NameIdentifierEncoder) encoder;
329 if (supportedNameFormats.contains(nameIdEncoder.getNameFormat())) {
332 "Using attribute {} suppoting name format {} to create the NameIdentifier for principal",
333 attribute.getId(), nameIdEncoder.getNameFormat());
334 return nameIdEncoder.encode(attribute);
339 requestContext.setFailureStatus(buildStatus(StatusCode.RESPONDER, null,
340 "Unable to construct NameIdentifier"));
341 log.error("No principal attribute supports an encoding into a supported name ID format.");
342 throw new ProfileException("No principal attribute supports an encoding into a supported name ID format.");
343 } catch (AttributeEncodingException e) {
344 log.error("Unable to construct NameIdentifier", e);
345 requestContext.setFailureStatus(buildStatus(StatusCode.RESPONDER, null,
346 "Unable to construct NameIdentifier"));
347 throw new ProfileException("Unable to encode NameIdentifier attribute", e);
353 * Gets the NameIdentifier format to use when creating NameIdentifiers for the relying party.
355 * @param requestContext current request context
357 * @return list of formats that may be used with the relying party
359 * @throws ProfileException thrown if there is a problem determining the NameIdentifier format to use
361 protected List<String> getNameFormats(BaseSAML1ProfileRequestContext<?, ?, ?> requestContext)
362 throws ProfileException {
363 ArrayList<String> nameFormats = new ArrayList<String>();
365 RoleDescriptor relyingPartyRole = requestContext.getPeerEntityRoleMetadata();
366 if (relyingPartyRole != null) {
367 List<String> relyingPartySupportedFormats = getEntitySupportedFormats(relyingPartyRole);
368 if (relyingPartySupportedFormats != null && !relyingPartySupportedFormats.isEmpty()) {
369 nameFormats.addAll(relyingPartySupportedFormats);
371 RoleDescriptor assertingPartyRole = requestContext.getLocalEntityRoleMetadata();
372 if (assertingPartyRole != null) {
373 List<String> assertingPartySupportedFormats = getEntitySupportedFormats(assertingPartyRole);
374 if (assertingPartySupportedFormats != null && !assertingPartySupportedFormats.isEmpty()) {
375 nameFormats.retainAll(assertingPartySupportedFormats);
381 if (nameFormats.isEmpty()) {
382 nameFormats.add("urn:oasis:names:tc:SAML:1.0:nameid-format:unspecified");
389 * Gets the list of NameIdentifier formats supported for a given role.
391 * @param role the role to get the list of supported NameIdentifier formats
393 * @return list of supported NameIdentifier formats
395 protected List<String> getEntitySupportedFormats(RoleDescriptor role) {
396 List<NameIDFormat> nameIDFormats = null;
398 if (role instanceof SSODescriptor) {
399 nameIDFormats = ((SSODescriptor) role).getNameIDFormats();
400 } else if (role instanceof AuthnAuthorityDescriptor) {
401 nameIDFormats = ((AuthnAuthorityDescriptor) role).getNameIDFormats();
402 } else if (role instanceof PDPDescriptor) {
403 nameIDFormats = ((PDPDescriptor) role).getNameIDFormats();
404 } else if (role instanceof AttributeAuthorityDescriptor) {
405 nameIDFormats = ((AttributeAuthorityDescriptor) role).getNameIDFormats();
408 ArrayList<String> supportedFormats = new ArrayList<String>();
409 if (nameIDFormats != null) {
410 for (NameIDFormat format : nameIDFormats) {
411 supportedFormats.add(format.getFormat());
415 return supportedFormats;
419 * Constructs an SAML response message carrying a request error.
421 * @param requestContext current request context containing the failure status
423 * @return the constructed error response
425 protected Response buildErrorResponse(BaseSAML1ProfileRequestContext<?, ?, ?> requestContext) {
426 Response samlResponse = responseBuilder.buildObject();
427 samlResponse.setIssueInstant(new DateTime());
428 populateStatusResponse(requestContext, samlResponse);
430 samlResponse.setStatus(requestContext.getFailureStatus());
436 * Populates the response's id, in response to, issue instant, version, and issuer properties.
438 * @param requestContext current request context
439 * @param response the response to populate
441 protected void populateStatusResponse(BaseSAML1ProfileRequestContext<?, ?, ?> requestContext,
442 ResponseAbstractType response) {
443 response.setID(getIdGenerator().generateIdentifier());
445 SAMLObject samlMessage = requestContext.getInboundSAMLMessage();
446 if (samlMessage != null && samlMessage instanceof RequestAbstractType) {
447 response.setInResponseTo(((RequestAbstractType) samlMessage).getID());
449 response.setVersion(SAMLVersion.VERSION_11);
453 * Build a status message, with an optional second-level failure message.
455 * @param topLevelCode top-level status code
456 * @param secondLevelCode second-level status code
457 * @param failureMessage An optional second-level failure message
459 * @return a Status object.
461 protected Status buildStatus(QName topLevelCode, QName secondLevelCode, String failureMessage) {
462 Status status = statusBuilder.buildObject();
464 StatusCode statusCode = statusCodeBuilder.buildObject();
465 statusCode.setValue(topLevelCode);
466 status.setStatusCode(statusCode);
468 if (secondLevelCode != null) {
469 StatusCode secondLevelStatusCode = statusCodeBuilder.buildObject();
470 secondLevelStatusCode.setValue(secondLevelCode);
471 statusCode.setStatusCode(secondLevelStatusCode);
474 if (failureMessage != null) {
475 StatusMessage msg = statusMessageBuilder.buildObject();
476 msg.setMessage(failureMessage);
477 status.setStatusMessage(msg);
484 * Resolved the attributes for the principal.
486 * @param requestContext current request context
488 * @throws ProfileException thrown if attributes can not be resolved
490 protected void resolveAttributes(BaseSAML1ProfileRequestContext<?, ?, ?> requestContext) throws ProfileException {
491 AbstractSAML1ProfileConfiguration profileConfiguration = requestContext.getProfileConfiguration();
492 SAML1AttributeAuthority attributeAuthority = profileConfiguration.getAttributeAuthority();
495 log.debug("Resolving attributes for principal {} of SAML request from relying party {}", requestContext
496 .getPrincipalName(), requestContext.getInboundMessageIssuer());
497 Map<String, BaseAttribute> principalAttributes = attributeAuthority.getAttributes(requestContext);
499 requestContext.setAttributes(principalAttributes);
500 } catch (AttributeRequestException e) {
501 log.error("Error resolving attributes for SAML request from relying party "
502 + requestContext.getInboundMessageIssuer(), e);
503 requestContext.setFailureStatus(buildStatus(StatusCode.RESPONDER, null, "Error resolving attributes"));
504 throw new ProfileException("Error resolving attributes for SAML request from relying party "
505 + requestContext.getInboundMessageIssuer(), e);
510 * Executes a query for attributes and builds a SAML attribute statement from the results.
512 * @param requestContext current request context
513 * @param subjectConfMethod subject confirmation method
515 * @return attribute statement resulting from the query
517 * @throws ProfileException thrown if there is a problem making the query
519 protected AttributeStatement buildAttributeStatement(BaseSAML1ProfileRequestContext<?, ?, ?> requestContext,
520 String subjectConfMethod) throws ProfileException {
522 log.debug("Creating attribute statement in response to SAML request from relying party {}", requestContext
523 .getInboundMessageIssuer());
524 AbstractSAML1ProfileConfiguration profileConfiguration = requestContext.getProfileConfiguration();
525 SAML1AttributeAuthority attributeAuthority = profileConfiguration.getAttributeAuthority();
528 AttributeStatement statment;
529 if (requestContext.getInboundSAMLMessage() instanceof AttributeQuery) {
530 statment = attributeAuthority.buildAttributeStatement((AttributeQuery) requestContext
531 .getInboundSAMLMessage(), requestContext.getPrincipalAttributes().values());
533 statment = attributeAuthority.buildAttributeStatement(null, requestContext.getPrincipalAttributes()
537 Subject statementSubject = buildSubject(requestContext, subjectConfMethod);
538 statment.setSubject(statementSubject);
541 } catch (AttributeRequestException e) {
542 log.error("Error encoding attributes for principal " + requestContext.getPrincipalName(), e);
543 requestContext.setFailureStatus(buildStatus(StatusCode.RESPONDER, null, "Error resolving attributes"));
544 throw new ProfileException("Error encoding attributes for principal " + requestContext.getPrincipalName(),
550 * Resolves the principal name of the subject of the request.
552 * @param requestContext current request context
554 * @throws ProfileException thrown if the principal name can not be resolved
556 protected void resolvePrincipal(BaseSAML1ProfileRequestContext<?, ?, ?> requestContext) throws ProfileException {
557 AbstractSAML1ProfileConfiguration profileConfiguration = requestContext.getProfileConfiguration();
558 SAML1AttributeAuthority attributeAuthority = profileConfiguration.getAttributeAuthority();
560 log.debug("Resolving principal name for subject of SAML request from relying party {}", requestContext
561 .getInboundMessageIssuer());
564 String principal = attributeAuthority.getPrincipal(requestContext);
565 requestContext.setPrincipalName(principal);
566 } catch (AttributeRequestException e) {
567 log.error("Error resolving attributes for SAML request from relying party "
568 + requestContext.getInboundMessageIssuer(), e);
569 requestContext.setFailureStatus(buildStatus(StatusCode.RESPONDER, StatusCode.REQUEST_DENIED,
570 "Error resolving principal"));
571 throw new ProfileException("Error resolving attributes for SAML request from relying party "
572 + requestContext.getInboundMessageIssuer(), e);
577 * Signs the given assertion if either the current profile configuration or the relying party configuration contains
578 * signing credentials.
580 * @param requestContext current request context
581 * @param assertion assertion to sign
583 * @throws ProfileException thrown if the metadata can not be located for the relying party or, if signing is
584 * required, if a signing credential is not configured
586 protected void signAssertion(BaseSAML1ProfileRequestContext<?, ?, ?> requestContext, Assertion assertion)
587 throws ProfileException {
588 log.debug("Determining if SAML assertion to relying party {} should be signed", requestContext
589 .getInboundMessageIssuer());
591 boolean signAssertion = false;
593 RoleDescriptor relyingPartyRole = requestContext.getPeerEntityRoleMetadata();
594 AbstractSAML1ProfileConfiguration profileConfig = requestContext.getProfileConfiguration();
595 if (profileConfig.getSignAssertions()) {
596 signAssertion = true;
597 log.debug("IdP relying party configuration {} indicates to sign assertions: {}", requestContext
598 .getRelyingPartyConfiguration().getRelyingPartyId(), signAssertion);
601 if (!signAssertion && relyingPartyRole instanceof SPSSODescriptor) {
602 SPSSODescriptor ssoDescriptor = (SPSSODescriptor) relyingPartyRole;
603 if (ssoDescriptor.getWantAssertionsSigned() != null) {
604 signAssertion = ssoDescriptor.getWantAssertionsSigned().booleanValue();
605 log.debug("Entity metadata for relying party {} indicates to sign assertions: {}", requestContext
606 .getInboundMessageIssuer(), signAssertion);
610 if (!signAssertion) {
614 log.debug("Determining signing credntial for assertion to relying party {}", requestContext
615 .getInboundMessageIssuer());
616 Credential signatureCredential = profileConfig.getSigningCredential();
617 if (signatureCredential == null) {
618 signatureCredential = requestContext.getRelyingPartyConfiguration().getDefaultSigningCredential();
621 if (signatureCredential == null) {
622 throw new ProfileException("No signing credential is specified for relying party configuration "
623 + requestContext.getRelyingPartyConfiguration().getProviderId()
624 + " or it's SAML2 attribute query profile configuration");
627 log.debug("Signing assertion to relying party {}", requestContext.getInboundMessageIssuer());
628 Signature signature = signatureBuilder.buildObject(Signature.DEFAULT_ELEMENT_NAME);
630 signature.setSigningCredential(signatureCredential);
632 // TODO pull SecurityConfiguration from SAMLMessageContext? needs to be added
633 // TODO how to pull what keyInfoGenName to use?
634 SecurityHelper.prepareSignatureParams(signature, signatureCredential, null, null);
635 } catch (SecurityException e) {
636 throw new ProfileException("Error preparing signature for signing", e);
639 assertion.setSignature(signature);
641 Marshaller assertionMarshaller = Configuration.getMarshallerFactory().getMarshaller(assertion);
643 assertionMarshaller.marshall(assertion);
644 Signer.signObject(signature);
645 } catch (MarshallingException e) {
646 log.error("Unable to marshall assertion for signing", e);
647 throw new ProfileException("Unable to marshall assertion for signing", e);