2 * Copyright [2006] [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;
21 import javax.servlet.ServletRequest;
22 import javax.servlet.ServletResponse;
24 import org.apache.log4j.Logger;
25 import org.opensaml.common.binding.BindingException;
26 import org.opensaml.common.binding.decoding.MessageDecoder;
27 import org.opensaml.common.binding.encoding.MessageEncoder;
28 import org.opensaml.common.binding.security.SAMLSecurityPolicy;
29 import org.opensaml.saml1.binding.decoding.HTTPSOAP11Decoder;
30 import org.opensaml.saml1.binding.encoding.HTTPSOAP11Encoder;
31 import org.opensaml.saml1.core.AttributeQuery;
32 import org.opensaml.saml1.core.Response;
33 import org.opensaml.saml1.core.Statement;
34 import org.opensaml.saml1.core.StatusCode;
35 import org.opensaml.saml2.metadata.AttributeAuthorityDescriptor;
36 import org.opensaml.saml2.metadata.SPSSODescriptor;
37 import org.opensaml.ws.security.SecurityPolicyException;
39 import edu.internet2.middleware.shibboleth.common.profile.ProfileException;
40 import edu.internet2.middleware.shibboleth.common.profile.ProfileRequest;
41 import edu.internet2.middleware.shibboleth.common.profile.ProfileResponse;
42 import edu.internet2.middleware.shibboleth.common.relyingparty.RelyingPartyConfiguration;
43 import edu.internet2.middleware.shibboleth.common.relyingparty.provider.saml1.AttributeQueryConfiguration;
46 * SAML 1 Attribute Query profile handler.
48 public class AttributeQueryProfileHandler extends AbstractSAML1ProfileHandler {
51 private final Logger log = Logger.getLogger(AttributeQueryProfileHandler.class);
54 public String getProfileId() {
55 return "urn:mace:shibboleth:2.0:idp:profiles:saml1:query:attribute";
59 public void processRequest(ProfileRequest<ServletRequest> request, ProfileResponse<ServletResponse> response)
60 throws ProfileException {
62 AttributeQueryContext requestContext = new AttributeQueryContext(request, response);
64 Response samlResponse;
66 decodeRequest(requestContext);
68 resolvePrincipal(requestContext);
70 ArrayList<Statement> statements = new ArrayList<Statement>();
71 statements.add(buildAttributeStatement(requestContext, "urn:oasis:names:tc:SAML:1.0:cm:sender-vouches"));
73 samlResponse = buildResponse(requestContext, statements);
74 } catch (ProfileException e) {
75 samlResponse = buildErrorResponse(requestContext);
80 * Decodes the message in the request and adds it to the request context.
82 * @param requestContext request context contianing the request to decode
84 * @throws ProfileException throw if there is a problem decoding the request
86 protected void decodeRequest(AttributeQueryContext requestContext) throws ProfileException {
87 if (log.isDebugEnabled()) {
88 log.debug("Decoding incomming request");
90 MessageDecoder<ServletRequest> decoder = getMessageDecoderFactory().getMessageDecoder(
91 HTTPSOAP11Decoder.BINDING_URI);
92 if (decoder == null) {
93 throw new ProfileException("No request decoder was registered for binding type: "
94 + HTTPSOAP11Decoder.BINDING_URI);
96 super.populateMessageDecoder(decoder);
98 decoder.setRequest(requestContext.getProfileRequest().getRawRequest());
99 requestContext.setMessageDecoder(decoder);
103 if (log.isDebugEnabled()) {
104 log.debug("Decoded request");
106 } catch (BindingException e) {
107 log.error("Error decoding attribute query message", e);
108 requestContext.setFailureStatus(buildStatus(StatusCode.RESPONDER, null, "Error decoding message"));
109 throw new ProfileException("Error decoding attribute query message");
110 } catch (SecurityPolicyException e) {
111 log.error("Message did not meet security policy requirements", e);
112 requestContext.setFailureStatus(buildStatus(StatusCode.RESPONDER, StatusCode.REQUEST_DENIED,
113 "Message did not meet security policy requirements"));
114 throw new ProfileException("Message did not meet security policy requirements", e);
116 // Set as much information as can be retrieved from the decoded message
117 SAMLSecurityPolicy securityPolicy = requestContext.getMessageDecoder().getSecurityPolicy();
118 requestContext.setRelyingPartyId(securityPolicy.getIssuer());
120 RelyingPartyConfiguration rpConfig = getRelyingPartyConfiguration(requestContext.getRelyingPartyId());
121 requestContext.setRelyingPartyConfiguration(rpConfig);
123 requestContext.setRelyingPartyRole(SPSSODescriptor.DEFAULT_ELEMENT_NAME);
125 requestContext.setAssertingPartyId(requestContext.getRelyingPartyConfiguration().getProviderId());
127 requestContext.setAssertingPartyRole(AttributeAuthorityDescriptor.DEFAULT_ELEMENT_NAME);
129 requestContext.setProfileConfiguration((AttributeQueryConfiguration) rpConfig
130 .getProfileConfiguration(AttributeQueryConfiguration.PROFILE_ID));
132 requestContext.setSamlRequest((AttributeQuery) requestContext.getMessageDecoder().getSAMLMessage());
137 * Encodes the request's SAML response and writes it to the servlet response.
139 * @param requestContext current request context
141 * @throws ProfileException thrown if no message encoder is registered for this profiles binding
143 protected void encodeResponse(AttributeQueryContext requestContext) throws ProfileException {
144 if (log.isDebugEnabled()) {
145 log.debug("Encoding response to SAML request from relying party " + requestContext.getRelyingPartyId());
147 MessageEncoder<ServletResponse> encoder = getMessageEncoderFactory().getMessageEncoder(
148 HTTPSOAP11Encoder.BINDING_URI);
149 if (encoder == null) {
150 throw new ProfileException("No response encoder was registered for binding type: "
151 + HTTPSOAP11Encoder.BINDING_URI);
154 super.populateMessageEncoder(encoder);
155 encoder.setResponse(requestContext.getProfileResponse().getRawResponse());
156 encoder.setSamlMessage(requestContext.getSamlResponse());
157 requestContext.setMessageEncoder(encoder);
161 } catch (BindingException e) {
162 throw new ProfileException("Unable to encode response to relying party: "
163 + requestContext.getRelyingPartyId(), e);
167 /** Basic data structure used to accumulate information as a request is being processed. */
168 protected class AttributeQueryContext extends
169 SAML1ProfileRequestContext<AttributeQuery, Response, AttributeQueryConfiguration> {
174 * @param request current profile request
175 * @param response current profile response
177 public AttributeQueryContext(ProfileRequest<ServletRequest> request, ProfileResponse<ServletResponse> response) {
178 super(request, response);