Have decoders extract basic SAML message information (message ID, claimed issuer...
[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.apache.log4j.Logger;
22 import org.opensaml.common.binding.decoding.SAMLMessageDecoder;
23 import org.opensaml.common.xml.SAMLConstants;
24 import org.opensaml.saml2.core.AttributeQuery;
25 import org.opensaml.saml2.core.Response;
26 import org.opensaml.saml2.core.Statement;
27 import org.opensaml.saml2.core.StatusCode;
28 import org.opensaml.saml2.metadata.AttributeAuthorityDescriptor;
29 import org.opensaml.saml2.metadata.SPSSODescriptor;
30 import org.opensaml.saml2.metadata.provider.MetadataProvider;
31 import org.opensaml.saml2.metadata.provider.MetadataProviderException;
32 import org.opensaml.ws.message.decoder.MessageDecodingException;
33 import org.opensaml.ws.security.SecurityPolicyException;
34 import org.opensaml.ws.transport.http.HTTPInTransport;
35 import org.opensaml.ws.transport.http.HTTPOutTransport;
36
37 import edu.internet2.middleware.shibboleth.common.profile.ProfileException;
38 import edu.internet2.middleware.shibboleth.common.relyingparty.RelyingPartyConfiguration;
39 import edu.internet2.middleware.shibboleth.common.relyingparty.provider.saml2.AttributeQueryConfiguration;
40
41 /** SAML 2.0 Attribute Query profile handler. */
42 public class AttributeQueryProfileHandler extends AbstractSAML2ProfileHandler {
43
44     /** Class logger. */
45     private static Logger log = Logger.getLogger(AttributeQueryProfileHandler.class);
46
47     /** {@inheritDoc} */
48     public String getProfileId() {
49         return "urn:mace:shibboleth:2.0:idp:profiles:saml2:query:attribute";
50     }
51
52     /** {@inheritDoc} */
53     public void processRequest(HTTPInTransport inTransport, HTTPOutTransport outTransport) throws ProfileException {
54         Response samlResponse;
55
56         AttributeQueryContext requestContext = decodeRequest(inTransport, outTransport);
57
58         try {
59             if (requestContext.getRelyingPartyConfiguration() == null) {
60                 log.error("SAML 2 Attribute Query profile is not configured for relying party "
61                         + requestContext.getPeerEntityId());
62                 requestContext.setFailureStatus(buildStatus(StatusCode.RESPONDER_URI, StatusCode.REQUEST_DENIED_URI,
63                         "SAML 2 Attribute Query profile is not configured for relying party "
64                                 + requestContext.getPeerEntityId()));
65                 samlResponse = buildErrorResponse(requestContext);
66             }
67
68             checkSamlVersion(requestContext);
69
70             // Resolve attribute query name id to principal name and place in context
71             resolvePrincipal(requestContext);
72             resolveAttributes(requestContext);
73             requestContext.setReleasedAttributes(requestContext.getPrincipalAttributes().keySet());
74
75             // Lookup principal name and attributes, create attribute statement from information
76             ArrayList<Statement> statements = new ArrayList<Statement>();
77             statements.add(buildAttributeStatement(requestContext));
78
79             // create the SAML response
80             samlResponse = buildResponse(requestContext, "urn:oasis:names:tc:SAML:2.0:cm:sender-vouches", statements);
81         } catch (ProfileException e) {
82             samlResponse = buildErrorResponse(requestContext);
83         }
84
85         requestContext.setOutboundSAMLMessage(samlResponse);
86         requestContext.setOutboundSAMLMessageId(samlResponse.getID());
87         requestContext.setOutboundSAMLMessageIssueInstant(samlResponse.getIssueInstant());
88
89         encodeResponse(requestContext);
90         writeAuditLogEntry(requestContext);
91     }
92
93     /**
94      * Decodes an incoming request and populates a created request context with the resultant information.
95      * 
96      * @param inTransport inbound message transport
97      * @param outTransport outbound message transport
98      * 
99      * @return the created request context
100      * 
101      * @throws ProfileException throw if there is a problem decoding the request
102      */
103     protected AttributeQueryContext decodeRequest(HTTPInTransport inTransport, HTTPOutTransport outTransport)
104             throws ProfileException {
105         if (log.isDebugEnabled()) {
106             log.debug("Decoding incomming request");
107         }
108
109         MetadataProvider metadataProvider = getMetadataProvider();
110
111         AttributeQueryContext requestContext = new AttributeQueryContext();
112         requestContext.setMetadataProvider(metadataProvider);
113         
114         requestContext.setInboundMessageTransport(inTransport);
115         requestContext.setInboundSAMLProtocol(SAMLConstants.SAML20P_NS);
116         requestContext.setPeerEntityRole(SPSSODescriptor.DEFAULT_ELEMENT_NAME);
117
118         
119         requestContext.setOutboundMessageTransport(outTransport);
120         requestContext.setOutboundSAMLProtocol(SAMLConstants.SAML20P_NS);
121
122         try {
123             SAMLMessageDecoder decoder = getMessageDecoders().get(getInboundBinding());
124             requestContext.setMessageDecoder(decoder);
125             decoder.decode(requestContext);
126             if (log.isDebugEnabled()) {
127                 log.debug("Decoded request");
128             }
129             return requestContext;
130         } catch (MessageDecodingException e) {
131             log.error("Error decoding attribute query message", e);
132             requestContext.setFailureStatus(buildStatus(StatusCode.RESPONDER_URI, null, "Error decoding message"));
133             throw new ProfileException("Error decoding attribute query message");
134         } catch (SecurityPolicyException e) {
135             log.error("Message did not meet security policy requirements", e);
136             requestContext.setFailureStatus(buildStatus(StatusCode.RESPONDER_URI, StatusCode.REQUEST_DENIED_URI,
137                     "Message did not meet security policy requirements"));
138             throw new ProfileException("Message did not meet security policy requirements", e);
139         } finally {
140             // Set as much information as can be retrieved from the decoded message
141             try {
142                 String relyingPartyId = requestContext.getPeerEntityId();
143                 RelyingPartyConfiguration rpConfig = getRelyingPartyConfiguration(relyingPartyId);
144                 requestContext.setRelyingPartyConfiguration(rpConfig);
145
146                 String assertingPartyId = requestContext.getRelyingPartyConfiguration().getProviderId();
147                 requestContext.setLocalEntityId(assertingPartyId);
148                 requestContext.setLocalEntityMetadata(metadataProvider.getEntityDescriptor(assertingPartyId));
149                 requestContext.setLocalEntityRole(AttributeAuthorityDescriptor.DEFAULT_ELEMENT_NAME);
150                 requestContext.setLocalEntityRoleMetadata(requestContext.getLocalEntityMetadata()
151                         .getAttributeAuthorityDescriptor(SAMLConstants.SAML20P_NS));
152
153                 AttributeQueryConfiguration profileConfig = (AttributeQueryConfiguration) rpConfig
154                         .getProfileConfiguration(AttributeQueryConfiguration.PROFILE_ID);
155                 requestContext.setProfileConfiguration(profileConfig);
156                 requestContext.setOutboundMessageArtifactType(profileConfig.getOutboundArtifactType());
157                 if (profileConfig.getSigningCredential() != null) {
158                     requestContext.setOutboundSAMLMessageSigningCredential(profileConfig.getSigningCredential());
159                 } else if (rpConfig.getDefaultSigningCredential() != null) {
160                     requestContext.setOutboundSAMLMessageSigningCredential(rpConfig.getDefaultSigningCredential());
161                 }
162
163             } catch (MetadataProviderException e) {
164                 log.error("Unable to locate metadata for asserting or relying party");
165                 requestContext.setFailureStatus(buildStatus(StatusCode.RESPONDER_URI, null,
166                         "Error locating party metadata"));
167                 throw new ProfileException("Error locating party metadata");
168             }
169         }
170     }
171
172     /** Basic data structure used to accumulate information as a request is being processed. */
173     protected class AttributeQueryContext extends
174             BaseSAML2ProfileRequestContext<AttributeQuery, Response, AttributeQueryConfiguration> {
175
176     }
177 }