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.saml2;
19 import java.util.ArrayList;
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;
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;
41 /** SAML 2.0 Attribute Query profile handler. */
42 public class AttributeQueryProfileHandler extends AbstractSAML2ProfileHandler {
45 private static Logger log = Logger.getLogger(AttributeQueryProfileHandler.class);
48 public String getProfileId() {
49 return "urn:mace:shibboleth:2.0:idp:profiles:saml2:query:attribute";
53 public void processRequest(HTTPInTransport inTransport, HTTPOutTransport outTransport) throws ProfileException {
54 Response samlResponse;
56 AttributeQueryContext requestContext = decodeRequest(inTransport, outTransport);
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);
68 checkSamlVersion(requestContext);
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());
75 // Lookup principal name and attributes, create attribute statement from information
76 ArrayList<Statement> statements = new ArrayList<Statement>();
77 statements.add(buildAttributeStatement(requestContext));
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);
85 requestContext.setOutboundSAMLMessage(samlResponse);
86 requestContext.setOutboundSAMLMessageId(samlResponse.getID());
87 requestContext.setOutboundSAMLMessageIssueInstant(samlResponse.getIssueInstant());
89 encodeResponse(requestContext);
90 writeAuditLogEntry(requestContext);
94 * Decodes an incoming request and populates a created request context with the resultant information.
96 * @param inTransport inbound message transport
97 * @param outTransport outbound message transport
99 * @return the created request context
101 * @throws ProfileException throw if there is a problem decoding the request
103 protected AttributeQueryContext decodeRequest(HTTPInTransport inTransport, HTTPOutTransport outTransport)
104 throws ProfileException {
105 if (log.isDebugEnabled()) {
106 log.debug("Decoding incomming request");
109 MetadataProvider metadataProvider = getMetadataProvider();
111 AttributeQueryContext requestContext = new AttributeQueryContext();
112 requestContext.setInboundMessageTransport(inTransport);
113 requestContext.setInboundSAMLProtocol(SAMLConstants.SAML20P_NS);
114 requestContext.setOutboundMessageTransport(outTransport);
115 requestContext.setOutboundSAMLProtocol(SAMLConstants.SAML20P_NS);
116 requestContext.setMetadataProvider(metadataProvider);
119 SAMLMessageDecoder decoder = getMessageDecoders().get(getInboundBinding());
120 requestContext.setMessageDecoder(decoder);
121 decoder.decode(requestContext);
122 if (log.isDebugEnabled()) {
123 log.debug("Decoded request");
125 return requestContext;
126 } catch (MessageDecodingException e) {
127 log.error("Error decoding attribute query message", e);
128 requestContext.setFailureStatus(buildStatus(StatusCode.RESPONDER_URI, null, "Error decoding message"));
129 throw new ProfileException("Error decoding attribute query message");
130 } catch (SecurityPolicyException e) {
131 log.error("Message did not meet security policy requirements", e);
132 requestContext.setFailureStatus(buildStatus(StatusCode.RESPONDER_URI, StatusCode.REQUEST_DENIED_URI,
133 "Message did not meet security policy requirements"));
134 throw new ProfileException("Message did not meet security policy requirements", e);
136 // Set as much information as can be retrieved from the decoded message
138 AttributeQuery attributeQuery = requestContext.getInboundSAMLMessage();
139 requestContext.setInboundSAMLMessageId(attributeQuery.getID());
140 requestContext.setInboundSAMLMessageIssueInstant(attributeQuery.getIssueInstant());
142 String relyingPartyId = requestContext.getPeerEntityId();
143 requestContext.setPeerEntityMetadata(metadataProvider.getEntityDescriptor(relyingPartyId));
144 requestContext.setPeerEntityRole(SPSSODescriptor.DEFAULT_ELEMENT_NAME);
145 requestContext.setPeerEntityRoleMetadata(requestContext.getPeerEntityMetadata().getSPSSODescriptor(
146 SAMLConstants.SAML20P_NS));
147 RelyingPartyConfiguration rpConfig = getRelyingPartyConfiguration(relyingPartyId);
148 requestContext.setRelyingPartyConfiguration(rpConfig);
150 String assertingPartyId = requestContext.getRelyingPartyConfiguration().getProviderId();
151 requestContext.setLocalEntityId(assertingPartyId);
152 requestContext.setLocalEntityMetadata(metadataProvider.getEntityDescriptor(assertingPartyId));
153 requestContext.setLocalEntityRole(AttributeAuthorityDescriptor.DEFAULT_ELEMENT_NAME);
154 requestContext.setLocalEntityRoleMetadata(requestContext.getLocalEntityMetadata()
155 .getAttributeAuthorityDescriptor(SAMLConstants.SAML20P_NS));
157 AttributeQueryConfiguration profileConfig = (AttributeQueryConfiguration) rpConfig
158 .getProfileConfiguration(AttributeQueryConfiguration.PROFILE_ID);
159 requestContext.setProfileConfiguration(profileConfig);
160 requestContext.setOutboundMessageArtifactType(profileConfig.getOutboundArtifactType());
161 if (profileConfig.getSigningCredential() != null) {
162 requestContext.setOutboundSAMLMessageSigningCredential(profileConfig.getSigningCredential());
163 } else if (rpConfig.getDefaultSigningCredential() != null) {
164 requestContext.setOutboundSAMLMessageSigningCredential(rpConfig.getDefaultSigningCredential());
167 } catch (MetadataProviderException e) {
168 log.error("Unable to locate metadata for asserting or relying party");
169 requestContext.setFailureStatus(buildStatus(StatusCode.RESPONDER_URI, null,
170 "Error locating party metadata"));
171 throw new ProfileException("Error locating party metadata");
176 /** Basic data structure used to accumulate information as a request is being processed. */
177 protected class AttributeQueryContext extends
178 BaseSAML2ProfileRequestContext<AttributeQuery, Response, AttributeQueryConfiguration> {