Properly detect if incoming request is an attribute query or not
[java-idp.git] / src / edu / internet2 / middleware / shibboleth / idp / profile / saml2 / AbstractSAML2ProfileHandler.java
1 /*
2  * Copyright [2007] [University Corporation for Advanced Internet Development, Inc.]
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package edu.internet2.middleware.shibboleth.idp.profile.saml2;
18
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.List;
22 import java.util.Map;
23
24 import javax.servlet.ServletRequest;
25 import javax.servlet.ServletResponse;
26
27 import org.apache.log4j.Logger;
28 import org.joda.time.DateTime;
29 import org.opensaml.common.SAMLObjectBuilder;
30 import org.opensaml.common.SAMLVersion;
31 import org.opensaml.common.impl.SAMLObjectContentReference;
32 import org.opensaml.log.Level;
33 import org.opensaml.saml2.core.Assertion;
34 import org.opensaml.saml2.core.AttributeQuery;
35 import org.opensaml.saml2.core.AttributeStatement;
36 import org.opensaml.saml2.core.Audience;
37 import org.opensaml.saml2.core.AudienceRestriction;
38 import org.opensaml.saml2.core.AuthnRequest;
39 import org.opensaml.saml2.core.Conditions;
40 import org.opensaml.saml2.core.Issuer;
41 import org.opensaml.saml2.core.NameID;
42 import org.opensaml.saml2.core.ProxyRestriction;
43 import org.opensaml.saml2.core.RequestAbstractType;
44 import org.opensaml.saml2.core.Response;
45 import org.opensaml.saml2.core.Statement;
46 import org.opensaml.saml2.core.Status;
47 import org.opensaml.saml2.core.StatusCode;
48 import org.opensaml.saml2.core.StatusMessage;
49 import org.opensaml.saml2.core.StatusResponseType;
50 import org.opensaml.saml2.core.Subject;
51 import org.opensaml.saml2.core.SubjectConfirmation;
52 import org.opensaml.saml2.metadata.AttributeAuthorityDescriptor;
53 import org.opensaml.saml2.metadata.AuthnAuthorityDescriptor;
54 import org.opensaml.saml2.metadata.NameIDFormat;
55 import org.opensaml.saml2.metadata.PDPDescriptor;
56 import org.opensaml.saml2.metadata.RoleDescriptor;
57 import org.opensaml.saml2.metadata.SPSSODescriptor;
58 import org.opensaml.saml2.metadata.SSODescriptor;
59 import org.opensaml.xml.XMLObjectBuilder;
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.opensaml.xml.util.DatatypeHelper;
64
65 import edu.internet2.middleware.shibboleth.common.attribute.AttributeRequestException;
66 import edu.internet2.middleware.shibboleth.common.attribute.BaseAttribute;
67 import edu.internet2.middleware.shibboleth.common.attribute.encoding.AttributeEncoder;
68 import edu.internet2.middleware.shibboleth.common.attribute.encoding.AttributeEncodingException;
69 import edu.internet2.middleware.shibboleth.common.attribute.encoding.SAML2NameIDAttributeEncoder;
70 import edu.internet2.middleware.shibboleth.common.attribute.provider.SAML2AttributeAuthority;
71 import edu.internet2.middleware.shibboleth.common.attribute.provider.ShibbolethSAMLAttributeRequestContext;
72 import edu.internet2.middleware.shibboleth.common.log.AuditLogEntry;
73 import edu.internet2.middleware.shibboleth.common.profile.ProfileException;
74 import edu.internet2.middleware.shibboleth.common.profile.ProfileRequest;
75 import edu.internet2.middleware.shibboleth.common.profile.ProfileResponse;
76 import edu.internet2.middleware.shibboleth.common.relyingparty.provider.saml2.AbstractSAML2ProfileConfiguration;
77 import edu.internet2.middleware.shibboleth.idp.profile.AbstractSAMLProfileHandler;
78 import edu.internet2.middleware.shibboleth.idp.session.ServiceInformation;
79 import edu.internet2.middleware.shibboleth.idp.session.Session;
80
81 /** Common implementation details for profile handlers. */
82 public abstract class AbstractSAML2ProfileHandler extends AbstractSAMLProfileHandler {
83
84     /** SAML Version for this profile handler. */
85     public static final SAMLVersion SAML_VERSION = SAMLVersion.VERSION_20;
86
87     /** Class logger. */
88     private Logger log = Logger.getLogger(AbstractSAML2ProfileHandler.class);
89
90     /** For building response. */
91     private SAMLObjectBuilder<Response> responseBuilder;
92
93     /** For building status. */
94     private SAMLObjectBuilder<Status> statusBuilder;
95
96     /** For building statuscode. */
97     private SAMLObjectBuilder<StatusCode> statusCodeBuilder;
98
99     /** For building StatusMessages. */
100     private SAMLObjectBuilder<StatusMessage> statusMessageBuilder;
101
102     /** For building assertion. */
103     private SAMLObjectBuilder<Assertion> assertionBuilder;
104
105     /** For building issuer. */
106     private SAMLObjectBuilder<Issuer> issuerBuilder;
107
108     /** For building subject. */
109     private SAMLObjectBuilder<Subject> subjectBuilder;
110
111     /** For builder subject confirmation. */
112     private SAMLObjectBuilder<SubjectConfirmation> subjectConfirmationBuilder;
113
114     /** For building conditions. */
115     private SAMLObjectBuilder<Conditions> conditionsBuilder;
116
117     /** For building audience restriction. */
118     private SAMLObjectBuilder<AudienceRestriction> audienceRestrictionBuilder;
119
120     /** For building proxy retrictions. */
121     private SAMLObjectBuilder<ProxyRestriction> proxyRestrictionBuilder;
122
123     /** For building audience. */
124     private SAMLObjectBuilder<Audience> audienceBuilder;
125
126     /** For building signature. */
127     private XMLObjectBuilder<Signature> signatureBuilder;
128
129     /** Constructor. */
130     @SuppressWarnings("unchecked")
131     protected AbstractSAML2ProfileHandler() {
132         super();
133
134         responseBuilder = (SAMLObjectBuilder<Response>) getBuilderFactory().getBuilder(Response.DEFAULT_ELEMENT_NAME);
135         statusBuilder = (SAMLObjectBuilder<Status>) getBuilderFactory().getBuilder(Status.DEFAULT_ELEMENT_NAME);
136         statusCodeBuilder = (SAMLObjectBuilder<StatusCode>) getBuilderFactory().getBuilder(
137                 StatusCode.DEFAULT_ELEMENT_NAME);
138         statusMessageBuilder = (SAMLObjectBuilder<StatusMessage>) getBuilderFactory().getBuilder(
139                 StatusMessage.DEFAULT_ELEMENT_NAME);
140         issuerBuilder = (SAMLObjectBuilder<Issuer>) getBuilderFactory().getBuilder(Issuer.DEFAULT_ELEMENT_NAME);
141         assertionBuilder = (SAMLObjectBuilder<Assertion>) getBuilderFactory()
142                 .getBuilder(Assertion.DEFAULT_ELEMENT_NAME);
143         subjectBuilder = (SAMLObjectBuilder<Subject>) getBuilderFactory().getBuilder(Subject.DEFAULT_ELEMENT_NAME);
144         subjectConfirmationBuilder = (SAMLObjectBuilder<SubjectConfirmation>) getBuilderFactory().getBuilder(
145                 SubjectConfirmation.DEFAULT_ELEMENT_NAME);
146         conditionsBuilder = (SAMLObjectBuilder<Conditions>) getBuilderFactory().getBuilder(
147                 Conditions.DEFAULT_ELEMENT_NAME);
148         audienceRestrictionBuilder = (SAMLObjectBuilder<AudienceRestriction>) getBuilderFactory().getBuilder(
149                 AudienceRestriction.DEFAULT_ELEMENT_NAME);
150         proxyRestrictionBuilder = (SAMLObjectBuilder<ProxyRestriction>) getBuilderFactory().getBuilder(
151                 ProxyRestriction.DEFAULT_ELEMENT_NAME);
152         audienceBuilder = (SAMLObjectBuilder<Audience>) getBuilderFactory().getBuilder(Audience.DEFAULT_ELEMENT_NAME);
153         signatureBuilder = (XMLObjectBuilder<Signature>) getBuilderFactory().getBuilder(Signature.DEFAULT_ELEMENT_NAME);
154     }
155
156     /**
157      * Checks that the SAML major version for a request is 2.
158      * 
159      * @param requestContext current request context containing the SAML message
160      * 
161      * @throws ProfileException thrown if the major version of the SAML request is not 2
162      */
163     protected void checkSamlVersion(SAML2ProfileRequestContext requestContext) throws ProfileException {
164         SAMLVersion version = requestContext.getSamlRequest().getVersion();
165         if (version.getMajorVersion() < 2) {
166             requestContext.setFailureStatus(buildStatus(StatusCode.VERSION_MISMATCH_URI,
167                     StatusCode.REQUEST_VERSION_TOO_LOW_URI, null));
168             throw new ProfileException("SAML request version too low");
169         } else if (version.getMajorVersion() > 2) {
170             requestContext.setFailureStatus(buildStatus(StatusCode.VERSION_MISMATCH_URI,
171                     StatusCode.REQUEST_VERSION_TOO_HIGH_URI, null));
172             throw new ProfileException("SAML request version too high");
173         }
174     }
175
176     /**
177      * Builds a response to the attribute query within the request context.
178      * 
179      * @param requestContext current request context
180      * @param assertionSubject subject of the assertion within the response
181      * @param statements the statements to include in the response
182      * 
183      * @return the built response
184      * 
185      * @throws ProfileException thrown if there is a problem creating the SAML response
186      */
187     protected Response buildResponse(SAML2ProfileRequestContext requestContext, Subject assertionSubject,
188             List<Statement> statements) throws ProfileException {
189
190         DateTime issueInstant = new DateTime();
191
192         // create the assertion and add the attribute statement
193         Assertion assertion = buildAssertion(requestContext, issueInstant);
194         assertion.setSubject(assertionSubject);
195         if (statements != null) {
196             assertion.getStatements().addAll(statements);
197         }
198
199         // create the SAML response and add the assertion
200         Response samlResponse = responseBuilder.buildObject();
201         samlResponse.setIssueInstant(issueInstant);
202         populateStatusResponse(requestContext, samlResponse);
203
204         samlResponse.getAssertions().add(assertion);
205
206         // sign the assertion if it should be signed
207         signAssertion(requestContext, assertion);
208
209         Status status = buildStatus(StatusCode.SUCCESS_URI, null, null);
210         samlResponse.setStatus(status);
211
212         return samlResponse;
213     }
214
215     /**
216      * Builds a basic assertion with its id, issue instant, SAML version, issuer, subject, and conditions populated.
217      * 
218      * @param requestContext current request context
219      * @param issueInstant time to use as assertion issue instant
220      * 
221      * @return the built assertion
222      */
223     protected Assertion buildAssertion(SAML2ProfileRequestContext requestContext, DateTime issueInstant) {
224         Assertion assertion = assertionBuilder.buildObject();
225         assertion.setID(getIdGenerator().generateIdentifier());
226         assertion.setIssueInstant(issueInstant);
227         assertion.setVersion(SAMLVersion.VERSION_20);
228         assertion.setIssuer(buildEntityIssuer(requestContext));
229
230         Conditions conditions = buildConditions(requestContext, issueInstant);
231         assertion.setConditions(conditions);
232
233         return assertion;
234     }
235
236     /**
237      * Creates an {@link Issuer} populated with information about the relying party.
238      * 
239      * @param requestContext current request context
240      * 
241      * @return the built issuer
242      */
243     protected Issuer buildEntityIssuer(SAML2ProfileRequestContext requestContext) {
244         Issuer issuer = issuerBuilder.buildObject();
245         issuer.setFormat(Issuer.ENTITY);
246         issuer.setValue(requestContext.getAssertingPartyId());
247
248         return issuer;
249     }
250
251     /**
252      * Builds a SAML assertion condition set. The following fields are set; not before, not on or after, audience
253      * restrictions, and proxy restrictions.
254      * 
255      * @param requestContext current request context
256      * @param issueInstant timestamp the assertion was created
257      * 
258      * @return constructed conditions
259      */
260     protected Conditions buildConditions(SAML2ProfileRequestContext requestContext, DateTime issueInstant) {
261         AbstractSAML2ProfileConfiguration profileConfig = requestContext.getProfileConfiguration();
262
263         Conditions conditions = conditionsBuilder.buildObject();
264         conditions.setNotBefore(issueInstant);
265         conditions.setNotOnOrAfter(issueInstant.plus(profileConfig.getAssertionLifetime()));
266
267         Collection<String> audiences;
268
269         // add audience restrictions
270         audiences = profileConfig.getAssertionAudiences();
271         if (audiences != null && audiences.size() > 0) {
272             AudienceRestriction audienceRestriction = audienceRestrictionBuilder.buildObject();
273             for (String audienceUri : audiences) {
274                 Audience audience = audienceBuilder.buildObject();
275                 audience.setAudienceURI(audienceUri);
276                 audienceRestriction.getAudiences().add(audience);
277             }
278             conditions.getAudienceRestrictions().add(audienceRestriction);
279         }
280
281         // add proxy restrictions
282         audiences = profileConfig.getProxyAudiences();
283         if (audiences != null && audiences.size() > 0) {
284             ProxyRestriction proxyRestriction = proxyRestrictionBuilder.buildObject();
285             Audience audience;
286             for (String audienceUri : audiences) {
287                 audience = audienceBuilder.buildObject();
288                 audience.setAudienceURI(audienceUri);
289                 proxyRestriction.getAudiences().add(audience);
290             }
291
292             proxyRestriction.setProxyCount(profileConfig.getProxyCount());
293             conditions.getConditions().add(proxyRestriction);
294         }
295
296         return conditions;
297     }
298
299     /**
300      * Populates the response's id, in response to, issue instant, version, and issuer properties.
301      * 
302      * @param requestContext current request context
303      * @param response the response to populate
304      */
305     protected void populateStatusResponse(SAML2ProfileRequestContext requestContext, StatusResponseType response) {
306         response.setID(getIdGenerator().generateIdentifier());
307         if (requestContext.getSamlRequest() != null) {
308             response.setInResponseTo(requestContext.getSamlRequest().getID());
309         }
310         response.setVersion(SAMLVersion.VERSION_20);
311         response.setIssuer(buildEntityIssuer(requestContext));
312     }
313
314     /**
315      * Executes a query for attributes and builds a SAML attribute statement from the results.
316      * 
317      * @param requestContext current request context
318      * 
319      * @return attribute statement resulting from the query
320      * 
321      * @throws ProfileException thrown if there is a problem making the query
322      */
323     protected AttributeStatement buildAttributeStatement(SAML2ProfileRequestContext requestContext)
324             throws ProfileException {
325
326         if (log.isDebugEnabled()) {
327             log.debug("Creating attribute statement in response to SAML request "
328                     + requestContext.getSamlRequest().getID() + " from relying party "
329                     + requestContext.getRelyingPartyId());
330         }
331
332         AbstractSAML2ProfileConfiguration profileConfiguration = requestContext.getProfileConfiguration();
333         SAML2AttributeAuthority attributeAuthority = profileConfiguration.getAttributeAuthority();
334
335         try {
336             if (log.isDebugEnabled()) {
337                 log.debug("Resolving attributes for principal " + requestContext.getPrincipalName()
338                         + " of SAML request " + requestContext.getSamlRequest().getID() + " from relying party "
339                         + requestContext.getRelyingPartyId());
340             }
341             Map<String, BaseAttribute> principalAttributes = attributeAuthority
342                     .getAttributes(buildAttributeRequestContext(requestContext));
343
344             requestContext.setPrincipalAttributes(principalAttributes);
345
346             if (requestContext.getSamlRequest() instanceof AttributeQuery) {
347                 return attributeAuthority.buildAttributeStatement((AttributeQuery) requestContext.getSamlRequest(),
348                         principalAttributes.values());
349             } else {
350                 return attributeAuthority.buildAttributeStatement(null, principalAttributes.values());
351             }
352         } catch (AttributeRequestException e) {
353             log.error("Error resolving attributes for SAML request " + requestContext.getSamlRequest().getID()
354                     + " from relying party " + requestContext.getRelyingPartyId(), e);
355             requestContext.setFailureStatus(buildStatus(StatusCode.RESPONDER_URI, null, "Error resolving attributes"));
356             throw new ProfileException("Error resolving attributes for SAML request "
357                     + requestContext.getSamlRequest().getID() + " from relying party "
358                     + requestContext.getRelyingPartyId(), e);
359         }
360     }
361
362     /**
363      * Resolves the principal name of the subject of the request.
364      * 
365      * @param requestContext current request context
366      * 
367      * @throws ProfileException thrown if the principal name can not be resolved
368      */
369     protected void resolvePrincipal(SAML2ProfileRequestContext requestContext) throws ProfileException {
370         AbstractSAML2ProfileConfiguration profileConfiguration = requestContext.getProfileConfiguration();
371         if (profileConfiguration == null) {
372             log.error("Unable to resolve principal, no SAML 2 profile configuration for relying party "
373                     + requestContext.getRelyingPartyId());
374             requestContext.setFailureStatus(buildStatus(StatusCode.RESPONDER_URI, StatusCode.REQUEST_DENIED_URI,
375                     "Error resolving principal"));
376             throw new ProfileException(
377                     "Unable to resolve principal, no SAML 2 profile configuration for relying party "
378                             + requestContext.getRelyingPartyId());
379         }
380         SAML2AttributeAuthority attributeAuthority = profileConfiguration.getAttributeAuthority();
381
382         if (log.isDebugEnabled()) {
383             log.debug("Resolving principal name for subject of SAML request " + requestContext.getSamlRequest().getID()
384                     + " from relying party " + requestContext.getRelyingPartyId());
385         }
386
387         try {
388             String principal = attributeAuthority.getPrincipal(buildAttributeRequestContext(requestContext));
389             requestContext.setPrincipalName(principal);
390         } catch (AttributeRequestException e) {
391             log.error("Error resolving attributes for SAML request " + requestContext.getSamlRequest().getID()
392                     + " from relying party " + requestContext.getRelyingPartyId(), e);
393             requestContext.setFailureStatus(buildStatus(StatusCode.RESPONDER_URI, StatusCode.UNKNOWN_PRINCIPAL_URI,
394                     "Error resolving principal"));
395             throw new ProfileException("Error resolving attributes for SAML request "
396                     + requestContext.getSamlRequest().getID() + " from relying party "
397                     + requestContext.getRelyingPartyId(), e);
398         }
399     }
400
401     /**
402      * Creates an attribute query context from the current profile request context.
403      * 
404      * @param requestContext current profile request
405      * 
406      * @return created query context
407      */
408     protected ShibbolethSAMLAttributeRequestContext<NameID, AttributeQuery> buildAttributeRequestContext(
409             SAML2ProfileRequestContext requestContext) {
410
411         ShibbolethSAMLAttributeRequestContext<NameID, AttributeQuery> queryContext;
412
413         if(requestContext.getSamlRequest() instanceof AttributeQuery){
414         queryContext = new ShibbolethSAMLAttributeRequestContext<NameID, AttributeQuery>(getMetadataProvider(),
415                 requestContext.getRelyingPartyConfiguration(), (AttributeQuery) requestContext.getSamlRequest());
416         }else{
417             queryContext = new ShibbolethSAMLAttributeRequestContext<NameID, AttributeQuery>(getMetadataProvider(),
418                     requestContext.getRelyingPartyConfiguration(), null);
419         }
420         queryContext.setAttributeRequester(requestContext.getAssertingPartyId());
421         queryContext.setPrincipalName(requestContext.getPrincipalName());
422         queryContext.setProfileConfiguration(requestContext.getProfileConfiguration());
423         queryContext.setRequest(requestContext.getProfileRequest());
424
425         Session userSession = getSessionManager().getSession(getUserSessionId(requestContext.getProfileRequest()));
426         if (userSession != null) {
427             queryContext.setUserSession(userSession);
428             ServiceInformation serviceInfo = userSession.getServicesInformation().get(
429                     requestContext.getRelyingPartyId());
430             if (serviceInfo != null) {
431                 String principalAuthenticationMethod = serviceInfo.getAuthenticationMethod().getAuthenticationMethod();
432
433                 requestContext.setPrincipalAuthenticationMethod(principalAuthenticationMethod);
434                 queryContext.setPrincipalAuthenticationMethod(principalAuthenticationMethod);
435             }
436         }
437
438         return queryContext;
439     }
440
441     /**
442      * Signs the given assertion if either the current profile configuration or the relying party configuration contains
443      * signing credentials.
444      * 
445      * @param requestContext current request context
446      * @param assertion assertion to sign
447      * 
448      * @throws ProfileException thrown if the metadata can not be located for the relying party or, if signing is
449      *             required, if a signing credential is not configured
450      */
451     protected void signAssertion(SAML2ProfileRequestContext requestContext, Assertion assertion)
452             throws ProfileException {
453         if (log.isDebugEnabled()) {
454             log.debug("Determining if SAML assertion to relying party " + requestContext.getRelyingPartyId()
455                     + " should be signed");
456         }
457
458         boolean signAssertion = false;
459
460         AbstractSAML2ProfileConfiguration profileConfig = requestContext.getProfileConfiguration();
461
462         if (requestContext.getRelyingPartyRoleMetadata() instanceof SPSSODescriptor) {
463             SPSSODescriptor ssoDescriptor = (SPSSODescriptor) requestContext.getRelyingPartyRoleMetadata();
464             if (ssoDescriptor.getWantAssertionsSigned() != null) {
465                 signAssertion = ssoDescriptor.getWantAssertionsSigned().booleanValue();
466                 if (log.isDebugEnabled()) {
467                     log.debug("Entity metadata for relying party " + requestContext.getRelyingPartyId()
468                             + " indicates to sign assertions: " + signAssertion);
469                 }
470             }
471         } else if (profileConfig.getSignAssertions()) {
472             signAssertion = true;
473             log.debug("IdP relying party configuration "
474                     + requestContext.getRelyingPartyConfiguration().getRelyingPartyId()
475                     + " indicates to sign assertions: " + signAssertion);
476         }
477
478         if (!signAssertion) {
479             return;
480         }
481
482         if (log.isDebugEnabled()) {
483             log.debug("Determining signing credntial for assertion to relying party "
484                     + requestContext.getRelyingPartyId());
485         }
486         Credential signatureCredential = profileConfig.getSigningCredential();
487         if (signatureCredential == null) {
488             signatureCredential = requestContext.getRelyingPartyConfiguration().getDefaultSigningCredential();
489         }
490
491         if (signatureCredential == null) {
492             throw new ProfileException("No signing credential is specified for relying party configuration "
493                     + requestContext.getRelyingPartyConfiguration().getProviderId()
494                     + " or it's SAML2 attribute query profile configuration");
495         }
496
497         if (log.isDebugEnabled()) {
498             log.debug("Signing assertion to relying party " + requestContext.getRelyingPartyId());
499         }
500         SAMLObjectContentReference contentRef = new SAMLObjectContentReference(assertion);
501         Signature signature = signatureBuilder.buildObject(Signature.DEFAULT_ELEMENT_NAME);
502         signature.getContentReferences().add(contentRef);
503         assertion.setSignature(signature);
504
505         Signer.signObject(signature);
506     }
507
508     /**
509      * Build a status message, with an optional second-level failure message.
510      * 
511      * @param topLevelCode The top-level status code. Should be from saml-core-2.0-os, sec. 3.2.2.2
512      * @param secondLevelCode An optional second-level failure code. Should be from saml-core-2.0-is, sec 3.2.2.2. If
513      *            null, no second-level Status element will be set.
514      * @param failureMessage An optional second-level failure message
515      * 
516      * @return a Status object.
517      */
518     protected Status buildStatus(String topLevelCode, String secondLevelCode, String failureMessage) {
519         Status status = statusBuilder.buildObject();
520
521         StatusCode statusCode = statusCodeBuilder.buildObject();
522         statusCode.setValue(DatatypeHelper.safeTrimOrNullString(topLevelCode));
523         status.setStatusCode(statusCode);
524
525         if (secondLevelCode != null) {
526             StatusCode secondLevelStatusCode = statusCodeBuilder.buildObject();
527             secondLevelStatusCode.setValue(DatatypeHelper.safeTrimOrNullString(secondLevelCode));
528             statusCode.setStatusCode(secondLevelStatusCode);
529         }
530
531         if (failureMessage != null) {
532             StatusMessage msg = statusMessageBuilder.buildObject();
533             msg.setMessage(failureMessage);
534             status.setStatusMessage(msg);
535         }
536
537         return status;
538     }
539
540     /**
541      * Builds the SAML subject for the user for the service provider.
542      * 
543      * @param requestContext current request context
544      * @param confirmationMethod subject confirmation method used for the subject
545      * 
546      * @return SAML subject for the user for the service provider
547      * 
548      * @throws ProfileException thrown if a NameID can not be created either because there was a problem encoding the
549      *             name ID attribute or because there are no supported name formats
550      */
551     protected Subject buildSubject(SAML2ProfileRequestContext requestContext, String confirmationMethod)
552             throws ProfileException {
553         NameID nameID = buildNameId(requestContext);
554         requestContext.setSubjectNameID(nameID);
555         // TODO handle encryption
556
557         SubjectConfirmation subjectConfirmation = subjectConfirmationBuilder.buildObject();
558         subjectConfirmation.setMethod(confirmationMethod);
559
560         Subject subject = subjectBuilder.buildObject();
561         subject.setNameID(nameID);
562         subject.getSubjectConfirmations().add(subjectConfirmation);
563
564         return subject;
565     }
566
567     /**
568      * Builds a NameID appropriate for this request. NameIDs are built by inspecting the SAML request and metadata,
569      * picking a name format that was requested by the relying party or is mutually supported by both the relying party
570      * and asserting party as described in their metadata entries. Once a set of supported name formats is determined
571      * the principals attributes are inspected for an attribute supported an attribute encoder whose category is one of
572      * the supported name formats.
573      * 
574      * @param requestContext current request context
575      * 
576      * @return the NameID appropriate for this request
577      * 
578      * @throws ProfileException thrown if a NameID can not be created either because there was a problem encoding the
579      *             name ID attribute or because there are no supported name formats
580      */
581     protected NameID buildNameId(SAML2ProfileRequestContext requestContext) throws ProfileException {
582         if (log.isDebugEnabled()) {
583             log.debug("Building assertion NameID for principal/relying party:" + requestContext.getPrincipalName()
584                     + "/" + requestContext.getRelyingPartyId());
585         }
586         Map<String, BaseAttribute> principalAttributes = requestContext.getPrincipalAttributes();
587         List<String> supportedNameFormats = getNameFormats(requestContext);
588
589         if (log.isDebugEnabled()) {
590             log.debug("Supported NameID formats: " + supportedNameFormats);
591         }
592
593         if (principalAttributes == null || supportedNameFormats == null) {
594             log.error("No attributes for principal " + requestContext.getPrincipalName() 
595                     + " support constructions of NameID");
596             requestContext.setFailureStatus(buildStatus(StatusCode.RESPONDER_URI, StatusCode.INVALID_NAMEID_POLICY_URI,
597                     "Unable to construct NameID"));
598             throw new ProfileException("No principal attributes support NameID construction");
599         }
600
601         try {
602             SAML2NameIDAttributeEncoder nameIdEncoder;
603             for (BaseAttribute<?> attribute : principalAttributes.values()) {
604                 for (AttributeEncoder encoder : attribute.getEncoders()) {
605                     if (encoder instanceof SAML2NameIDAttributeEncoder) {
606                         nameIdEncoder = (SAML2NameIDAttributeEncoder) encoder;
607                         if (supportedNameFormats.contains(nameIdEncoder.getNameFormat())) {
608                             if (log.isDebugEnabled()) {
609                                 log.debug("Using attribute " + attribute.getId() + " suppoting NameID format "
610                                         + nameIdEncoder.getNameFormat() + " to create the NameID for principal "
611                                         + requestContext.getPrincipalName());
612                             }
613                             return nameIdEncoder.encode(attribute);
614                         }
615                     }
616                 }
617             }
618             requestContext.setFailureStatus(buildStatus(StatusCode.RESPONDER_URI, null, "Unable to construct NameID"));
619             throw new ProfileException("No principal attribute supported encoding into the a supported name ID format.");
620         } catch (AttributeEncodingException e) {
621             requestContext.setFailureStatus(buildStatus(StatusCode.RESPONDER_URI, null, "Unable to construct NameID"));
622             throw new ProfileException("Unable to encode NameID attribute", e);
623         }
624     }
625
626     /**
627      * Gets the NameID format to use when creating NameIDs for the relying party.
628      * 
629      * @param requestContext current request context
630      * 
631      * @return list of nameID formats that may be used with the relying party
632      * 
633      * @throws ProfileException thrown if there is a problem determing the NameID format to use
634      */
635     protected List<String> getNameFormats(SAML2ProfileRequestContext requestContext) throws ProfileException {
636         ArrayList<String> nameFormats = new ArrayList<String>();
637
638         List<String> assertingPartySupportedFormats = getEntitySupportedFormats(requestContext
639                 .getAssertingPartyRoleMetadata());
640
641         String nameFormat = null;
642         if (requestContext.getSamlRequest() instanceof AuthnRequest) {
643             AuthnRequest authnRequest = (AuthnRequest) requestContext.getSamlRequest();
644             if (authnRequest.getNameIDPolicy() != null) {
645                 nameFormat = authnRequest.getNameIDPolicy().getFormat();
646                 if (assertingPartySupportedFormats.contains(nameFormat)) {
647                     nameFormats.add(nameFormat);
648                 } else {
649                     requestContext.setFailureStatus(buildStatus(StatusCode.RESPONDER_URI,
650                             StatusCode.INVALID_NAMEID_POLICY_URI, "Format not supported: " + nameFormat));
651                     throw new ProfileException("NameID format required by relying party is not supported");
652                 }
653             }
654         }
655
656         if (nameFormats.isEmpty()) {
657             List<String> relyingPartySupportedFormats = getEntitySupportedFormats(requestContext
658                     .getRelyingPartyRoleMetadata());
659
660             assertingPartySupportedFormats.retainAll(relyingPartySupportedFormats);
661             nameFormats.addAll(assertingPartySupportedFormats);
662         }
663         if (nameFormats.isEmpty()) {
664             nameFormats.add("urn:oasis:names:tc:SAML:2.0:nameid-format:unspecified");
665         }
666
667         return nameFormats;
668     }
669
670     /**
671      * Gets the list of NameID formats supported for a given role.
672      * 
673      * @param role the role to get the list of supported NameID formats
674      * 
675      * @return list of supported NameID formats
676      */
677     protected List<String> getEntitySupportedFormats(RoleDescriptor role) {
678         List<NameIDFormat> nameIDFormats = null;
679
680         if (role instanceof SSODescriptor) {
681             nameIDFormats = ((SSODescriptor) role).getNameIDFormats();
682         } else if (role instanceof AuthnAuthorityDescriptor) {
683             nameIDFormats = ((AuthnAuthorityDescriptor) role).getNameIDFormats();
684         } else if (role instanceof PDPDescriptor) {
685             nameIDFormats = ((PDPDescriptor) role).getNameIDFormats();
686         } else if (role instanceof AttributeAuthorityDescriptor) {
687             nameIDFormats = ((AttributeAuthorityDescriptor) role).getNameIDFormats();
688         }
689
690         ArrayList<String> supportedFormats = new ArrayList<String>();
691         if (nameIDFormats != null) {
692             for (NameIDFormat format : nameIDFormats) {
693                 supportedFormats.add(format.getFormat());
694             }
695         }
696
697         return supportedFormats;
698     }
699
700     /**
701      * Constructs an SAML response message carrying a request error.
702      * 
703      * @param requestContext current request context
704      * 
705      * @return the constructed error response
706      */
707     protected Response buildErrorResponse(SAML2ProfileRequestContext requestContext) {
708         Response samlResponse = responseBuilder.buildObject();
709         samlResponse.setIssueInstant(new DateTime());
710         populateStatusResponse(requestContext, samlResponse);
711
712         samlResponse.setStatus(requestContext.getFailureStatus());
713
714         return samlResponse;
715     }
716
717     /**
718      * Writes an aduit log entry indicating the successful response to the attribute request.
719      * 
720      * @param context current request context
721      */
722     protected void writeAuditLogEntry(SAML2ProfileRequestContext context) {
723         AuditLogEntry auditLogEntry = new AuditLogEntry();
724         auditLogEntry.setMessageProfile(getProfileId());
725         auditLogEntry.setPrincipalAuthenticationMethod(context.getPrincipalAuthenticationMethod());
726         auditLogEntry.setPrincipalName(context.getPrincipalName());
727         auditLogEntry.setAssertingPartyId(context.getAssertingPartyId());
728         auditLogEntry.setRelyingPartyId(context.getRelyingPartyId());
729         auditLogEntry.setRequestBinding(context.getMessageDecoder().getBindingURI());
730         auditLogEntry.setRequestId(context.getSamlRequest().getID());
731         auditLogEntry.setResponseBinding(context.getMessageEncoder().getBindingURI());
732         auditLogEntry.setResponseId(context.getSamlResponse().getID());
733         if(context.getPrincipalAttributes() != null){
734             auditLogEntry.getReleasedAttributes().addAll(context.getPrincipalAttributes().keySet());
735         }
736         getAduitLog().log(Level.CRITICAL, auditLogEntry);
737     }
738
739     /**
740      * Contextual object used to accumlate information as profile requests are being processed.
741      * 
742      * @param <RequestType> type of SAML 2 request
743      * @param <ResponseType> type of SAML 2 response
744      * @param <ProfileConfigurationType> configuration type for this profile
745      */
746     protected class SAML2ProfileRequestContext<RequestType extends RequestAbstractType, ResponseType extends StatusResponseType, ProfileConfigurationType extends AbstractSAML2ProfileConfiguration>
747             extends SAMLProfileRequestContext {
748
749         /** SAML request message. */
750         private RequestType samlRequest;
751
752         /** SAML response message. */
753         private ResponseType samlResponse;
754
755         /** Request profile configuration. */
756         private ProfileConfigurationType profileConfiguration;
757
758         /** The NameID of the subject of this request. */
759         private NameID subjectNameID;
760
761         /** The request failure status. */
762         private Status failureStatus;
763
764         /**
765          * Constructor.
766          * 
767          * @param request current profile request
768          * @param response current profile response
769          */
770         public SAML2ProfileRequestContext(ProfileRequest<ServletRequest> request,
771                 ProfileResponse<ServletResponse> response) {
772             super(request, response);
773         }
774
775         /**
776          * Gets the NameID of the subject of this request.
777          * 
778          * @return NameID of the subject of this request
779          */
780         public NameID getSubjectNameID() {
781             return subjectNameID;
782         }
783
784         /**
785          * Sets the NameID of the subject of this request.
786          * 
787          * @param nameID NameID of the subject of this request
788          */
789         public void setSubjectNameID(NameID nameID) {
790             subjectNameID = nameID;
791         }
792
793         /**
794          * Gets the profile configuration for this request.
795          * 
796          * @return profile configuration for this request
797          */
798         public ProfileConfigurationType getProfileConfiguration() {
799             return profileConfiguration;
800         }
801
802         /**
803          * Sets the profile configuration for this request.
804          * 
805          * @param configuration profile configuration for this request
806          */
807         public void setProfileConfiguration(ProfileConfigurationType configuration) {
808             profileConfiguration = configuration;
809         }
810
811         /**
812          * Gets the SAML request message.
813          * 
814          * @return SAML request message
815          */
816         public RequestType getSamlRequest() {
817             return samlRequest;
818         }
819
820         /**
821          * Sets the SAML request message.
822          * 
823          * @param request SAML request message
824          */
825         public void setSamlRequest(RequestType request) {
826             samlRequest = request;
827         }
828
829         /**
830          * Gets the SAML response message.
831          * 
832          * @return SAML response message
833          */
834         public ResponseType getSamlResponse() {
835             return samlResponse;
836         }
837
838         /**
839          * Sets the SAML response message.
840          * 
841          * @param response SAML response message
842          */
843         public void setSamlResponse(ResponseType response) {
844             samlResponse = response;
845         }
846
847         /**
848          * Gets the status reflecting a request failure.
849          * 
850          * @return status reflecting a request failure
851          */
852         public Status getFailureStatus() {
853             return failureStatus;
854         }
855
856         /**
857          * Sets the status reflecting a request failure.
858          * 
859          * @param status status reflecting a request failure
860          */
861         public void setFailureStatus(Status status) {
862             failureStatus = status;
863         }
864     }
865 }