2 * Copyright 2001-2007 Internet2
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.
18 * QueryAttributeResolver.cpp
20 * AttributeResolver based on SAML queries.
24 #include "Application.h"
25 #include "ServiceProvider.h"
26 #include "SessionCache.h"
27 #include "attribute/Attribute.h"
28 #include "attribute/resolver/AttributeExtractor.h"
29 #include "attribute/resolver/AttributeResolver.h"
30 #include "attribute/resolver/ResolutionContext.h"
31 #include "binding/SOAPClient.h"
32 #include "util/SPConstants.h"
34 #include <log4cpp/Category.hh>
35 #include <saml/exceptions.h>
36 #include <saml/binding/SecurityPolicy.h>
37 #include <saml/saml1/binding/SAML1SOAPClient.h>
38 #include <saml/saml1/core/Assertions.h>
39 #include <saml/saml1/core/Protocols.h>
40 #include <saml/saml1/profile/AssertionValidator.h>
41 #include <saml/saml2/binding/SAML2SOAPClient.h>
42 #include <saml/saml2/core/Protocols.h>
43 #include <saml/saml2/metadata/Metadata.h>
44 #include <saml/saml2/metadata/MetadataProvider.h>
45 #include <saml/saml2/profile/AssertionValidator.h>
46 #include <xmltooling/util/NDC.h>
47 #include <xmltooling/util/XMLHelper.h>
48 #include <xercesc/util/XMLUniDefs.hpp>
50 using namespace shibsp;
51 using namespace opensaml::saml1;
52 using namespace opensaml::saml1p;
53 using namespace opensaml::saml2;
54 using namespace opensaml::saml2p;
55 using namespace opensaml::saml2md;
56 using namespace opensaml;
57 using namespace xmltooling;
58 using namespace log4cpp;
63 class SHIBSP_DLLLOCAL QueryContext : public ResolutionContext
66 QueryContext(const Application& application, const Session& session)
67 : m_query(true), m_app(application), m_session(&session), m_metadata(NULL), m_entity(NULL), m_nameid(session.getNameID()) {
71 const Application& application,
72 const EntityDescriptor* issuer,
74 const vector<const opensaml::Assertion*>* tokens=NULL,
75 const multimap<string,Attribute*>* attributes=NULL
76 ) : m_query(true), m_app(application), m_session(NULL), m_metadata(NULL), m_entity(issuer), m_nameid(nameid) {
79 for (vector<const opensaml::Assertion*>::const_iterator t = tokens->begin(); t!=tokens->end(); ++t) {
80 const saml2::Assertion* token2 = dynamic_cast<const saml2::Assertion*>(*t);
81 if (token2 && !token2->getAttributeStatements().empty()) {
85 const saml1::Assertion* token1 = dynamic_cast<const saml1::Assertion*>(*t);
86 if (token1 && !token1->getAttributeStatements().empty()) {
97 for_each(m_attributes.begin(), m_attributes.end(), cleanup_pair<string,shibsp::Attribute>());
98 for_each(m_assertions.begin(), m_assertions.end(), xmltooling::cleanup<opensaml::Assertion>());
101 bool doQuery() const {
105 const Application& getApplication() const {
108 const EntityDescriptor* getEntityDescriptor() const {
111 if (m_session && m_session->getEntityID()) {
112 m_metadata = m_app.getMetadataProvider();
115 return m_entity = m_metadata->getEntityDescriptor(m_session->getEntityID());
120 const NameID* getNameID() const {
123 const Session* getSession() const {
126 multimap<string,shibsp::Attribute*>& getResolvedAttributes() {
129 vector<opensaml::Assertion*>& getResolvedAssertions() {
135 const Application& m_app;
136 const Session* m_session;
137 mutable MetadataProvider* m_metadata;
138 mutable const EntityDescriptor* m_entity;
139 const NameID* m_nameid;
140 multimap<string,shibsp::Attribute*> m_attributes;
141 vector<opensaml::Assertion*> m_assertions;
144 class SHIBSP_DLLLOCAL QueryResolver : public AttributeResolver
147 QueryResolver(const DOMElement* e);
149 for_each(m_SAML1Designators.begin(), m_SAML1Designators.end(), xmltooling::cleanup<AttributeDesignator>());
150 for_each(m_SAML2Designators.begin(), m_SAML2Designators.end(), xmltooling::cleanup<saml2::Attribute>());
153 ResolutionContext* createResolutionContext(
154 const Application& application,
155 const EntityDescriptor* issuer,
156 const NameID* nameid,
157 const vector<const opensaml::Assertion*>* tokens=NULL,
158 const multimap<string,shibsp::Attribute*>* attributes=NULL
160 return new QueryContext(application,issuer,nameid,tokens,attributes);
163 ResolutionContext* createResolutionContext(const Application& application, const Session& session) const {
164 return new QueryContext(application,session);
167 Lockable* lock() {return this;}
170 void resolveAttributes(ResolutionContext& ctx) const;
173 bool SAML1Query(QueryContext& ctx) const;
174 bool SAML2Query(QueryContext& ctx) const;
177 vector<AttributeDesignator*> m_SAML1Designators;
178 vector<saml2::Attribute*> m_SAML2Designators;
181 AttributeResolver* SHIBSP_DLLLOCAL QueryResolverFactory(const DOMElement* const & e)
183 return new QueryResolver(e);
188 void SHIBSP_API shibsp::registerAttributeResolvers()
190 SPConfig::getConfig().AttributeResolverManager.registerFactory(QUERY_ATTRIBUTE_RESOLVER, QueryResolverFactory);
193 QueryResolver::QueryResolver(const DOMElement* e) : m_log(Category::getInstance(SHIBSP_LOGCAT".AttributeResolver"))
196 xmltooling::NDC ndc("QueryResolver");
199 DOMElement* child = XMLHelper::getFirstChildElement(e);
202 if (XMLHelper::isNodeNamed(e, samlconstants::SAML20_NS, saml2::Attribute::LOCAL_NAME)) {
203 auto_ptr<XMLObject> obj(saml2::AttributeBuilder::buildOneFromElement(child));
204 saml2::Attribute* down = dynamic_cast<saml2::Attribute*>(obj.get());
206 m_SAML2Designators.push_back(down);
210 else if (XMLHelper::isNodeNamed(e, samlconstants::SAML1P_NS, AttributeDesignator::LOCAL_NAME)) {
211 auto_ptr<XMLObject> obj(AttributeDesignatorBuilder::buildOneFromElement(child));
212 AttributeDesignator* down = dynamic_cast<AttributeDesignator*>(obj.get());
214 m_SAML1Designators.push_back(down);
219 catch (exception& ex) {
220 m_log.error("exception loading attribute designator: %s", ex.what());
222 child = XMLHelper::getNextSiblingElement(child);
226 bool QueryResolver::SAML1Query(QueryContext& ctx) const
229 xmltooling::NDC ndc("query");
233 const AttributeAuthorityDescriptor* AA = ctx.getEntityDescriptor()->getAttributeAuthorityDescriptor(samlconstants::SAML11_PROTOCOL_ENUM);
235 AA = ctx.getEntityDescriptor()->getAttributeAuthorityDescriptor(samlconstants::SAML10_PROTOCOL_ENUM);
239 m_log.info("no SAML 1.x AttributeAuthority role found in metadata");
243 shibsp::SecurityPolicy policy(ctx.getApplication());
244 MetadataCredentialCriteria mcc(*AA);
245 shibsp::SOAPClient soaper(policy);
246 const PropertySet* policySettings =
247 ctx.getApplication().getServiceProvider().getPolicySettings(ctx.getApplication().getString("policyId").second);
248 pair<bool,bool> signedAssertions = policySettings->getBool("signedAssertions");
250 auto_ptr_XMLCh binding(samlconstants::SAML1_BINDING_SOAP);
251 saml1p::Response* response=NULL;
252 const vector<AttributeService*>& endpoints=AA->getAttributeServices();
253 for (vector<AttributeService*>::const_iterator ep=endpoints.begin(); !response && ep!=endpoints.end(); ++ep) {
255 if (!XMLString::equals((*ep)->getBinding(),binding.get()))
257 auto_ptr_char loc((*ep)->getLocation());
258 auto_ptr_XMLCh issuer(ctx.getApplication().getString("entityID").second);
259 NameIdentifier* nameid = NameIdentifierBuilder::buildNameIdentifier();
260 nameid->setName(ctx.getNameID()->getName());
261 nameid->setFormat(ctx.getNameID()->getFormat());
262 nameid->setNameQualifier(ctx.getNameID()->getNameQualifier());
263 saml1::Subject* subject = saml1::SubjectBuilder::buildSubject();
264 subject->setNameIdentifier(nameid);
265 saml1p::AttributeQuery* query = saml1p::AttributeQueryBuilder::buildAttributeQuery();
266 query->setSubject(subject);
267 query->setResource(issuer.get());
268 for (vector<AttributeDesignator*>::const_iterator ad = m_SAML1Designators.begin(); ad!=m_SAML1Designators.end(); ++ad)
269 query->getAttributeDesignators().push_back((*ad)->cloneAttributeDesignator());
270 Request* request = RequestBuilder::buildRequest();
271 request->setAttributeQuery(query);
272 request->setMinorVersion(version);
274 SAML1SOAPClient client(soaper);
275 client.sendSAML(request, mcc, loc.get());
276 response = client.receiveSAML();
278 catch (exception& ex) {
279 m_log.error("exception making SAML query: %s", ex.what());
285 m_log.error("unable to successfully query for attributes");
289 const vector<saml1::Assertion*>& assertions = const_cast<const saml1p::Response*>(response)->getAssertions();
290 if (assertions.size()>1)
291 m_log.warn("simple resolver only supports one assertion in the query response");
293 auto_ptr<saml1p::Response> wrapper(response);
294 saml1::Assertion* newtoken = assertions.front();
296 if (!newtoken->getSignature() && signedAssertions.first && signedAssertions.second) {
297 m_log.error("assertion unsigned, rejecting it based on signedAssertions policy");
302 policy.evaluate(*newtoken);
303 if (!policy.isSecure())
304 throw SecurityPolicyException("Security of SAML 1.x query result not established.");
305 saml1::AssertionValidator tokval(ctx.getApplication().getAudiences(), time(NULL));
306 tokval.validateAssertion(*newtoken);
308 catch (exception& ex) {
309 m_log.error("assertion failed policy/validation: %s", ex.what());
315 ctx.getResolvedAssertions().push_back(newtoken);
317 // Finally, extract and filter the result.
319 AttributeExtractor* extractor = ctx.getApplication().getAttributeExtractor();
321 Locker extlocker(extractor);
322 extractor->extractAttributes(ctx.getApplication(), AA, *newtoken, ctx.getResolvedAttributes());
325 catch (exception& ex) {
326 m_log.error("caught exception extracting/filtering attributes from query result: %s", ex.what());
332 bool QueryResolver::SAML2Query(QueryContext& ctx) const
335 xmltooling::NDC ndc("query");
338 const AttributeAuthorityDescriptor* AA = ctx.getEntityDescriptor()->getAttributeAuthorityDescriptor(samlconstants::SAML20P_NS);
340 m_log.info("no SAML 2 AttributeAuthority role found in metadata");
344 shibsp::SecurityPolicy policy(ctx.getApplication());
345 MetadataCredentialCriteria mcc(*AA);
346 shibsp::SOAPClient soaper(policy);
347 const PropertySet* policySettings =
348 ctx.getApplication().getServiceProvider().getPolicySettings(ctx.getApplication().getString("policyId").second);
349 pair<bool,bool> signedAssertions = policySettings->getBool("signedAssertions");
351 auto_ptr_XMLCh binding(samlconstants::SAML20_BINDING_SOAP);
352 saml2p::StatusResponseType* srt=NULL;
353 const vector<AttributeService*>& endpoints=AA->getAttributeServices();
354 for (vector<AttributeService*>::const_iterator ep=endpoints.begin(); !srt && ep!=endpoints.end(); ++ep) {
356 if (!XMLString::equals((*ep)->getBinding(),binding.get()))
358 auto_ptr_char loc((*ep)->getLocation());
359 auto_ptr_XMLCh issuer(ctx.getApplication().getString("entityID").second);
360 saml2::Subject* subject = saml2::SubjectBuilder::buildSubject();
361 subject->setNameID(ctx.getNameID()->cloneNameID());
362 saml2p::AttributeQuery* query = saml2p::AttributeQueryBuilder::buildAttributeQuery();
363 query->setSubject(subject);
364 Issuer* iss = IssuerBuilder::buildIssuer();
365 iss->setName(issuer.get());
366 query->setIssuer(iss);
367 for (vector<saml2::Attribute*>::const_iterator ad = m_SAML2Designators.begin(); ad!=m_SAML2Designators.end(); ++ad)
368 query->getAttributes().push_back((*ad)->cloneAttribute());
370 SAML2SOAPClient client(soaper);
371 client.sendSAML(query, mcc, loc.get());
372 srt = client.receiveSAML();
374 catch (exception& ex) {
375 m_log.error("exception making SAML query: %s", ex.what());
381 m_log.error("unable to successfully query for attributes");
384 saml2p::Response* response = dynamic_cast<saml2p::Response*>(srt);
387 m_log.error("message was not a samlp:Response");
391 const vector<saml2::Assertion*>& assertions = const_cast<const saml2p::Response*>(response)->getAssertions();
392 if (assertions.size()>1)
393 m_log.warn("simple resolver only supports one assertion in the query response");
395 auto_ptr<saml2p::Response> wrapper(response);
396 saml2::Assertion* newtoken = assertions.front();
398 if (!newtoken->getSignature() && signedAssertions.first && signedAssertions.second) {
399 m_log.error("assertion unsigned, rejecting it based on signedAssertions policy");
404 policy.evaluate(*newtoken);
405 if (!policy.isSecure())
406 throw SecurityPolicyException("Security of SAML 2.0 query result not established.");
407 saml2::AssertionValidator tokval(ctx.getApplication().getAudiences(), time(NULL));
408 tokval.validateAssertion(*newtoken);
410 catch (exception& ex) {
411 m_log.error("assertion failed policy/validation: %s", ex.what());
417 ctx.getResolvedAssertions().push_back(newtoken);
419 // Finally, extract and filter the result.
421 AttributeExtractor* extractor = ctx.getApplication().getAttributeExtractor();
423 Locker extlocker(extractor);
424 extractor->extractAttributes(ctx.getApplication(), AA, *newtoken, ctx.getResolvedAttributes());
427 catch (exception& ex) {
428 m_log.error("caught exception extracting/filtering attributes from query result: %s", ex.what());
434 void QueryResolver::resolveAttributes(ResolutionContext& ctx) const
437 xmltooling::NDC ndc("resolveAttributes");
440 QueryContext& qctx = dynamic_cast<QueryContext&>(ctx);
441 if (!qctx.doQuery()) {
442 m_log.debug("found AttributeStatement in input to new session, skipping query");
446 if (qctx.getNameID() && qctx.getEntityDescriptor()) {
447 m_log.debug("attempting SAML 2.0 attribute query");
448 if (!SAML2Query(qctx)) {
449 m_log.debug("attempting SAML 1.x attribute query");
453 m_log.warn("can't attempt attribute query, either no NameID or no metadata to use");