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 * XMLAttributeExtractor.cpp
20 * AttributeExtractor based on an XML mapping file.
24 #include "Application.h"
25 #include "ServiceProvider.h"
26 #include "attribute/AttributeDecoder.h"
27 #include "attribute/resolver/AttributeExtractor.h"
28 #include "util/SPConstants.h"
30 #include <saml/saml1/core/Assertions.h>
31 #include <saml/saml2/core/Assertions.h>
32 #include <saml/saml2/metadata/MetadataCredentialCriteria.h>
33 #include <xmltooling/util/NDC.h>
34 #include <xmltooling/util/ReloadableXMLFile.h>
35 #include <xmltooling/util/XMLHelper.h>
36 #include <xercesc/util/XMLUniDefs.hpp>
38 using namespace shibsp;
39 using namespace opensaml::saml2md;
40 using namespace opensaml;
41 using namespace xmltooling;
43 using saml1::NameIdentifier;
45 using saml2::EncryptedAttribute;
49 #if defined (_MSC_VER)
50 #pragma warning( push )
51 #pragma warning( disable : 4250 )
54 class XMLExtractorImpl
57 XMLExtractorImpl(const DOMElement* e, Category& log);
59 for (attrmap_t::iterator i = m_attrMap.begin(); i!=m_attrMap.end(); ++i)
60 delete i->second.first;
62 m_document->release();
65 void setDocument(DOMDocument* doc) {
69 void extractAttributes(
70 const Application& application, const char* assertingParty, const NameIdentifier& nameid, vector<Attribute*>& attributes
72 void extractAttributes(
73 const Application& application, const char* assertingParty, const NameID& nameid, vector<Attribute*>& attributes
75 void extractAttributes(
76 const Application& application, const char* assertingParty, const saml1::Attribute& attr, vector<Attribute*>& attributes
78 void extractAttributes(
79 const Application& application, const char* assertingParty, const saml2::Attribute& attr, vector<Attribute*>& attributes
81 void extractAttributes(
82 const Application& application, const char* assertingParty, const Extensions& ext, vector<Attribute*>& attributes
85 void getAttributeIds(vector<string>& attributes) const {
86 attributes.insert(attributes.end(), m_attributeIds.begin(), m_attributeIds.end());
91 DOMDocument* m_document;
93 typedef map< pair<xstring,xstring>,pair< AttributeDecoder*,vector<string> > > attrmap_t;
95 typedef map< pair<string,string>,pair< AttributeDecoder*,vector<string> > > attrmap_t;
98 vector<string> m_attributeIds;
101 class XMLExtractor : public AttributeExtractor, public ReloadableXMLFile
104 XMLExtractor(const DOMElement* e) : ReloadableXMLFile(e, Category::getInstance(SHIBSP_LOGCAT".AttributeExtractor")), m_impl(NULL) {
111 void extractAttributes(
112 const Application& application, const RoleDescriptor* issuer, const XMLObject& xmlObject, vector<Attribute*>& attributes
115 void getAttributeIds(std::vector<std::string>& attributes) const {
117 m_impl->getAttributeIds(attributes);
121 pair<bool,DOMElement*> load();
124 XMLExtractorImpl* m_impl;
127 #if defined (_MSC_VER)
128 #pragma warning( pop )
131 AttributeExtractor* SHIBSP_DLLLOCAL XMLAttributeExtractorFactory(const DOMElement* const & e)
133 return new XMLExtractor(e);
136 static const XMLCh _AttributeDecoder[] = UNICODE_LITERAL_16(A,t,t,r,i,b,u,t,e,D,e,c,o,d,e,r);
137 static const XMLCh Attributes[] = UNICODE_LITERAL_10(A,t,t,r,i,b,u,t,e,s);
138 static const XMLCh _id[] = UNICODE_LITERAL_2(i,d);
139 static const XMLCh _aliases[] = UNICODE_LITERAL_7(a,l,i,a,s,e,s);
140 static const XMLCh _name[] = UNICODE_LITERAL_4(n,a,m,e);
141 static const XMLCh nameFormat[] = UNICODE_LITERAL_10(n,a,m,e,F,o,r,m,a,t);
144 XMLExtractorImpl::XMLExtractorImpl(const DOMElement* e, Category& log) : m_log(log), m_document(NULL)
147 xmltooling::NDC ndc("XMLExtractorImpl");
150 if (!XMLHelper::isNodeNamed(e, shibspconstants::SHIB2ATTRIBUTEMAP_NS, Attributes))
151 throw ConfigurationException("XML AttributeExtractor requires am:Attributes at root of configuration.");
153 DOMElement* child = XMLHelper::getFirstChildElement(e, shibspconstants::SHIB2ATTRIBUTEMAP_NS, saml1::Attribute::LOCAL_NAME);
155 // Check for missing name or id.
156 const XMLCh* name = child->getAttributeNS(NULL, _name);
157 if (!name || !*name) {
158 m_log.warn("skipping Attribute with no name");
159 child = XMLHelper::getNextSiblingElement(child, shibspconstants::SHIB2ATTRIBUTEMAP_NS, saml1::Attribute::LOCAL_NAME);
163 auto_ptr_char id(child->getAttributeNS(NULL, _id));
164 if (!id.get() || !*id.get()) {
165 m_log.warn("skipping Attribute with no id");
166 child = XMLHelper::getNextSiblingElement(child, shibspconstants::SHIB2ATTRIBUTEMAP_NS, saml1::Attribute::LOCAL_NAME);
169 else if (!strcmp(id.get(), "REMOTE_USER")) {
170 m_log.warn("skipping Attribute, id of REMOTE_USER is a reserved name");
171 child = XMLHelper::getNextSiblingElement(child, shibspconstants::SHIB2ATTRIBUTEMAP_NS, saml1::Attribute::LOCAL_NAME);
175 AttributeDecoder* decoder=NULL;
177 DOMElement* dchild = XMLHelper::getFirstChildElement(child, shibspconstants::SHIB2ATTRIBUTEMAP_NS, _AttributeDecoder);
179 auto_ptr<QName> q(XMLHelper::getXSIType(dchild));
181 decoder = SPConfig::getConfig().AttributeDecoderManager.newPlugin(*q.get(), dchild);
184 decoder = SPConfig::getConfig().AttributeDecoderManager.newPlugin(StringAttributeDecoderType, NULL);
186 catch (exception& ex) {
187 m_log.error("skipping Attribute (%s), error building AttributeDecoder: %s", id.get(), ex.what());
191 child = XMLHelper::getNextSiblingElement(child, shibspconstants::SHIB2ATTRIBUTEMAP_NS, saml1::Attribute::LOCAL_NAME);
195 // Empty NameFormat implies the usual Shib URI naming defaults.
196 const XMLCh* format = child->getAttributeNS(NULL, nameFormat);
197 if (!format || XMLString::equals(format, shibspconstants::SHIB1_ATTRIBUTE_NAMESPACE_URI) ||
198 XMLString::equals(format, saml2::Attribute::URI_REFERENCE))
199 format = &chNull; // ignore default Format/Namespace values
201 // Fetch/create the map entry and see if it's a duplicate rule.
203 pair< AttributeDecoder*,vector<string> >& decl = m_attrMap[pair<xstring,xstring>(name,format)];
205 auto_ptr_char n(name);
206 auto_ptr_char f(format);
207 pair< AttributeDecoder*,vector<string> >& decl = m_attrMap[pair<string,string>(n.get(),f.get())];
210 m_log.warn("skipping duplicate Attribute mapping (same name and nameFormat)");
212 child = XMLHelper::getNextSiblingElement(child, shibspconstants::SHIB2ATTRIBUTEMAP_NS, saml1::Attribute::LOCAL_NAME);
216 if (m_log.isInfoEnabled()) {
218 auto_ptr_char n(name);
219 auto_ptr_char f(format);
221 m_log.info("creating mapping for Attribute %s%s%s", n.get(), *f.get() ? ", Format/Namespace:" : "", f.get());
224 decl.first = decoder;
225 decl.second.push_back(id.get());
226 m_attributeIds.push_back(id.get());
228 name = child->getAttributeNS(NULL, _aliases);
230 auto_ptr_char aliases(name);
232 char* start = const_cast<char*>(aliases.get());
233 while (start && *start) {
234 while (*start && isspace(*start))
238 pos = strchr(start,' ');
241 if (strcmp(start, "REMOTE_USER")) {
242 decl.second.push_back(start);
243 m_attributeIds.push_back(start);
246 m_log.warn("skipping alias, REMOTE_USER is a reserved name");
248 start = pos ? pos+1 : NULL;
252 child = XMLHelper::getNextSiblingElement(child, shibspconstants::SHIB2ATTRIBUTEMAP_NS, saml1::Attribute::LOCAL_NAME);
256 void XMLExtractorImpl::extractAttributes(
257 const Application& application, const char* assertingParty, const NameIdentifier& nameid, vector<Attribute*>& attributes
261 map< pair<xstring,xstring>,pair< AttributeDecoder*,vector<string> > >::const_iterator rule;
263 map< pair<string,string>,pair< AttributeDecoder*,vector<string> > >::const_iterator rule;
266 const XMLCh* format = nameid.getFormat();
267 if (!format || !*format)
268 format = NameIdentifier::UNSPECIFIED;
270 if ((rule=m_attrMap.find(pair<xstring,xstring>(format,xstring()))) != m_attrMap.end()) {
272 auto_ptr_char temp(format);
273 if ((rule=m_attrMap.find(pair<string,string>(temp.get(),string()))) != m_attrMap.end()) {
275 Attribute* a = rule->second.first->decode(rule->second.second, &nameid, assertingParty, application.getString("entityID").second);
277 attributes.push_back(a);
279 else if (m_log.isDebugEnabled()) {
281 auto_ptr_char temp(format);
283 m_log.debug("skipping unmapped NameIdentifier with format (%s)", temp.get());
287 void XMLExtractorImpl::extractAttributes(
288 const Application& application, const char* assertingParty, const NameID& nameid, vector<Attribute*>& attributes
292 map< pair<xstring,xstring>,pair< AttributeDecoder*,vector<string> > >::const_iterator rule;
294 map< pair<string,string>,pair< AttributeDecoder*,vector<string> > >::const_iterator rule;
297 const XMLCh* format = nameid.getFormat();
298 if (!format || !*format)
299 format = NameID::UNSPECIFIED;
301 if ((rule=m_attrMap.find(pair<xstring,xstring>(format,xstring()))) != m_attrMap.end()) {
303 auto_ptr_char temp(format);
304 if ((rule=m_attrMap.find(pair<string,string>(temp.get(),string()))) != m_attrMap.end()) {
306 Attribute* a = rule->second.first->decode(rule->second.second, &nameid, assertingParty, application.getString("entityID").second);
308 attributes.push_back(a);
310 else if (m_log.isDebugEnabled()) {
312 auto_ptr_char temp(format);
314 m_log.debug("skipping unmapped NameID with format (%s)", temp.get());
318 void XMLExtractorImpl::extractAttributes(
319 const Application& application, const char* assertingParty, const saml1::Attribute& attr, vector<Attribute*>& attributes
323 map< pair<xstring,xstring>,pair< AttributeDecoder*,vector<string> > >::const_iterator rule;
325 map< pair<string,string>,pair< AttributeDecoder*,vector<string> > >::const_iterator rule;
328 const XMLCh* name = attr.getAttributeName();
329 const XMLCh* format = attr.getAttributeNamespace();
332 if (!format || XMLString::equals(format, shibspconstants::SHIB1_ATTRIBUTE_NAMESPACE_URI))
335 if ((rule=m_attrMap.find(pair<xstring,xstring>(name,format))) != m_attrMap.end()) {
337 auto_ptr_char temp1(name);
338 auto_ptr_char temp2(format);
339 if ((rule=m_attrMap.find(pair<string,string>(temp1.get(),temp2.get()))) != m_attrMap.end()) {
341 Attribute* a = rule->second.first->decode(rule->second.second, &attr, assertingParty, application.getString("entityID").second);
343 attributes.push_back(a);
345 else if (m_log.isInfoEnabled()) {
347 auto_ptr_char temp1(name);
348 auto_ptr_char temp2(format);
350 m_log.info("skipping unmapped SAML 1.x Attribute with Name: %s%s%s", temp1.get(), *temp2.get() ? ", Namespace:" : "", temp2.get());
354 void XMLExtractorImpl::extractAttributes(
355 const Application& application, const char* assertingParty, const saml2::Attribute& attr, vector<Attribute*>& attributes
359 map< pair<xstring,xstring>,pair< AttributeDecoder*,vector<string> > >::const_iterator rule;
361 map< pair<string,string>,pair< AttributeDecoder*,vector<string> > >::const_iterator rule;
364 const XMLCh* name = attr.getName();
365 const XMLCh* format = attr.getNameFormat();
368 if (!format || !*format)
369 format = saml2::Attribute::UNSPECIFIED;
370 else if (XMLString::equals(format, saml2::Attribute::URI_REFERENCE))
373 if ((rule=m_attrMap.find(pair<xstring,xstring>(name,format))) != m_attrMap.end()) {
375 auto_ptr_char temp1(name);
376 auto_ptr_char temp2(format);
377 if ((rule=m_attrMap.find(pair<string,string>(temp1.get(),temp2.get()))) != m_attrMap.end()) {
379 Attribute* a = rule->second.first->decode(rule->second.second, &attr, assertingParty, application.getString("entityID").second);
381 attributes.push_back(a);
383 else if (m_log.isInfoEnabled()) {
385 auto_ptr_char temp1(name);
386 auto_ptr_char temp2(format);
388 m_log.info("skipping unmapped SAML 2.0 Attribute with Name: %s%s%s", temp1.get(), *temp2.get() ? ", Format:" : "", temp2.get());
392 void XMLExtractorImpl::extractAttributes(
393 const Application& application, const char* assertingParty, const Extensions& ext, vector<Attribute*>& attributes
396 const vector<XMLObject*> exts = ext.getUnknownXMLObjects();
397 for (vector<XMLObject*>::const_iterator i = exts.begin(); i!=exts.end(); ++i) {
398 const saml2::Attribute* attr = dynamic_cast<const saml2::Attribute*>(*i);
400 extractAttributes(application, assertingParty, *attr, attributes);
404 void XMLExtractor::extractAttributes(
405 const Application& application, const RoleDescriptor* issuer, const XMLObject& xmlObject, vector<Attribute*>& attributes
411 // Check for assertions.
412 if (XMLString::equals(xmlObject.getElementQName().getLocalPart(), saml1::Assertion::LOCAL_NAME)) {
413 const saml2::Assertion* token2 = dynamic_cast<const saml2::Assertion*>(&xmlObject);
415 auto_ptr_char assertingParty(issuer ? dynamic_cast<const EntityDescriptor*>(issuer->getParent())->getEntityID() : NULL);
416 const vector<saml2::AttributeStatement*>& statements = token2->getAttributeStatements();
417 for (vector<saml2::AttributeStatement*>::const_iterator s = statements.begin(); s!=statements.end(); ++s) {
418 const vector<saml2::Attribute*>& attrs = const_cast<const saml2::AttributeStatement*>(*s)->getAttributes();
419 for (vector<saml2::Attribute*>::const_iterator a = attrs.begin(); a!=attrs.end(); ++a)
420 m_impl->extractAttributes(application, assertingParty.get(), *(*a), attributes);
422 const vector<saml2::EncryptedAttribute*>& encattrs = const_cast<const saml2::AttributeStatement*>(*s)->getEncryptedAttributes();
423 for (vector<saml2::EncryptedAttribute*>::const_iterator ea = encattrs.begin(); ea!=encattrs.end(); ++ea)
424 extractAttributes(application, issuer, *(*ea), attributes);
429 const saml1::Assertion* token1 = dynamic_cast<const saml1::Assertion*>(&xmlObject);
431 auto_ptr_char assertingParty(issuer ? dynamic_cast<const EntityDescriptor*>(issuer->getParent())->getEntityID() : NULL);
432 const vector<saml1::AttributeStatement*>& statements = token1->getAttributeStatements();
433 for (vector<saml1::AttributeStatement*>::const_iterator s = statements.begin(); s!=statements.end(); ++s) {
434 const vector<saml1::Attribute*>& attrs = const_cast<const saml1::AttributeStatement*>(*s)->getAttributes();
435 for (vector<saml1::Attribute*>::const_iterator a = attrs.begin(); a!=attrs.end(); ++a)
436 m_impl->extractAttributes(application, assertingParty.get(), *(*a), attributes);
441 throw AttributeExtractionException("Unable to extract attributes, unknown object type.");
444 // Check for metadata.
445 if (XMLString::equals(xmlObject.getElementQName().getNamespaceURI(), samlconstants::SAML20MD_NS)) {
446 const EntityDescriptor* entity = dynamic_cast<const EntityDescriptor*>(&xmlObject);
448 throw AttributeExtractionException("Unable to extract attributes, unknown metadata object type.");
449 auto_ptr_char assertingParty(issuer ? dynamic_cast<const EntityDescriptor*>(issuer->getParent())->getEntityID() : NULL);
450 const Extensions* ext = entity->getExtensions();
452 m_impl->extractAttributes(application, assertingParty.get(), *ext, attributes);
453 const EntitiesDescriptor* group = dynamic_cast<const EntitiesDescriptor*>(entity->getParent());
455 ext = group->getExtensions();
457 m_impl->extractAttributes(application, assertingParty.get(), *ext, attributes);
458 group = dynamic_cast<const EntitiesDescriptor*>(group->getParent());
463 // Check for attributes.
464 if (XMLString::equals(xmlObject.getElementQName().getLocalPart(), saml1::Attribute::LOCAL_NAME)) {
465 auto_ptr_char assertingParty(issuer ? dynamic_cast<const EntityDescriptor*>(issuer->getParent())->getEntityID() : NULL);
467 const saml2::Attribute* attr2 = dynamic_cast<const saml2::Attribute*>(&xmlObject);
469 return m_impl->extractAttributes(application, assertingParty.get(), *attr2, attributes);
471 const saml1::Attribute* attr1 = dynamic_cast<const saml1::Attribute*>(&xmlObject);
473 return m_impl->extractAttributes(application, assertingParty.get(), *attr1, attributes);
475 throw AttributeExtractionException("Unable to extract attributes, unknown object type.");
478 if (XMLString::equals(xmlObject.getElementQName().getLocalPart(), EncryptedAttribute::LOCAL_NAME)) {
479 const EncryptedAttribute* encattr = dynamic_cast<const EncryptedAttribute*>(&xmlObject);
481 const XMLCh* recipient = application.getXMLString("entityID").second;
482 CredentialResolver* cr = application.getCredentialResolver();
484 m_log.warn("found encrypted attribute, but no CredentialResolver was available");
489 Locker credlocker(cr);
491 MetadataCredentialCriteria mcc(*issuer);
492 auto_ptr<XMLObject> decrypted(encattr->decrypt(*cr, recipient, &mcc));
493 if (m_log.isDebugEnabled())
494 m_log.debugStream() << "decrypted Attribute: " << *(decrypted.get()) << logging::eol;
495 return extractAttributes(application, issuer, *(decrypted.get()), attributes);
498 auto_ptr<XMLObject> decrypted(encattr->decrypt(*cr, recipient));
499 if (m_log.isDebugEnabled())
500 m_log.debugStream() << "decrypted Attribute: " << *(decrypted.get()) << logging::eol;
501 return extractAttributes(application, issuer, *(decrypted.get()), attributes);
504 catch (exception& ex) {
505 m_log.error("caught exception decrypting Attribute: %s", ex.what());
511 // Check for NameIDs.
512 const NameID* name2 = dynamic_cast<const NameID*>(&xmlObject);
514 auto_ptr_char assertingParty(issuer ? dynamic_cast<const EntityDescriptor*>(issuer->getParent())->getEntityID() : NULL);
515 return m_impl->extractAttributes(application, assertingParty.get(), *name2, attributes);
518 const NameIdentifier* name1 = dynamic_cast<const NameIdentifier*>(&xmlObject);
520 auto_ptr_char assertingParty(issuer ? dynamic_cast<const EntityDescriptor*>(issuer->getParent())->getEntityID() : NULL);
521 return m_impl->extractAttributes(application, assertingParty.get(), *name1, attributes);
524 throw AttributeExtractionException("Unable to extract attributes, unknown object type.");
527 pair<bool,DOMElement*> XMLExtractor::load()
529 // Load from source using base class.
530 pair<bool,DOMElement*> raw = ReloadableXMLFile::load();
532 // If we own it, wrap it.
533 XercesJanitor<DOMDocument> docjanitor(raw.first ? raw.second->getOwnerDocument() : NULL);
535 XMLExtractorImpl* impl = new XMLExtractorImpl(raw.second, m_log);
537 // If we held the document, transfer it to the impl. If we didn't, it's a no-op.
538 impl->setDocument(docjanitor.release());
543 return make_pair(false,(DOMElement*)NULL);