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.Request;
33 import org.opensaml.saml1.core.Response;
34 import org.opensaml.saml1.core.Statement;
35 import org.opensaml.saml1.core.StatusCode;
36 import org.opensaml.saml2.metadata.RoleDescriptor;
37 import org.opensaml.saml2.metadata.provider.MetadataProviderException;
38 import org.opensaml.ws.security.SecurityPolicyException;
40 import edu.internet2.middleware.shibboleth.common.ShibbolethConstants;
41 import edu.internet2.middleware.shibboleth.common.profile.ProfileException;
42 import edu.internet2.middleware.shibboleth.common.profile.ProfileRequest;
43 import edu.internet2.middleware.shibboleth.common.profile.ProfileResponse;
44 import edu.internet2.middleware.shibboleth.common.relyingparty.RelyingPartyConfiguration;
45 import edu.internet2.middleware.shibboleth.common.relyingparty.provider.saml1.AttributeQueryConfiguration;
48 * SAML 1 Attribute Query profile handler.
50 public class AttributeQueryProfileHandler extends AbstractSAML1ProfileHandler {
53 private final Logger log = Logger.getLogger(AttributeQueryProfileHandler.class);
56 public String getProfileId() {
57 return "urn:mace:shibboleth:2.0:idp:profiles:saml1:query:attribute";
61 public void processRequest(ProfileRequest<ServletRequest> request, ProfileResponse<ServletResponse> response)
62 throws ProfileException {
64 AttributeQueryContext requestContext = new AttributeQueryContext(request, response);
66 Response samlResponse;
68 decodeRequest(requestContext);
70 if (requestContext.getRelyingPartyConfiguration() == null) {
71 log.error("SAML 1 Attribute Query profile is not configured for relying party "
72 + requestContext.getRelyingPartyId());
73 requestContext.setFailureStatus(buildStatus(StatusCode.RESPONDER, StatusCode.REQUEST_DENIED,
74 "SAML 1 Attribute Query profile is not configured for relying party "
75 + requestContext.getRelyingPartyId()));
76 samlResponse = buildErrorResponse(requestContext);
79 resolvePrincipal(requestContext);
81 ArrayList<Statement> statements = new ArrayList<Statement>();
82 statements.add(buildAttributeStatement(requestContext, "urn:oasis:names:tc:SAML:1.0:cm:sender-vouches"));
84 samlResponse = buildResponse(requestContext, statements);
85 } catch (ProfileException e) {
86 samlResponse = buildErrorResponse(requestContext);
89 requestContext.setSamlResponse(samlResponse);
90 encodeResponse(requestContext);
94 * Decodes the message in the request and adds it to the request context.
96 * @param requestContext request context contianing the request to decode
98 * @throws ProfileException throw if there is a problem decoding the request
100 protected void decodeRequest(AttributeQueryContext requestContext) throws ProfileException {
101 if (log.isDebugEnabled()) {
102 log.debug("Decoding incomming request");
104 MessageDecoder<ServletRequest> decoder = getMessageDecoderFactory().getMessageDecoder(
105 HTTPSOAP11Decoder.BINDING_URI);
106 if (decoder == null) {
107 throw new ProfileException("No request decoder was registered for binding type: "
108 + HTTPSOAP11Decoder.BINDING_URI);
110 super.populateMessageDecoder(decoder);
112 ProfileRequest<ServletRequest> profileRequest = requestContext.getProfileRequest();
113 decoder.setRequest(profileRequest.getRawRequest());
114 requestContext.setMessageDecoder(decoder);
118 if (log.isDebugEnabled()) {
119 log.debug("Decoded request");
121 } catch (BindingException e) {
122 log.error("Error decoding attribute query message", e);
123 requestContext.setFailureStatus(buildStatus(StatusCode.RESPONDER, null, "Error decoding message"));
124 throw new ProfileException("Error decoding attribute query message");
125 } catch (SecurityPolicyException e) {
126 log.error("Message did not meet security policy requirements", e);
127 requestContext.setFailureStatus(buildStatus(StatusCode.RESPONDER, StatusCode.REQUEST_DENIED,
128 "Message did not meet security policy requirements"));
129 throw new ProfileException("Message did not meet security policy requirements", e);
131 // Set as much information as can be retrieved from the decoded message
132 SAMLSecurityPolicy securityPolicy = requestContext.getMessageDecoder().getSecurityPolicy();
133 requestContext.setRelyingPartyId(securityPolicy.getIssuer());
135 Request request = (Request) requestContext.getMessageDecoder().getSAMLMessage();
136 requestContext.setSamlRequest(request);
137 requestContext.setAttributeQuery(request.getAttributeQuery());
139 populateRelyingPartyData(requestContext);
141 populateAssertingPartyData(requestContext);
146 * Populates the relying party entity and role metadata and relying party configuration data.
148 * @param requestContext current request context with relying party ID populated
150 * @throws ProfileException thrown if metadata can not be located for the relying party
152 protected void populateRelyingPartyData(AttributeQueryContext requestContext) throws ProfileException {
154 requestContext.setRelyingPartyMetadata(getMetadataProvider().getEntityDescriptor(
155 requestContext.getRelyingPartyId()));
157 RoleDescriptor relyingPartyRole = requestContext.getRelyingPartyMetadata().getSPSSODescriptor(
158 ShibbolethConstants.SAML11P_NS);
160 if (relyingPartyRole == null) {
161 relyingPartyRole = requestContext.getRelyingPartyMetadata()
162 .getSPSSODescriptor(ShibbolethConstants.SAML10P_NS);
163 if (relyingPartyRole == null) {
164 throw new MetadataProviderException("Unable to locate SPSSO role descriptor for entity "
165 + requestContext.getRelyingPartyId());
168 requestContext.setRelyingPartyRoleMetadata(relyingPartyRole);
170 RelyingPartyConfiguration rpConfig = getRelyingPartyConfiguration(requestContext.getRelyingPartyId());
171 requestContext.setRelyingPartyConfiguration(rpConfig);
173 requestContext.setProfileConfiguration((AttributeQueryConfiguration) rpConfig
174 .getProfileConfiguration(AttributeQueryConfiguration.PROFILE_ID));
176 } catch (MetadataProviderException e) {
177 log.error("Unable to locate metadata for relying party " + requestContext.getRelyingPartyId());
178 requestContext.setFailureStatus(buildStatus(StatusCode.RESPONDER, null,
179 "Unable to locate metadata for relying party " + requestContext.getRelyingPartyId()));
180 throw new ProfileException("Unable to locate metadata for relying party "
181 + requestContext.getRelyingPartyId());
186 * Populates the asserting party entity and role metadata.
188 * @param requestContext current request context with relying party configuration populated
190 * @throws ProfileException thrown if metadata can not be located for the asserting party
192 protected void populateAssertingPartyData(AttributeQueryContext requestContext) throws ProfileException {
193 String assertingPartyId = requestContext.getRelyingPartyConfiguration().getProviderId();
196 requestContext.setAssertingPartyId(assertingPartyId);
198 requestContext.setAssertingPartyMetadata(getMetadataProvider().getEntityDescriptor(assertingPartyId));
200 RoleDescriptor assertingPartyRole = requestContext.getAssertingPartyMetadata()
201 .getAttributeAuthorityDescriptor(ShibbolethConstants.SAML11P_NS);
203 if (assertingPartyRole == null) {
204 assertingPartyRole = requestContext.getAssertingPartyMetadata().getAttributeAuthorityDescriptor(
205 ShibbolethConstants.SAML10P_NS);
206 if (assertingPartyRole == null) {
207 throw new MetadataProviderException("Unable to locate IDPSSO role descriptor for entity "
211 requestContext.setAssertingPartyRoleMetadata(assertingPartyRole);
212 } catch (MetadataProviderException e) {
213 log.error("Unable to locate metadata for asserting party " + assertingPartyId);
214 requestContext.setFailureStatus(buildStatus(StatusCode.RESPONDER, null,
215 "Unable to locate metadata for relying party " + assertingPartyId));
216 throw new ProfileException("Unable to locate metadata for relying party " + assertingPartyId);
221 * Encodes the request's SAML response and writes it to the servlet response.
223 * @param requestContext current request context
225 * @throws ProfileException thrown if no message encoder is registered for this profiles binding
227 protected void encodeResponse(AttributeQueryContext requestContext) throws ProfileException {
228 if (log.isDebugEnabled()) {
229 log.debug("Encoding response to SAML request from relying party " + requestContext.getRelyingPartyId());
231 MessageEncoder<ServletResponse> encoder = getMessageEncoderFactory().getMessageEncoder(
232 HTTPSOAP11Encoder.BINDING_URI);
233 if (encoder == null) {
234 throw new ProfileException("No response encoder was registered for binding type: "
235 + HTTPSOAP11Encoder.BINDING_URI);
238 super.populateMessageEncoder(encoder);
239 ProfileResponse<ServletResponse> profileResponse = requestContext.getProfileResponse();
240 encoder.setResponse(profileResponse.getRawResponse());
241 encoder.setSamlMessage(requestContext.getSamlResponse());
242 requestContext.setMessageEncoder(encoder);
246 } catch (BindingException e) {
247 throw new ProfileException("Unable to encode response to relying party: "
248 + requestContext.getRelyingPartyId(), e);
252 /** Basic data structure used to accumulate information as a request is being processed. */
253 protected class AttributeQueryContext extends
254 SAML1ProfileRequestContext<Request, Response, AttributeQueryConfiguration> {
256 private AttributeQuery attributeQuery;
261 * @param request current profile request
262 * @param response current profile response
264 public AttributeQueryContext(ProfileRequest<ServletRequest> request, ProfileResponse<ServletResponse> response) {
265 super(request, response);
268 public AttributeQuery getAttributeQuery(){
269 return attributeQuery;
272 public void setAttributeQuery(AttributeQuery query){
273 attributeQuery = query;