move decoing phase into try/catch
[java-idp.git] / src / edu / internet2 / middleware / shibboleth / idp / profile / saml2 / AttributeQueryProfileHandler.java
1 /*
2  * Copyright [2006] [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
21 import org.opensaml.common.SAMLObjectBuilder;
22 import org.opensaml.common.binding.BasicEndpointSelector;
23 import org.opensaml.common.binding.decoding.SAMLMessageDecoder;
24 import org.opensaml.common.xml.SAMLConstants;
25 import org.opensaml.saml2.core.AttributeQuery;
26 import org.opensaml.saml2.core.AttributeStatement;
27 import org.opensaml.saml2.core.Response;
28 import org.opensaml.saml2.core.Statement;
29 import org.opensaml.saml2.core.StatusCode;
30 import org.opensaml.saml2.core.Subject;
31 import org.opensaml.saml2.metadata.AssertionConsumerService;
32 import org.opensaml.saml2.metadata.AttributeAuthorityDescriptor;
33 import org.opensaml.saml2.metadata.Endpoint;
34 import org.opensaml.saml2.metadata.EntityDescriptor;
35 import org.opensaml.saml2.metadata.SPSSODescriptor;
36 import org.opensaml.saml2.metadata.provider.MetadataProvider;
37 import org.opensaml.ws.message.decoder.MessageDecodingException;
38 import org.opensaml.ws.transport.http.HTTPInTransport;
39 import org.opensaml.ws.transport.http.HTTPOutTransport;
40 import org.opensaml.xml.security.SecurityException;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 import edu.internet2.middleware.shibboleth.common.profile.ProfileException;
45 import edu.internet2.middleware.shibboleth.common.profile.provider.BaseSAMLProfileRequestContext;
46 import edu.internet2.middleware.shibboleth.common.relyingparty.provider.saml2.AttributeQueryConfiguration;
47 import edu.internet2.middleware.shibboleth.idp.session.AuthenticationMethodInformation;
48 import edu.internet2.middleware.shibboleth.idp.session.Session;
49
50 /** SAML 2.0 Attribute Query profile handler. */
51 public class AttributeQueryProfileHandler extends AbstractSAML2ProfileHandler {
52
53     /** Class logger. */
54     private static Logger log = LoggerFactory.getLogger(AttributeQueryProfileHandler.class);
55
56     /** Builder of assertion consumer service endpoints. */
57     private SAMLObjectBuilder<AssertionConsumerService> acsEndpointBuilder;
58
59     /** Constructor. */
60     public AttributeQueryProfileHandler() {
61         super();
62
63         acsEndpointBuilder = (SAMLObjectBuilder<AssertionConsumerService>) getBuilderFactory().getBuilder(
64                 AssertionConsumerService.DEFAULT_ELEMENT_NAME);
65     }
66
67     /** {@inheritDoc} */
68     public String getProfileId() {
69         return AttributeQueryConfiguration.PROFILE_ID;
70     }
71
72     /** {@inheritDoc} */
73     public void processRequest(HTTPInTransport inTransport, HTTPOutTransport outTransport) throws ProfileException {
74         Response samlResponse;
75
76         AttributeQueryContext requestContext = null;
77
78         try {
79             requestContext = decodeRequest(inTransport, outTransport);
80             
81             if (requestContext.getProfileConfiguration() == null) {
82                 log.error("SAML 2 Attribute Query profile is not configured for relying party "
83                         + requestContext.getInboundMessageIssuer());
84                 requestContext.setFailureStatus(buildStatus(StatusCode.RESPONDER_URI, StatusCode.REQUEST_DENIED_URI,
85                         "SAML 2 Attribute Query profile is not configured for relying party "
86                                 + requestContext.getInboundMessageIssuer()));
87                 samlResponse = buildErrorResponse(requestContext);
88             } else {
89                 checkSamlVersion(requestContext);
90
91                 // Resolve attribute query name id to principal name and place in context
92                 resolvePrincipal(requestContext);
93
94                 Session idpSession = getSessionManager().getSession(requestContext.getPrincipalName());
95                 if (idpSession != null) {
96                     AuthenticationMethodInformation authnInfo = idpSession.getAuthenticationMethods().get(
97                             requestContext.getInboundMessageIssuer());
98                     if (authnInfo != null) {
99                         requestContext.setPrincipalAuthenticationMethod(authnInfo.getAuthenticationMethod());
100                     }
101                 }
102
103                 resolveAttributes(requestContext);
104                 requestContext.setReleasedAttributes(requestContext.getAttributes().keySet());
105
106                 // Lookup principal name and attributes, create attribute statement from information
107                 ArrayList<Statement> statements = new ArrayList<Statement>();
108                 AttributeStatement attributeStatement = buildAttributeStatement(requestContext);
109                 if (attributeStatement != null) {
110                     statements.add(attributeStatement);
111                 }
112
113                 // create the SAML response
114                 samlResponse = buildResponse(requestContext, "urn:oasis:names:tc:SAML:2.0:cm:sender-vouches",
115                         statements);
116             }
117         } catch (ProfileException e) {
118             samlResponse = buildErrorResponse(requestContext);
119         }
120
121         requestContext.setOutboundSAMLMessage(samlResponse);
122         requestContext.setOutboundSAMLMessageId(samlResponse.getID());
123         requestContext.setOutboundSAMLMessageIssueInstant(samlResponse.getIssueInstant());
124
125         encodeResponse(requestContext);
126         writeAuditLogEntry(requestContext);
127     }
128
129     /**
130      * Decodes an incoming request and populates a created request context with the resultant information.
131      * 
132      * @param inTransport inbound message transport
133      * @param outTransport outbound message transport
134      * 
135      * @return the created request context
136      * 
137      * @throws ProfileException throw if there is a problem decoding the request
138      */
139     protected AttributeQueryContext decodeRequest(HTTPInTransport inTransport, HTTPOutTransport outTransport)
140             throws ProfileException {
141         log.debug("Decoding message with decoder binding {}", getInboundBinding());
142
143         AttributeQueryContext requestContext = new AttributeQueryContext();
144
145         MetadataProvider metadataProvider = getMetadataProvider();
146         requestContext.setMetadataProvider(metadataProvider);
147
148         requestContext.setInboundMessageTransport(inTransport);
149         requestContext.setInboundSAMLProtocol(SAMLConstants.SAML20P_NS);
150         requestContext.setSecurityPolicyResolver(getSecurityPolicyResolver());
151         requestContext.setPeerEntityRole(SPSSODescriptor.DEFAULT_ELEMENT_NAME);
152
153         requestContext.setOutboundMessageTransport(outTransport);
154         requestContext.setOutboundSAMLProtocol(SAMLConstants.SAML20P_NS);
155
156         try {
157             SAMLMessageDecoder decoder = getMessageDecoders().get(getInboundBinding());
158             requestContext.setMessageDecoder(decoder);
159             decoder.decode(requestContext);
160             log.debug("Decoded request");
161
162             if (!(requestContext.getInboundSAMLMessage() instanceof AttributeQuery)) {
163                 log.error("Incoming message was not a AttributeQuery, it was a {}", requestContext
164                         .getInboundSAMLMessage().getClass().getName());
165                 requestContext.setFailureStatus(buildStatus(StatusCode.REQUESTER_URI, null,
166                         "Invalid SAML AttributeQuery message."));
167                 throw new ProfileException("Invalid SAML AttributeQuery message.");
168             }
169
170             return requestContext;
171         } catch (MessageDecodingException e) {
172             log.error("Error decoding attribute query message", e);
173             requestContext.setFailureStatus(buildStatus(StatusCode.RESPONDER_URI, null, "Error decoding message"));
174             throw new ProfileException("Error decoding attribute query message");
175         } catch (SecurityException e) {
176             log.error("Message did not meet security requirements", e);
177             requestContext.setFailureStatus(buildStatus(StatusCode.RESPONDER_URI, StatusCode.REQUEST_DENIED_URI,
178                     "Message did not meet security requirements"));
179             throw new ProfileException("Message did not meet security requirements", e);
180         } finally {
181             // Set as much information as can be retrieved from the decoded message
182             populateRequestContext(requestContext);
183             populateSAMLMessageInformation(requestContext);
184             populateProfileInformation(requestContext);
185         }
186     }
187
188     /** {@inheritDoc} */
189     protected void populateRelyingPartyInformation(BaseSAMLProfileRequestContext requestContext)
190             throws ProfileException {
191         super.populateRelyingPartyInformation(requestContext);
192
193         EntityDescriptor relyingPartyMetadata = requestContext.getPeerEntityMetadata();
194         if (relyingPartyMetadata != null) {
195             requestContext.setPeerEntityRole(SPSSODescriptor.DEFAULT_ELEMENT_NAME);
196             requestContext.setPeerEntityRoleMetadata(relyingPartyMetadata.getSPSSODescriptor(SAMLConstants.SAML20P_NS));
197         }
198     }
199
200     /** {@inheritDoc} */
201     protected void populateAssertingPartyInformation(BaseSAMLProfileRequestContext requestContext)
202             throws ProfileException {
203         super.populateAssertingPartyInformation(requestContext);
204
205         EntityDescriptor localEntityDescriptor = requestContext.getLocalEntityMetadata();
206         if (localEntityDescriptor != null) {
207             requestContext.setLocalEntityRole(AttributeAuthorityDescriptor.DEFAULT_ELEMENT_NAME);
208             requestContext.setLocalEntityRoleMetadata(localEntityDescriptor
209                     .getAttributeAuthorityDescriptor(SAMLConstants.SAML20P_NS));
210         }
211     }
212
213     /**
214      * Populates the request context with information from the inbound SAML message.
215      * 
216      * This method requires the the following request context properties to be populated: inbound saml message
217      * 
218      * This methods populates the following request context properties: subject name identifier
219      * 
220      * @param requestContext current request context
221      * 
222      * @throws ProfileException thrown if the inbound SAML message or subject identifier is null
223      */
224     protected void populateSAMLMessageInformation(BaseSAMLProfileRequestContext requestContext) throws ProfileException {
225         AttributeQuery query = (AttributeQuery) requestContext.getInboundSAMLMessage();
226         if (query != null) {
227             Subject subject = query.getSubject();
228             if (subject == null) {
229                 log.error("Attribute query did not contain a proper subject");
230                 ((AttributeQueryContext) requestContext).setFailureStatus(buildStatus(StatusCode.REQUESTER_URI, null,
231                         "Attribute query did not contain a proper subject"));
232                 throw new ProfileException("Attribute query did not contain a proper subject");
233             }
234             requestContext.setSubjectNameIdentifier(subject.getNameID());
235         }
236     }
237
238     /**
239      * Selects the appropriate endpoint for the relying party and stores it in the request context.
240      * 
241      * @param requestContext current request context
242      * 
243      * @return Endpoint selected from the information provided in the request context
244      */
245     protected Endpoint selectEndpoint(BaseSAMLProfileRequestContext requestContext) {
246         Endpoint endpoint;
247
248         if (getInboundBinding().equals(SAMLConstants.SAML2_SOAP11_BINDING_URI)) {
249             endpoint = acsEndpointBuilder.buildObject();
250             endpoint.setBinding(SAMLConstants.SAML2_SOAP11_BINDING_URI);
251         } else {
252             BasicEndpointSelector endpointSelector = new BasicEndpointSelector();
253             endpointSelector.setEndpointType(AssertionConsumerService.DEFAULT_ELEMENT_NAME);
254             endpointSelector.setMetadataProvider(getMetadataProvider());
255             endpointSelector.setEntityMetadata(requestContext.getPeerEntityMetadata());
256             endpointSelector.setEntityRoleMetadata(requestContext.getPeerEntityRoleMetadata());
257             endpointSelector.setSamlRequest(requestContext.getInboundSAMLMessage());
258             endpointSelector.getSupportedIssuerBindings().addAll(getSupportedOutboundBindings());
259             endpoint = endpointSelector.selectEndpoint();
260         }
261
262         return endpoint;
263     }
264
265     /** Basic data structure used to accumulate information as a request is being processed. */
266     protected class AttributeQueryContext extends
267             BaseSAML2ProfileRequestContext<AttributeQuery, Response, AttributeQueryConfiguration> {
268
269     }
270 }