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 org.apache.log4j.Logger;
22 import org.opensaml.common.SAMLObjectBuilder;
23 import org.opensaml.common.binding.BasicEndpointSelector;
24 import org.opensaml.common.binding.decoding.SAMLMessageDecoder;
25 import org.opensaml.common.xml.SAMLConstants;
26 import org.opensaml.saml1.core.AttributeQuery;
27 import org.opensaml.saml1.core.Request;
28 import org.opensaml.saml1.core.Response;
29 import org.opensaml.saml1.core.Statement;
30 import org.opensaml.saml1.core.StatusCode;
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.saml2.metadata.provider.MetadataProviderException;
38 import org.opensaml.ws.message.decoder.MessageDecodingException;
39 import org.opensaml.ws.security.SecurityPolicyException;
40 import org.opensaml.ws.transport.http.HTTPInTransport;
41 import org.opensaml.ws.transport.http.HTTPOutTransport;
43 import edu.internet2.middleware.shibboleth.common.profile.ProfileException;
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);
55 /** Builder of assertion consumer service endpoints. */
56 private SAMLObjectBuilder<AssertionConsumerService> acsEndpointBuilder;
59 public AttributeQueryProfileHandler() {
62 acsEndpointBuilder = (SAMLObjectBuilder<AssertionConsumerService>) getBuilderFactory().getBuilder(
63 AssertionConsumerService.DEFAULT_ELEMENT_NAME);
67 public String getProfileId() {
68 return "urn:mace:shibboleth:2.0:idp:profiles:saml1:query:attribute";
72 public void processRequest(HTTPInTransport inTransport, HTTPOutTransport outTransport) throws ProfileException {
73 AttributeQueryContext requestContext = decodeRequest(inTransport, outTransport);
75 Response samlResponse;
77 if (requestContext.getProfileConfiguration() == null) {
78 log.error("SAML 1 Attribute Query profile is not configured for relying party "
79 + requestContext.getInboundMessageIssuer());
80 requestContext.setFailureStatus(buildStatus(StatusCode.RESPONDER, StatusCode.REQUEST_DENIED,
81 "SAML 1 Attribute Query profile is not configured for relying party "
82 + requestContext.getInboundMessageIssuer()));
83 samlResponse = buildErrorResponse(requestContext);
85 resolvePrincipal(requestContext);
86 resolveAttributes(requestContext);
87 requestContext.setReleasedAttributes(requestContext.getPrincipalAttributes().keySet());
89 ArrayList<Statement> statements = new ArrayList<Statement>();
90 statements.add(buildAttributeStatement(requestContext,
91 "urn:oasis:names:tc:SAML:1.0:cm:sender-vouches"));
93 samlResponse = buildResponse(requestContext, statements);
95 } catch (ProfileException e) {
96 samlResponse = buildErrorResponse(requestContext);
99 requestContext.setOutboundSAMLMessage(samlResponse);
100 requestContext.setOutboundSAMLMessageId(samlResponse.getID());
101 requestContext.setOutboundSAMLMessageIssueInstant(samlResponse.getIssueInstant());
102 encodeResponse(requestContext);
103 writeAuditLogEntry(requestContext);
107 * Decodes an incoming request and populates a created request context with the resultant information.
109 * @param inTransport inbound message transport
110 * @param outTransport outbound message transport
112 * @return the created request context
114 * @throws ProfileException throw if there is a problem decoding the request
116 protected AttributeQueryContext decodeRequest(HTTPInTransport inTransport, HTTPOutTransport outTransport)
117 throws ProfileException {
118 if (log.isDebugEnabled()) {
119 log.debug("Decoding incomming request");
122 MetadataProvider metadataProvider = getMetadataProvider();
124 AttributeQueryContext requestContext = new AttributeQueryContext();
125 requestContext.setMetadataProvider(metadataProvider);
127 requestContext.setInboundMessageTransport(inTransport);
128 requestContext.setInboundSAMLProtocol(SAMLConstants.SAML11P_NS);
129 requestContext.setPeerEntityRole(SPSSODescriptor.DEFAULT_ELEMENT_NAME);
131 requestContext.setOutboundMessageTransport(outTransport);
132 requestContext.setOutboundSAMLProtocol(SAMLConstants.SAML11P_NS);
135 SAMLMessageDecoder decoder = getMessageDecoders().get(getInboundBinding());
136 if (decoder == null) {
137 throw new ProfileException("No message decoder configured for inbound binding " + getInboundBinding());
139 requestContext.setMessageDecoder(decoder);
140 decoder.decode(requestContext);
141 if (log.isDebugEnabled()) {
142 log.debug("Decoded request");
144 return requestContext;
145 } catch (MessageDecodingException e) {
146 log.error("Error decoding attribute query message", e);
147 requestContext.setFailureStatus(buildStatus(StatusCode.RESPONDER, null, "Error decoding message"));
148 throw new ProfileException("Error decoding attribute query message");
149 } catch (SecurityPolicyException e) {
150 log.error("Message did not meet security policy requirements", e);
151 requestContext.setFailureStatus(buildStatus(StatusCode.RESPONDER, StatusCode.REQUEST_DENIED,
152 "Message did not meet security policy requirements"));
153 throw new ProfileException("Message did not meet security policy requirements", e);
155 // Set as much information as can be retrieved from the decoded message
157 Request request = requestContext.getInboundSAMLMessage();
158 if (request == null) {
159 throw new ProfileException("No inbound SAML message found.");
161 AttributeQuery query = request.getAttributeQuery();
162 requestContext.setSubjectNameIdentifier(query.getSubject().getNameIdentifier());
164 String relyingPartyId = requestContext.getInboundMessageIssuer();
165 RelyingPartyConfiguration rpConfig = getRelyingPartyConfiguration(relyingPartyId);
166 requestContext.setRelyingPartyConfiguration(rpConfig);
167 requestContext.setPeerEntityEndpoint(selectEndpoint(requestContext));
169 String assertingPartyId = requestContext.getRelyingPartyConfiguration().getProviderId();
170 requestContext.setLocalEntityId(assertingPartyId);
171 EntityDescriptor assertingPartyMetadata = metadataProvider.getEntityDescriptor(assertingPartyId);
172 if (assertingPartyMetadata == null) {
173 throw new MetadataProviderException("Unable to locate metadata for asserting party "
176 requestContext.setLocalEntityMetadata(assertingPartyMetadata);
177 requestContext.setLocalEntityRole(AttributeAuthorityDescriptor.DEFAULT_ELEMENT_NAME);
178 requestContext.setLocalEntityRoleMetadata(assertingPartyMetadata
179 .getAttributeAuthorityDescriptor(SAMLConstants.SAML11P_NS));
181 AttributeQueryConfiguration profileConfig = (AttributeQueryConfiguration) rpConfig
182 .getProfileConfiguration(AttributeQueryConfiguration.PROFILE_ID);
183 if (profileConfig != null) {
184 requestContext.setProfileConfiguration(profileConfig);
185 requestContext.setOutboundMessageArtifactType(profileConfig.getOutboundArtifactType());
186 if (profileConfig.getSigningCredential() != null) {
187 requestContext.setOutboundSAMLMessageSigningCredential(profileConfig.getSigningCredential());
188 } else if (rpConfig.getDefaultSigningCredential() != null) {
189 requestContext.setOutboundSAMLMessageSigningCredential(rpConfig.getDefaultSigningCredential());
193 } catch (MetadataProviderException e) {
194 log.error(e.getMessage());
196 .setFailureStatus(buildStatus(StatusCode.RESPONDER, null, "Error locating party metadata"));
197 throw new ProfileException("Error locating party metadata");
203 * Selects the appropriate endpoint for the relying party and stores it in the request context.
205 * @param requestContext current request context
207 * @return Endpoint selected from the information provided in the request context
209 protected Endpoint selectEndpoint(AttributeQueryContext requestContext) {
212 if (getInboundBinding().equals(SAMLConstants.SAML1_SOAP11_BINDING_URI)) {
213 endpoint = acsEndpointBuilder.buildObject();
214 endpoint.setBinding(SAMLConstants.SAML1_SOAP11_BINDING_URI);
216 BasicEndpointSelector endpointSelector = new BasicEndpointSelector();
217 endpointSelector.setEndpointType(AssertionConsumerService.DEFAULT_ELEMENT_NAME);
218 endpointSelector.setMetadataProvider(getMetadataProvider());
219 endpointSelector.setEntityMetadata(requestContext.getPeerEntityMetadata());
220 endpointSelector.setEntityRoleMetadata(requestContext.getPeerEntityRoleMetadata());
221 endpointSelector.setSamlRequest(requestContext.getInboundSAMLMessage());
222 endpointSelector.getSupportedIssuerBindings().addAll(getSupportedOutboundBindings());
223 endpoint = endpointSelector.selectEndpoint();
229 /** Basic data structure used to accumulate information as a request is being processed. */
230 protected class AttributeQueryContext extends
231 BaseSAML1ProfileRequestContext<Request, Response, AttributeQueryConfiguration> {}