2 * Licensed to the University Corporation for Advanced Internet
3 * Development, Inc. (UCAID) under one or more contributor license
4 * agreements. See the NOTICE file distributed with this work for
5 * additional information regarding copyright ownership.
7 * UCAID licenses this file to you under the Apache License,
8 * Version 2.0 (the "License"); you may not use this file except
9 * in compliance with the License. You may obtain a copy of the
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17 * either express or implied. See the License for the specific
18 * language governing permissions and limitations under the License.
22 * XMLAttributeExtractor.cpp
24 * AttributeExtractor based on an XML mapping file.
28 #include "exceptions.h"
29 #include "Application.h"
30 #include "ServiceProvider.h"
31 #include "attribute/Attribute.h"
32 #include "attribute/AttributeDecoder.h"
33 #include "attribute/filtering/AttributeFilter.h"
34 #include "attribute/filtering/BasicFilteringContext.h"
35 #include "attribute/resolver/AttributeExtractor.h"
36 #include "remoting/ddf.h"
37 #include "security/SecurityPolicy.h"
38 #include "util/SPConstants.h"
40 #include <boost/bind.hpp>
41 #include <boost/shared_ptr.hpp>
42 #include <boost/algorithm/string.hpp>
43 #include <boost/iterator/indirect_iterator.hpp>
44 #include <boost/ptr_container/ptr_vector.hpp>
45 #include <boost/tuple/tuple.hpp>
46 #include <saml/SAMLConfig.h>
47 #include <saml/saml1/core/Assertions.h>
48 #include <saml/saml2/core/Assertions.h>
49 #include <saml/saml2/metadata/Metadata.h>
50 #include <saml/saml2/metadata/MetadataCredentialCriteria.h>
51 #include <saml/saml2/metadata/ObservableMetadataProvider.h>
52 #include <xmltooling/XMLToolingConfig.h>
53 #include <xmltooling/security/TrustEngine.h>
54 #include <xmltooling/util/NDC.h>
55 #include <xmltooling/util/ReloadableXMLFile.h>
56 #include <xmltooling/util/Threads.h>
57 #include <xmltooling/util/XMLHelper.h>
58 #include <xercesc/util/XMLUniDefs.hpp>
60 using namespace shibsp;
61 using namespace opensaml::saml2md;
62 using namespace opensaml;
63 using namespace xmltooling;
64 using namespace boost;
66 using saml1::NameIdentifier;
68 using saml2::EncryptedAttribute;
72 #if defined (_MSC_VER)
73 #pragma warning( push )
74 #pragma warning( disable : 4250 )
77 class XMLExtractorImpl : public ObservableMetadataProvider::Observer
80 XMLExtractorImpl(const DOMElement* e, Category& log);
82 for (map<const ObservableMetadataProvider*,decoded_t>::iterator i=m_decodedMap.begin(); i!=m_decodedMap.end(); ++i) {
83 i->first->removeObserver(this);
84 for (decoded_t::iterator attrs = i->second.begin(); attrs!=i->second.end(); ++attrs)
85 for_each(attrs->second.begin(), attrs->second.end(), mem_fun_ref<DDF&,DDF>(&DDF::destroy));
88 m_document->release();
91 void setDocument(DOMDocument* doc) {
95 void onEvent(const ObservableMetadataProvider& metadata) const {
96 // Destroy attributes we cached from this provider.
98 SharedLock wrapper(m_attrLock, false);
99 decoded_t& d = m_decodedMap[&metadata];
100 for (decoded_t::iterator a = d.begin(); a!=d.end(); ++a)
101 for_each(a->second.begin(), a->second.end(), mem_fun_ref<DDF&,DDF>(&DDF::destroy));
105 void extractAttributes(const Application&, const char*, const char*, const NameIdentifier&, ptr_vector<Attribute>&) const;
106 void extractAttributes(const Application&, const char*, const char*, const NameID&, ptr_vector<Attribute>&) const;
107 void extractAttributes(const Application&, const GenericRequest*, const char*, const char*, const saml1::Attribute&, ptr_vector<Attribute>&) const;
108 void extractAttributes(const Application&, const GenericRequest*, const char*, const char*, const saml2::Attribute&, ptr_vector<Attribute>&) const;
109 void extractAttributes(const Application&, const GenericRequest*, const char*, const char*, const saml1::AttributeStatement&, ptr_vector<Attribute>&) const;
110 void extractAttributes(const Application&, const GenericRequest*, const char*, const char*, const saml2::AttributeStatement&, ptr_vector<Attribute>&) const;
111 void extractAttributes(
112 const Application&, const GenericRequest*, const ObservableMetadataProvider*, const XMLCh*, const char*, const Extensions&, ptr_vector<Attribute>&
115 void getAttributeIds(vector<string>& attributes) const {
116 attributes.insert(attributes.end(), m_attributeIds.begin(), m_attributeIds.end());
119 void generateMetadata(SPSSODescriptor& role) const;
123 DOMDocument* m_document;
124 typedef map< pair<xstring,xstring>,pair< boost::shared_ptr<AttributeDecoder>,vector<string> > > attrmap_t;
126 vector<string> m_attributeIds;
127 vector< tuple<xstring,xstring,bool> > m_requestedAttrs;
129 // settings for embedded assertions in metadata
131 scoped_ptr<AttributeFilter> m_filter;
132 scoped_ptr<MetadataProvider> m_metadata;
133 scoped_ptr<TrustEngine> m_trust;
134 bool m_entityAssertions,m_metaAttrCaching;
136 // manages caching of decoded Attributes
137 scoped_ptr<RWLock> m_attrLock;
138 typedef map< const EntityAttributes*,vector<DDF> > decoded_t;
139 mutable map<const ObservableMetadataProvider*,decoded_t> m_decodedMap;
142 class XMLExtractor : public AttributeExtractor, public ReloadableXMLFile
145 XMLExtractor(const DOMElement* e) : ReloadableXMLFile(e, Category::getInstance(SHIBSP_LOGCAT".AttributeExtractor.XML")) {
153 void extractAttributes(
154 const Application& application, const RoleDescriptor* issuer, const XMLObject& xmlObject, vector<Attribute*>& attributes
156 extractAttributes(application, nullptr, issuer, xmlObject, attributes);
159 void extractAttributes(const Application&, const GenericRequest*, const RoleDescriptor*, const XMLObject&, vector<Attribute*>&) const;
161 void getAttributeIds(std::vector<std::string>& attributes) const {
163 m_impl->getAttributeIds(attributes);
166 void generateMetadata(SPSSODescriptor& role) const {
168 m_impl->generateMetadata(role);
172 pair<bool,DOMElement*> background_load();
175 scoped_ptr<XMLExtractorImpl> m_impl;
177 void extractAttributes(const Application&, const GenericRequest*, const RoleDescriptor*, const XMLObject&, ptr_vector<Attribute>&) const;
180 #if defined (_MSC_VER)
181 #pragma warning( pop )
184 AttributeExtractor* SHIBSP_DLLLOCAL XMLAttributeExtractorFactory(const DOMElement* const & e)
186 return new XMLExtractor(e);
189 static const XMLCh _aliases[] = UNICODE_LITERAL_7(a,l,i,a,s,e,s);
190 static const XMLCh _AttributeDecoder[] = UNICODE_LITERAL_16(A,t,t,r,i,b,u,t,e,D,e,c,o,d,e,r);
191 static const XMLCh _AttributeFilter[] = UNICODE_LITERAL_15(A,t,t,r,i,b,u,t,e,F,i,l,t,e,r);
192 static const XMLCh Attributes[] = UNICODE_LITERAL_10(A,t,t,r,i,b,u,t,e,s);
193 static const XMLCh _id[] = UNICODE_LITERAL_2(i,d);
194 static const XMLCh isRequested[] = UNICODE_LITERAL_11(i,s,R,e,q,u,e,s,t,e,d);
195 static const XMLCh _MetadataProvider[] = UNICODE_LITERAL_16(M,e,t,a,d,a,t,a,P,r,o,v,i,d,e,r);
196 static const XMLCh metadataAttributeCaching[] = UNICODE_LITERAL_24(m,e,t,a,d,a,t,a,A,t,t,r,i,b,u,t,e,C,a,c,h,i,n,g);
197 static const XMLCh metadataPolicyId[] = UNICODE_LITERAL_16(m,e,t,a,d,a,t,a,P,o,l,i,c,y,I,d);
198 static const XMLCh _name[] = UNICODE_LITERAL_4(n,a,m,e);
199 static const XMLCh nameFormat[] = UNICODE_LITERAL_10(n,a,m,e,F,o,r,m,a,t);
200 static const XMLCh _TrustEngine[] = UNICODE_LITERAL_11(T,r,u,s,t,E,n,g,i,n,e);
201 static const XMLCh _type[] = UNICODE_LITERAL_4(t,y,p,e);
204 XMLExtractorImpl::XMLExtractorImpl(const DOMElement* e, Category& log)
207 m_policyId(XMLHelper::getAttrString(e, nullptr, metadataPolicyId)),
208 m_entityAssertions(true),
209 m_metaAttrCaching(XMLHelper::getAttrBool(e, true, metadataAttributeCaching))
212 xmltooling::NDC ndc("XMLExtractorImpl");
215 if (!XMLHelper::isNodeNamed(e, shibspconstants::SHIB2ATTRIBUTEMAP_NS, Attributes))
216 throw ConfigurationException("XML AttributeExtractor requires am:Attributes at root of configuration.");
218 DOMElement* child = XMLHelper::getFirstChildElement(e, shibspconstants::SHIB2ATTRIBUTEMAP_NS, _MetadataProvider);
221 string t(XMLHelper::getAttrString(child, nullptr, _type));
223 throw ConfigurationException("MetadataProvider element missing type attribute.");
224 m_log.info("building MetadataProvider of type %s...", t.c_str());
225 m_metadata.reset(SAMLConfig::getConfig().MetadataProviderManager.newPlugin(t.c_str(), child));
228 catch (std::exception& ex) {
230 m_entityAssertions = false;
231 m_log.crit("error building/initializing dedicated MetadataProvider: %s", ex.what());
232 m_log.crit("disabling support for Assertions in EntityAttributes extension");
236 if (m_entityAssertions) {
237 child = XMLHelper::getFirstChildElement(e, shibspconstants::SHIB2ATTRIBUTEMAP_NS, _TrustEngine);
240 string t(XMLHelper::getAttrString(child, nullptr, _type));
242 throw ConfigurationException("TrustEngine element missing type attribute.");
243 m_log.info("building TrustEngine of type %s...", t.c_str());
244 m_trust.reset(XMLToolingConfig::getConfig().TrustEngineManager.newPlugin(t.c_str(), child));
246 catch (std::exception& ex) {
247 m_entityAssertions = false;
248 m_log.crit("error building/initializing dedicated TrustEngine: %s", ex.what());
249 m_log.crit("disabling support for Assertions in EntityAttributes extension");
254 if (m_entityAssertions) {
255 child = XMLHelper::getFirstChildElement(e, shibspconstants::SHIB2ATTRIBUTEMAP_NS, _AttributeFilter);
258 string t(XMLHelper::getAttrString(child, nullptr, _type));
260 throw ConfigurationException("AttributeFilter element missing type attribute.");
261 m_log.info("building AttributeFilter of type %s...", t.c_str());
262 m_filter.reset(SPConfig::getConfig().AttributeFilterManager.newPlugin(t.c_str(), child));
264 catch (std::exception& ex) {
265 m_entityAssertions = false;
266 m_log.crit("error building/initializing dedicated AttributeFilter: %s", ex.what());
267 m_log.crit("disabling support for Assertions in EntityAttributes extension");
272 child = XMLHelper::getFirstChildElement(e, shibspconstants::SHIB2ATTRIBUTEMAP_NS, saml1::Attribute::LOCAL_NAME);
274 // Check for missing name or id.
275 const XMLCh* name = child->getAttributeNS(nullptr, _name);
276 if (!name || !*name) {
277 m_log.warn("skipping Attribute with no name");
278 child = XMLHelper::getNextSiblingElement(child, shibspconstants::SHIB2ATTRIBUTEMAP_NS, saml1::Attribute::LOCAL_NAME);
282 auto_ptr_char id(child->getAttributeNS(nullptr, _id));
283 if (!id.get() || !*id.get()) {
284 m_log.warn("skipping Attribute with no id");
285 child = XMLHelper::getNextSiblingElement(child, shibspconstants::SHIB2ATTRIBUTEMAP_NS, saml1::Attribute::LOCAL_NAME);
288 else if (!strcmp(id.get(), "REMOTE_USER")) {
289 m_log.warn("skipping Attribute, id of REMOTE_USER is a reserved name");
290 child = XMLHelper::getNextSiblingElement(child, shibspconstants::SHIB2ATTRIBUTEMAP_NS, saml1::Attribute::LOCAL_NAME);
294 boost::shared_ptr<AttributeDecoder> decoder;
296 DOMElement* dchild = XMLHelper::getFirstChildElement(child, shibspconstants::SHIB2ATTRIBUTEMAP_NS, _AttributeDecoder);
298 auto_ptr<xmltooling::QName> q(XMLHelper::getXSIType(dchild));
300 decoder.reset(SPConfig::getConfig().AttributeDecoderManager.newPlugin(*q.get(), dchild));
303 decoder.reset(SPConfig::getConfig().AttributeDecoderManager.newPlugin(StringAttributeDecoderType, nullptr));
305 catch (std::exception& ex) {
306 m_log.error("skipping Attribute (%s), error building AttributeDecoder: %s", id.get(), ex.what());
310 child = XMLHelper::getNextSiblingElement(child, shibspconstants::SHIB2ATTRIBUTEMAP_NS, saml1::Attribute::LOCAL_NAME);
314 // Empty NameFormat implies the usual Shib URI naming defaults.
315 const XMLCh* format = child->getAttributeNS(nullptr, nameFormat);
316 if (!format || XMLString::equals(format, shibspconstants::SHIB1_ATTRIBUTE_NAMESPACE_URI) ||
317 XMLString::equals(format, saml2::Attribute::URI_REFERENCE))
318 format = &chNull; // ignore default Format/Namespace values
320 // Fetch/create the map entry and see if it's a duplicate rule.
321 pair< boost::shared_ptr<AttributeDecoder>,vector<string> >& decl = m_attrMap[pair<xstring,xstring>(name,format)];
323 m_log.warn("skipping duplicate Attribute mapping (same name and nameFormat)");
324 child = XMLHelper::getNextSiblingElement(child, shibspconstants::SHIB2ATTRIBUTEMAP_NS, saml1::Attribute::LOCAL_NAME);
328 if (m_log.isInfoEnabled()) {
329 auto_ptr_char n(name);
330 auto_ptr_char f(format);
331 m_log.info("creating mapping for Attribute %s%s%s", n.get(), *f.get() ? ", Format/Namespace:" : "", f.get());
334 decl.first = decoder;
335 decl.second.push_back(id.get());
336 m_attributeIds.push_back(id.get());
338 // Check for isRequired/isRequested.
339 bool requested = XMLHelper::getAttrBool(child, false, isRequested);
340 bool required = XMLHelper::getAttrBool(child, false, RequestedAttribute::ISREQUIRED_ATTRIB_NAME);
341 if (required || requested)
342 m_requestedAttrs.push_back(tuple<xstring,xstring,bool>(name,format,required));
344 name = child->getAttributeNS(nullptr, _aliases);
346 auto_ptr_char aliases(name);
347 string dup(aliases.get());
348 set<string> new_aliases;
349 split(new_aliases, dup, is_space(), algorithm::token_compress_on);
350 set<string>::iterator ru = new_aliases.find("REMOTE_USER");
351 if (ru != new_aliases.end()) {
352 m_log.warn("skipping alias, REMOTE_USER is a reserved name");
353 new_aliases.erase(ru);
355 m_attributeIds.insert(m_attributeIds.end(), new_aliases.begin(), new_aliases.end());
358 child = XMLHelper::getNextSiblingElement(child, shibspconstants::SHIB2ATTRIBUTEMAP_NS, saml1::Attribute::LOCAL_NAME);
361 if (m_metaAttrCaching)
362 m_attrLock.reset(RWLock::create());
365 void XMLExtractorImpl::generateMetadata(SPSSODescriptor& role) const
367 if (m_requestedAttrs.empty())
370 const vector<AttributeConsumingService*>& svcs = const_cast<const SPSSODescriptor*>(&role)->getAttributeConsumingServices();
371 for (vector<AttributeConsumingService*>::const_iterator s =svcs.begin(); s != svcs.end(); ++s) {
372 pair<bool,int> i = (*s)->getIndex();
373 if (i.first && index == i.second)
374 index = i.second + 1;
376 AttributeConsumingService* svc = AttributeConsumingServiceBuilder::buildAttributeConsumingService();
377 role.getAttributeConsumingServices().push_back(svc);
378 svc->setIndex(index);
379 ServiceName* sn = ServiceNameBuilder::buildServiceName();
380 svc->getServiceNames().push_back(sn);
381 sn->setName(dynamic_cast<EntityDescriptor*>(role.getParent())->getEntityID());
382 static const XMLCh english[] = UNICODE_LITERAL_2(e,n);
383 sn->setLang(english);
385 for (vector< tuple<xstring,xstring,bool> >::const_iterator i = m_requestedAttrs.begin(); i != m_requestedAttrs.end(); ++i) {
386 RequestedAttribute* req = RequestedAttributeBuilder::buildRequestedAttribute();
387 svc->getRequestedAttributes().push_back(req);
388 req->setName(i->get<0>().c_str());
389 if (i->get<1>().empty())
390 req->setNameFormat(saml2::Attribute::URI_REFERENCE);
392 req->setNameFormat(i->get<1>().c_str());
394 req->isRequired(true);
398 void XMLExtractorImpl::extractAttributes(
399 const Application& application,
400 const char* assertingParty,
401 const char* relyingParty,
402 const NameIdentifier& nameid,
403 ptr_vector<Attribute>& attributes
406 const XMLCh* format = nameid.getFormat();
407 if (!format || !*format)
408 format = NameIdentifier::UNSPECIFIED;
409 attrmap_t::const_iterator rule;
410 if ((rule = m_attrMap.find(pair<xstring,xstring>(format,xstring()))) != m_attrMap.end()) {
411 auto_ptr<Attribute> a(rule->second.first->decode(nullptr, rule->second.second, &nameid, assertingParty, relyingParty));
413 attributes.push_back(a.get());
417 else if (m_log.isDebugEnabled()) {
418 auto_ptr_char temp(format);
419 m_log.debug("skipping unmapped NameIdentifier with format (%s)", temp.get());
423 void XMLExtractorImpl::extractAttributes(
424 const Application& application,
425 const char* assertingParty,
426 const char* relyingParty,
427 const NameID& nameid,
428 ptr_vector<Attribute>& attributes
431 const XMLCh* format = nameid.getFormat();
432 if (!format || !*format)
433 format = NameID::UNSPECIFIED;
434 attrmap_t::const_iterator rule;
435 if ((rule = m_attrMap.find(pair<xstring,xstring>(format,xstring()))) != m_attrMap.end()) {
436 auto_ptr<Attribute> a(rule->second.first->decode(nullptr, rule->second.second, &nameid, assertingParty, relyingParty));
438 attributes.push_back(a.get());
442 else if (m_log.isDebugEnabled()) {
443 auto_ptr_char temp(format);
444 m_log.debug("skipping unmapped NameID with format (%s)", temp.get());
448 void XMLExtractorImpl::extractAttributes(
449 const Application& application,
450 const GenericRequest* request,
451 const char* assertingParty,
452 const char* relyingParty,
453 const saml1::Attribute& attr,
454 ptr_vector<Attribute>& attributes
457 const XMLCh* name = attr.getAttributeName();
458 const XMLCh* format = attr.getAttributeNamespace();
461 if (!format || XMLString::equals(format, shibspconstants::SHIB1_ATTRIBUTE_NAMESPACE_URI))
463 attrmap_t::const_iterator rule;
464 if ((rule = m_attrMap.find(pair<xstring,xstring>(name,format))) != m_attrMap.end()) {
465 auto_ptr<Attribute> a(rule->second.first->decode(request, rule->second.second, &attr, assertingParty, relyingParty));
467 attributes.push_back(a.get());
471 else if (m_log.isInfoEnabled()) {
472 auto_ptr_char temp1(name);
473 auto_ptr_char temp2(format);
474 m_log.info("skipping unmapped SAML 1.x Attribute with Name: %s%s%s", temp1.get(), *temp2.get() ? ", Namespace:" : "", temp2.get());
478 void XMLExtractorImpl::extractAttributes(
479 const Application& application,
480 const GenericRequest* request,
481 const char* assertingParty,
482 const char* relyingParty,
483 const saml2::Attribute& attr,
484 ptr_vector<Attribute>& attributes
487 const XMLCh* name = attr.getName();
488 const XMLCh* format = attr.getNameFormat();
491 if (!format || !*format)
492 format = saml2::Attribute::UNSPECIFIED;
493 else if (XMLString::equals(format, saml2::Attribute::URI_REFERENCE))
495 attrmap_t::const_iterator rule;
496 if ((rule = m_attrMap.find(pair<xstring,xstring>(name,format))) != m_attrMap.end()) {
497 auto_ptr<Attribute> a(rule->second.first->decode(request, rule->second.second, &attr, assertingParty, relyingParty));
499 attributes.push_back(a.get());
504 else if (XMLString::equals(format, saml2::Attribute::UNSPECIFIED)) {
505 // As a fallback, if the format is "unspecified", null out the value and re-map.
506 if ((rule = m_attrMap.find(pair<xstring,xstring>(name,xstring()))) != m_attrMap.end()) {
507 auto_ptr<Attribute> a(rule->second.first->decode(request, rule->second.second, &attr, assertingParty, relyingParty));
509 attributes.push_back(a.get());
516 if (m_log.isInfoEnabled()) {
517 auto_ptr_char temp1(name);
518 auto_ptr_char temp2(format);
519 m_log.info("skipping unmapped SAML 2.0 Attribute with Name: %s%s%s", temp1.get(), *temp2.get() ? ", Format:" : "", temp2.get());
523 void XMLExtractorImpl::extractAttributes(
524 const Application& application,
525 const GenericRequest* request,
526 const char* assertingParty,
527 const char* relyingParty,
528 const saml1::AttributeStatement& statement,
529 ptr_vector<Attribute>& attributes
532 static void (XMLExtractorImpl::* extract)(
533 const Application&, const GenericRequest*, const char*, const char*, const saml1::Attribute&, ptr_vector<Attribute>&
534 ) const = &XMLExtractorImpl::extractAttributes;
536 make_indirect_iterator(statement.getAttributes().begin()), make_indirect_iterator(statement.getAttributes().end()),
537 boost::bind(extract, this, boost::cref(application), request, assertingParty, relyingParty, _1, boost::ref(attributes))
541 void XMLExtractorImpl::extractAttributes(
542 const Application& application,
543 const GenericRequest* request,
544 const char* assertingParty,
545 const char* relyingParty,
546 const saml2::AttributeStatement& statement,
547 ptr_vector<Attribute>& attributes
550 static void (XMLExtractorImpl::* extract)(
551 const Application&, const GenericRequest*, const char*, const char*, const saml2::Attribute&, ptr_vector<Attribute>&
552 ) const = &XMLExtractorImpl::extractAttributes;
554 make_indirect_iterator(statement.getAttributes().begin()), make_indirect_iterator(statement.getAttributes().end()),
555 boost::bind(extract, this, boost::cref(application), request, assertingParty, relyingParty, _1, boost::ref(attributes))
559 void XMLExtractorImpl::extractAttributes(
560 const Application& application,
561 const GenericRequest* request,
562 const ObservableMetadataProvider* observable,
563 const XMLCh* entityID,
564 const char* relyingParty,
565 const Extensions& ext,
566 ptr_vector<Attribute>& attributes
569 const vector<XMLObject*>& exts = ext.getUnknownXMLObjects();
570 for (vector<XMLObject*>::const_iterator i = exts.begin(); i != exts.end(); ++i) {
571 const EntityAttributes* container = dynamic_cast<const EntityAttributes*>(*i);
575 bool useCache = false;
576 map<const ObservableMetadataProvider*,decoded_t>::iterator cacheEntry;
578 // Check for cached result.
579 if (observable && m_metaAttrCaching) {
580 m_attrLock->rdlock();
581 cacheEntry = m_decodedMap.find(observable);
582 if (cacheEntry == m_decodedMap.end()) {
583 // We need to elevate the lock and retry.
584 m_attrLock->unlock();
585 m_attrLock->wrlock();
586 cacheEntry = m_decodedMap.find(observable);
587 if (cacheEntry == m_decodedMap.end()) {
588 SharedLock locker(m_attrLock, false); // guard in case these throw
590 // It's still brand new, so hook it for cache activation.
591 observable->addObserver(this);
593 // Prime the map reference with an empty decoded map.
594 cacheEntry = m_decodedMap.insert(make_pair(observable,decoded_t())).first;
596 // Downgrade the lock.
597 // We don't have to recheck because we never erase the master map entry entirely, even on changes.
598 locker.release(); // unguard for lock downgrade
599 m_attrLock->unlock();
600 m_attrLock->rdlock();
607 // We're holding the lock, so check the cache.
608 decoded_t::iterator d = cacheEntry->second.find(container);
609 if (d != cacheEntry->second.end()) {
610 SharedLock locker(m_attrLock, false); // pop the lock when we're done
611 for (vector<DDF>::iterator obj = d->second.begin(); obj != d->second.end(); ++obj) {
612 auto_ptr<Attribute> wrapper(Attribute::unmarshall(*obj));
613 m_log.debug("recovered cached metadata attribute (%s)", wrapper->getId());
614 attributes.push_back(wrapper.get());
621 // Add a guard for the lock if we're caching.
622 SharedLock locker(useCache ? m_attrLock.get() : nullptr, false);
624 // Use a holding area to support caching.
625 ptr_vector<Attribute> holding;
627 // Extract attributes into holding area with no asserting party set.
628 static void (XMLExtractorImpl::* extractV2Attr)(
629 const Application&, const GenericRequest*, const char*, const char*, const saml2::Attribute&, ptr_vector<Attribute>&
630 ) const = &XMLExtractorImpl::extractAttributes;
632 make_indirect_iterator(container->getAttributes().begin()), make_indirect_iterator(container->getAttributes().end()),
633 boost::bind(extractV2Attr, this, boost::ref(application), request, (const char*)nullptr, relyingParty, _1, boost::ref(holding))
636 if (entityID && m_entityAssertions) {
637 const vector<saml2::Assertion*>& asserts = container->getAssertions();
638 for (indirect_iterator<vector<saml2::Assertion*>::const_iterator> assert = make_indirect_iterator(asserts.begin());
639 assert != make_indirect_iterator(asserts.end()); ++assert) {
640 if (!(assert->getSignature())) {
641 if (m_log.isDebugEnabled()) {
642 auto_ptr_char eid(entityID);
643 m_log.debug("skipping unsigned assertion in metadata extension for entity (%s)", eid.get());
647 else if (assert->getAttributeStatements().empty()) {
648 if (m_log.isDebugEnabled()) {
649 auto_ptr_char eid(entityID);
650 m_log.debug("skipping assertion with no AttributeStatement in metadata extension for entity (%s)", eid.get());
656 const NameID* subject = assert->getSubject() ? assert->getSubject()->getNameID() : nullptr;
658 !XMLString::equals(subject->getFormat(), NameID::ENTITY) ||
659 !XMLString::equals(subject->getName(), entityID)) {
660 if (m_log.isDebugEnabled()) {
661 auto_ptr_char eid(entityID);
662 m_log.debug("skipping assertion with improper Subject in metadata extension for entity (%s)", eid.get());
669 // Set up and evaluate a policy for an AA asserting attributes to us.
670 shibsp::SecurityPolicy policy(application, &AttributeAuthorityDescriptor::ELEMENT_QNAME, false, m_policyId.c_str());
671 Locker locker(m_metadata.get());
673 policy.setMetadataProvider(m_metadata.get());
675 policy.setTrustEngine(m_trust.get());
676 // Populate recipient as audience.
677 const XMLCh* issuer = assert->getIssuer() ? assert->getIssuer()->getName() : nullptr;
678 policy.getAudiences().push_back(application.getRelyingParty(issuer)->getXMLString("entityID").second);
680 // Extract assertion information for policy.
681 policy.setMessageID(assert->getID());
682 policy.setIssueInstant(assert->getIssueInstantEpoch());
683 policy.setIssuer(assert->getIssuer());
685 // Look up metadata for issuer.
686 if (policy.getIssuer() && policy.getMetadataProvider()) {
687 if (policy.getIssuer()->getFormat() && !XMLString::equals(policy.getIssuer()->getFormat(), saml2::NameIDType::ENTITY)) {
688 m_log.debug("non-system entity issuer, skipping metadata lookup");
691 m_log.debug("searching metadata for entity assertion issuer...");
692 pair<const EntityDescriptor*,const RoleDescriptor*> lookup;
693 MetadataProvider::Criteria& mc = policy.getMetadataProviderCriteria();
694 mc.entityID_unicode = policy.getIssuer()->getName();
695 mc.role = &AttributeAuthorityDescriptor::ELEMENT_QNAME;
696 mc.protocol = samlconstants::SAML20P_NS;
697 lookup = policy.getMetadataProvider()->getEntityDescriptor(mc);
699 auto_ptr_char iname(policy.getIssuer()->getName());
700 m_log.debug("no metadata found, can't establish identity of issuer (%s)", iname.get());
702 else if (!lookup.second) {
703 m_log.debug("unable to find compatible AA role in metadata");
706 policy.setIssuerMetadata(lookup.second);
711 // Authenticate the assertion. We have to clone and marshall it to establish the signature for verification.
712 scoped_ptr<saml2::Assertion> tokencopy(assert->cloneAssertion());
713 tokencopy->marshall();
714 policy.evaluate(*tokencopy);
715 if (!policy.isAuthenticated()) {
716 if (m_log.isDebugEnabled()) {
717 auto_ptr_char tempid(tokencopy->getID());
718 auto_ptr_char eid(entityID);
720 "failed to authenticate assertion (%s) in metadata extension for entity (%s)", tempid.get(), eid.get()
726 // Override the asserting/relying party names based on this new issuer.
727 const EntityDescriptor* inlineEntity =
728 policy.getIssuerMetadata() ? dynamic_cast<const EntityDescriptor*>(policy.getIssuerMetadata()->getParent()) : nullptr;
729 auto_ptr_char inlineAssertingParty(inlineEntity ? inlineEntity->getEntityID() : nullptr);
730 relyingParty = application.getRelyingParty(inlineEntity)->getString("entityID").second;
732 // Use a private holding area for filtering purposes.
733 ptr_vector<Attribute> holding2;
734 const vector<saml2::Attribute*>& attrs2 =
735 const_cast<const saml2::AttributeStatement*>(tokencopy->getAttributeStatements().front())->getAttributes();
737 make_indirect_iterator(attrs2.begin()), make_indirect_iterator(attrs2.end()),
738 boost::bind(extractV2Attr, this, boost::ref(application), request, inlineAssertingParty.get(), relyingParty, _1, boost::ref(holding2))
741 // Now we locally filter the attributes so that the actual issuer can be properly set.
742 // If we relied on outside filtering, the attributes couldn't be distinguished from the
743 // ones that come from the user's IdP.
744 if (m_filter && !holding2.empty()) {
746 // The filter API uses an unsafe container, so we have to transfer everything into one and back.
747 vector<Attribute*> unsafe_holding2;
749 // Use a local exception context since the container is unsafe.
751 while (!holding2.empty()) {
752 ptr_vector<Attribute>::auto_type ptr = holding2.pop_back();
753 unsafe_holding2.push_back(ptr.get());
756 BasicFilteringContext fc(application, unsafe_holding2, policy.getIssuerMetadata());
757 Locker filtlocker(m_filter.get());
758 m_filter->filterAttributes(fc, unsafe_holding2);
760 // Transfer back to safe container
761 while (!unsafe_holding2.empty()) {
762 auto_ptr<Attribute> ptr(unsafe_holding2.back());
763 unsafe_holding2.pop_back();
764 holding2.push_back(ptr.get());
768 catch (std::exception& ex) {
769 m_log.error("caught exception filtering attributes: %s", ex.what());
770 m_log.error("dumping extracted attributes due to filtering exception");
771 for_each(unsafe_holding2.begin(), unsafe_holding2.end(), xmltooling::cleanup<Attribute>());
772 holding2.clear(); // in case the exception was during transfer between containers
776 if (!holding2.empty()) {
777 // Copy them over to the main holding tank, which transfers ownership.
778 holding.transfer(holding.end(), holding2);
781 catch (std::exception& ex) {
782 // Known exceptions are handled gracefully by skipping the assertion.
783 if (m_log.isDebugEnabled()) {
784 auto_ptr_char tempid(assert->getID());
785 auto_ptr_char eid(entityID);
787 "exception authenticating assertion (%s) in metadata extension for entity (%s): %s",
798 if (!holding.empty()) {
800 locker.release(); // unguard to upgrade lock
801 m_attrLock->unlock();
802 m_attrLock->wrlock();
803 SharedLock locker2(m_attrLock, false); // pop the lock when we're done
804 if (cacheEntry->second.count(container) == 0) {
805 static void (vector<DDF>::* push_back)(DDF const &) = &vector<DDF>::push_back;
806 vector<DDF>& marshalled = cacheEntry->second[container];
808 holding.begin(), holding.end(),
809 boost::bind(push_back, boost::ref(marshalled), boost::bind(&Attribute::marshall, _1))
814 // Copy them to the output parameter, which transfers ownership.
815 attributes.transfer(attributes.end(), holding);
818 // If the lock is held, it's guarded.
820 break; // only process a single extension element
824 void XMLExtractor::extractAttributes(
825 const Application& application, const GenericRequest* request, const RoleDescriptor* issuer, const XMLObject& xmlObject, vector<Attribute*>& attributes
831 ptr_vector<Attribute> holding;
832 extractAttributes(application, request, issuer, xmlObject, holding);
834 // Transfer ownership from the ptr_vector to the unsafe vector for API compatibility.
835 // Any throws should leave each container in a consistent state. The holding container
836 // is freed by us, and the result container by the caller.
837 while (!holding.empty()) {
838 ptr_vector<Attribute>::auto_type ptr = holding.pop_back();
839 attributes.push_back(ptr.get());
844 void XMLExtractor::extractAttributes(
845 const Application& application, const GenericRequest* request, const RoleDescriptor* issuer, const XMLObject& xmlObject, ptr_vector<Attribute>& attributes
848 static void (XMLExtractor::* extractEncrypted)(
849 const Application&, const GenericRequest*, const RoleDescriptor*, const XMLObject&, ptr_vector<Attribute>&
850 ) const = &XMLExtractor::extractAttributes;
851 static void (XMLExtractorImpl::* extractV1Statement)(
852 const Application&, const GenericRequest*, const char*, const char*, const saml1::AttributeStatement&, ptr_vector<Attribute>&
853 ) const = &XMLExtractorImpl::extractAttributes;
855 const EntityDescriptor* entity = issuer ? dynamic_cast<const EntityDescriptor*>(issuer->getParent()) : nullptr;
856 const char* relyingParty = application.getRelyingParty(entity)->getString("entityID").second;
858 // Check for statements.
859 if (XMLString::equals(xmlObject.getElementQName().getLocalPart(), saml1::AttributeStatement::LOCAL_NAME)) {
860 const saml2::AttributeStatement* statement2 = dynamic_cast<const saml2::AttributeStatement*>(&xmlObject);
862 auto_ptr_char assertingParty(entity ? entity->getEntityID() : nullptr);
863 m_impl->extractAttributes(application, request, assertingParty.get(), relyingParty, *statement2, attributes);
864 // Handle EncryptedAttributes inline so we have access to the role descriptor.
865 const vector<saml2::EncryptedAttribute*>& encattrs = statement2->getEncryptedAttributes();
867 make_indirect_iterator(encattrs.begin()), make_indirect_iterator(encattrs.end()),
868 boost::bind(extractEncrypted, this, boost::ref(application), request, issuer, _1, boost::ref(attributes))
873 const saml1::AttributeStatement* statement1 = dynamic_cast<const saml1::AttributeStatement*>(&xmlObject);
875 auto_ptr_char assertingParty(entity ? entity->getEntityID() : nullptr);
876 m_impl->extractAttributes(application, request, assertingParty.get(), relyingParty, *statement1, attributes);
880 throw AttributeExtractionException("Unable to extract attributes, unknown object type.");
883 // Check for assertions.
884 if (XMLString::equals(xmlObject.getElementQName().getLocalPart(), saml1::Assertion::LOCAL_NAME)) {
885 const saml2::Assertion* token2 = dynamic_cast<const saml2::Assertion*>(&xmlObject);
887 auto_ptr_char assertingParty(entity ? entity->getEntityID() : nullptr);
888 const vector<saml2::AttributeStatement*>& statements = token2->getAttributeStatements();
889 for (indirect_iterator<vector<saml2::AttributeStatement*>::const_iterator> s = make_indirect_iterator(statements.begin());
890 s != make_indirect_iterator(statements.end()); ++s) {
891 m_impl->extractAttributes(application, request, assertingParty.get(), relyingParty, *s, attributes);
892 // Handle EncryptedAttributes inline so we have access to the role descriptor.
893 const vector<saml2::EncryptedAttribute*>& encattrs = const_cast<const saml2::AttributeStatement&>(*s).getEncryptedAttributes();
895 make_indirect_iterator(encattrs.begin()), make_indirect_iterator(encattrs.end()),
896 boost::bind(extractEncrypted, this, boost::ref(application), request, issuer, _1, boost::ref(attributes))
902 const saml1::Assertion* token1 = dynamic_cast<const saml1::Assertion*>(&xmlObject);
904 auto_ptr_char assertingParty(entity ? entity->getEntityID() : nullptr);
905 const vector<saml1::AttributeStatement*>& statements = token1->getAttributeStatements();
906 for_each(make_indirect_iterator(statements.begin()), make_indirect_iterator(statements.end()),
907 boost::bind(extractV1Statement, m_impl.get(), boost::ref(application), request, assertingParty.get(), relyingParty, _1, boost::ref(attributes))
912 throw AttributeExtractionException("Unable to extract attributes, unknown object type.");
915 // Check for metadata.
916 if (XMLString::equals(xmlObject.getElementQName().getNamespaceURI(), samlconstants::SAML20MD_NS)) {
917 const RoleDescriptor* roleToExtract = dynamic_cast<const RoleDescriptor*>(&xmlObject);
918 const EntityDescriptor* entityToExtract = roleToExtract ? dynamic_cast<const EntityDescriptor*>(roleToExtract->getParent()) : nullptr;
919 if (!entityToExtract)
920 throw AttributeExtractionException("Unable to extract attributes, unknown metadata object type.");
921 const Extensions* ext = entityToExtract->getExtensions();
923 m_impl->extractAttributes(
926 dynamic_cast<const ObservableMetadataProvider*>(application.getMetadataProvider(false)),
927 entityToExtract->getEntityID(),
933 const EntitiesDescriptor* group = dynamic_cast<const EntitiesDescriptor*>(entityToExtract->getParent());
935 ext = group->getExtensions();
937 m_impl->extractAttributes(
940 dynamic_cast<const ObservableMetadataProvider*>(application.getMetadataProvider(false)),
941 nullptr, // not an entity, so inline assertions won't be processed
947 group = dynamic_cast<const EntitiesDescriptor*>(group->getParent());
952 // Check for attributes.
953 if (XMLString::equals(xmlObject.getElementQName().getLocalPart(), saml1::Attribute::LOCAL_NAME)) {
954 auto_ptr_char assertingParty(entity ? entity->getEntityID() : nullptr);
955 const saml2::Attribute* attr2 = dynamic_cast<const saml2::Attribute*>(&xmlObject);
957 return m_impl->extractAttributes(application, request, assertingParty.get(), relyingParty, *attr2, attributes);
959 const saml1::Attribute* attr1 = dynamic_cast<const saml1::Attribute*>(&xmlObject);
961 return m_impl->extractAttributes(application, request, assertingParty.get(), relyingParty, *attr1, attributes);
963 throw AttributeExtractionException("Unable to extract attributes, unknown object type.");
966 if (XMLString::equals(xmlObject.getElementQName().getLocalPart(), EncryptedAttribute::LOCAL_NAME)) {
967 const EncryptedAttribute* encattr = dynamic_cast<const EncryptedAttribute*>(&xmlObject);
969 const XMLCh* recipient = application.getXMLString("entityID").second;
970 CredentialResolver* cr = application.getCredentialResolver();
972 m_log.warn("found encrypted attribute, but no CredentialResolver was available");
977 Locker credlocker(cr);
979 MetadataCredentialCriteria mcc(*issuer);
980 scoped_ptr<XMLObject> decrypted(encattr->decrypt(*cr, recipient, &mcc));
981 if (m_log.isDebugEnabled())
982 m_log.debugStream() << "decrypted Attribute: " << *decrypted << logging::eol;
983 return extractAttributes(application, request, issuer, *decrypted, attributes);
986 scoped_ptr<XMLObject> decrypted(encattr->decrypt(*cr, recipient));
987 if (m_log.isDebugEnabled())
988 m_log.debugStream() << "decrypted Attribute: " << *decrypted << logging::eol;
989 return extractAttributes(application, request, issuer, *decrypted, attributes);
992 catch (std::exception& ex) {
993 m_log.error("caught exception decrypting Attribute: %s", ex.what());
999 // Check for NameIDs.
1000 const NameID* name2 = dynamic_cast<const NameID*>(&xmlObject);
1002 auto_ptr_char assertingParty(entity ? entity->getEntityID() : nullptr);
1003 return m_impl->extractAttributes(application, assertingParty.get(), relyingParty, *name2, attributes);
1006 const NameIdentifier* name1 = dynamic_cast<const NameIdentifier*>(&xmlObject);
1008 auto_ptr_char assertingParty(entity ? entity->getEntityID() : nullptr);
1009 return m_impl->extractAttributes(application, assertingParty.get(), relyingParty, *name1, attributes);
1012 m_log.debug("unable to extract attributes, unknown XML object type: %s", xmlObject.getElementQName().toString().c_str());
1015 pair<bool,DOMElement*> XMLExtractor::background_load()
1017 // Load from source using base class.
1018 pair<bool,DOMElement*> raw = ReloadableXMLFile::load();
1020 // If we own it, wrap it.
1021 XercesJanitor<DOMDocument> docjanitor(raw.first ? raw.second->getOwnerDocument() : nullptr);
1023 scoped_ptr<XMLExtractorImpl> impl(new XMLExtractorImpl(raw.second, m_log));
1025 // If we held the document, transfer it to the impl. If we didn't, it's a no-op.
1026 impl->setDocument(docjanitor.release());
1028 // Perform the swap inside a lock.
1031 SharedLock locker(m_lock, false);
1034 return make_pair(false,(DOMElement*)nullptr);