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 "SPRequest.h"
27 #include "attribute/AttributeDecoder.h"
28 #include "attribute/resolver/AttributeExtractor.h"
29 #include "util/SPConstants.h"
31 #include <saml/saml1/core/Assertions.h>
32 #include <saml/saml2/core/Assertions.h>
33 #include <saml/saml2/metadata/MetadataCredentialCriteria.h>
34 #include <xmltooling/util/NDC.h>
35 #include <xmltooling/util/ReloadableXMLFile.h>
36 #include <xmltooling/util/XMLHelper.h>
37 #include <xercesc/util/XMLUniDefs.hpp>
39 using namespace shibsp;
40 using namespace opensaml::saml2md;
41 using namespace opensaml;
42 using namespace xmltooling;
43 using namespace log4cpp;
45 using saml1::NameIdentifier;
47 using saml2::EncryptedAttribute;
51 #if defined (_MSC_VER)
52 #pragma warning( push )
53 #pragma warning( disable : 4250 )
56 class XMLExtractorImpl
59 XMLExtractorImpl(const DOMElement* e, Category& log);
61 for (attrmap_t::iterator i = m_attrMap.begin(); i!=m_attrMap.end(); ++i)
62 delete i->second.first;
64 m_document->release();
67 void setDocument(DOMDocument* doc) {
71 void extractAttributes(
72 const Application& application, const char* assertingParty, const NameIdentifier& nameid, multimap<string,Attribute*>& attributes
74 void extractAttributes(
75 const Application& application, const char* assertingParty, const NameID& nameid, multimap<string,Attribute*>& attributes
77 void extractAttributes(
78 const Application& application, const char* assertingParty, const saml1::Attribute& attr, multimap<string,Attribute*>& attributes
80 void extractAttributes(
81 const Application& application, const char* assertingParty, const saml2::Attribute& attr, multimap<string,Attribute*>& attributes
84 void clearHeaders(SPRequest& request) const {
85 for (vector<string>::const_iterator i = m_attributeIds.begin(); i!=m_attributeIds.end(); ++i)
86 request.clearHeader(i->c_str());
91 DOMDocument* m_document;
93 typedef map< pair<xstring,xstring>,pair<AttributeDecoder*,string> > attrmap_t;
95 typedef map< pair<string,string>,pair<AttributeDecoder*,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, multimap<string,Attribute*>& attributes
115 void clearHeaders(SPRequest& request) const {
117 m_impl->clearHeaders(request);
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 _name[] = UNICODE_LITERAL_4(n,a,m,e);
140 static const XMLCh nameFormat[] = UNICODE_LITERAL_10(n,a,m,e,F,o,r,m,a,t);
143 void SHIBSP_API shibsp::registerAttributeExtractors()
145 SPConfig::getConfig().AttributeExtractorManager.registerFactory(XML_ATTRIBUTE_EXTRACTOR, XMLAttributeExtractorFactory);
148 XMLExtractorImpl::XMLExtractorImpl(const DOMElement* e, Category& log) : m_log(log), m_document(NULL)
151 xmltooling::NDC ndc("XMLExtractorImpl");
154 if (!XMLHelper::isNodeNamed(e, shibspconstants::SHIB2ATTRIBUTEMAP_NS, Attributes))
155 throw ConfigurationException("XML AttributeExtractor requires am:Attributes at root of configuration.");
157 DOMElement* child = XMLHelper::getFirstChildElement(e, shibspconstants::SHIB2ATTRIBUTEMAP_NS, saml1::Attribute::LOCAL_NAME);
159 // Check for missing name or id.
160 const XMLCh* name = child->getAttributeNS(NULL, _name);
161 if (!name || !*name) {
162 m_log.warn("skipping Attribute with no name");
163 child = XMLHelper::getNextSiblingElement(child, shibspconstants::SHIB2ATTRIBUTEMAP_NS, saml1::Attribute::LOCAL_NAME);
167 auto_ptr_char id(child->getAttributeNS(NULL, _id));
168 if (!id.get() || !*id.get()) {
169 m_log.warn("skipping Attribute with no id");
170 child = XMLHelper::getNextSiblingElement(child, shibspconstants::SHIB2ATTRIBUTEMAP_NS, saml1::Attribute::LOCAL_NAME);
173 else if (!strcmp(id.get(), "REMOTE_USER")) {
174 m_log.warn("skipping Attribute, id of REMOTE_USER is a reserved name");
175 child = XMLHelper::getNextSiblingElement(child, shibspconstants::SHIB2ATTRIBUTEMAP_NS, saml1::Attribute::LOCAL_NAME);
179 AttributeDecoder* decoder=NULL;
181 DOMElement* dchild = XMLHelper::getFirstChildElement(child, shibspconstants::SHIB2ATTRIBUTEMAP_NS, _AttributeDecoder);
183 auto_ptr<QName> q(XMLHelper::getXSIType(dchild));
185 decoder = SPConfig::getConfig().AttributeDecoderManager.newPlugin(*q.get(), dchild);
188 decoder = SPConfig::getConfig().AttributeDecoderManager.newPlugin(StringAttributeDecoderType, NULL);
190 catch (exception& ex) {
191 m_log.error("skipping Attribute (%s), error building AttributeDecoder: %s", id.get(), ex.what());
195 child = XMLHelper::getNextSiblingElement(child, shibspconstants::SHIB2ATTRIBUTEMAP_NS, saml1::Attribute::LOCAL_NAME);
199 // Empty NameFormat implies the usual Shib URI naming defaults.
200 const XMLCh* format = child->getAttributeNS(NULL, nameFormat);
201 if (!format || XMLString::equals(format, shibspconstants::SHIB1_ATTRIBUTE_NAMESPACE_URI) ||
202 XMLString::equals(format, saml2::Attribute::URI_REFERENCE))
203 format = &chNull; // ignore default Format/Namespace values
205 // Fetch/create the map entry and see if it's a duplicate rule.
207 pair<AttributeDecoder*,string>& decl = m_attrMap[make_pair(name,format)];
209 auto_ptr_char n(name);
210 auto_ptr_char f(format);
211 pair<AttributeDecoder*,string>& decl = m_attrMap[make_pair(n.get(),f.get())];
214 m_log.warn("skipping duplicate Attribute mapping (same name and nameFormat)");
216 child = XMLHelper::getNextSiblingElement(child, shibspconstants::SHIB2ATTRIBUTEMAP_NS, saml1::Attribute::LOCAL_NAME);
220 if (m_log.isInfoEnabled()) {
222 auto_ptr_char n(name);
223 auto_ptr_char f(format);
225 m_log.info("creating mapping for Attribute %s%s%s", n.get(), *f.get() ? ", Format/Namespace:" : "", f.get());
228 decl.first = decoder;
229 decl.second = id.get();
230 m_attributeIds.push_back(id.get());
232 child = XMLHelper::getNextSiblingElement(child, shibspconstants::SHIB2ATTRIBUTEMAP_NS, saml1::Attribute::LOCAL_NAME);
236 void XMLExtractorImpl::extractAttributes(
237 const Application& application, const char* assertingParty, const NameIdentifier& nameid, multimap<string,Attribute*>& attributes
241 map< pair<xstring,xstring>,pair<AttributeDecoder*,string> >::const_iterator rule;
243 map< pair<string,string>,pair<AttributeDecoder*,string> >::const_iterator rule;
246 const XMLCh* format = nameid.getFormat();
247 if (!format || !*format)
248 format = NameIdentifier::UNSPECIFIED;
250 if ((rule=m_attrMap.find(make_pair(format,xstring()))) != m_attrMap.end()) {
252 auto_ptr_char temp(format);
253 if ((rule=m_attrMap.find(make_pair(temp.get(),string()))) != m_attrMap.end()) {
258 rule->second.first->decode(rule->second.second.c_str(), &nameid, assertingParty, application.getString("entityID").second)
264 void XMLExtractorImpl::extractAttributes(
265 const Application& application, const char* assertingParty, const NameID& nameid, multimap<string,Attribute*>& attributes
269 map< pair<xstring,xstring>,pair<AttributeDecoder*,string> >::const_iterator rule;
271 map< pair<string,string>,pair<AttributeDecoder*,string> >::const_iterator rule;
274 const XMLCh* format = nameid.getFormat();
275 if (!format || !*format)
276 format = NameID::UNSPECIFIED;
278 if ((rule=m_attrMap.find(make_pair(format,xstring()))) != m_attrMap.end()) {
280 auto_ptr_char temp(format);
281 if ((rule=m_attrMap.find(make_pair(temp.get(),string()))) != m_attrMap.end()) {
286 rule->second.first->decode(rule->second.second.c_str(), &nameid, assertingParty, application.getString("entityID").second)
292 void XMLExtractorImpl::extractAttributes(
293 const Application& application, const char* assertingParty, const saml1::Attribute& attr, multimap<string,Attribute*>& attributes
297 map< pair<xstring,xstring>,pair<AttributeDecoder*,string> >::const_iterator rule;
299 map< pair<string,string>,pair<AttributeDecoder*,string> >::const_iterator rule;
302 const XMLCh* name = attr.getAttributeName();
303 const XMLCh* format = attr.getAttributeNamespace();
306 if (!format || XMLString::equals(format, shibspconstants::SHIB1_ATTRIBUTE_NAMESPACE_URI))
309 if ((rule=m_attrMap.find(make_pair(name,format))) != m_attrMap.end()) {
311 auto_ptr_char temp1(name);
312 auto_ptr_char temp2(format);
313 if ((rule=m_attrMap.find(make_pair(temp1.get(),temp2.get()))) != m_attrMap.end()) {
318 rule->second.first->decode(rule->second.second.c_str(), &attr, assertingParty, application.getString("entityID").second)
324 void XMLExtractorImpl::extractAttributes(
325 const Application& application, const char* assertingParty, const saml2::Attribute& attr, multimap<string,Attribute*>& attributes
329 map< pair<xstring,xstring>,pair<AttributeDecoder*,string> >::const_iterator rule;
331 map< pair<string,string>,pair<AttributeDecoder*,string> >::const_iterator rule;
334 const XMLCh* name = attr.getName();
335 const XMLCh* format = attr.getNameFormat();
338 if (!format || !*format)
339 format = saml2::Attribute::UNSPECIFIED;
340 else if (XMLString::equals(format, saml2::Attribute::URI_REFERENCE))
343 if ((rule=m_attrMap.find(make_pair(name,format))) != m_attrMap.end()) {
345 auto_ptr_char temp1(name);
346 auto_ptr_char temp2(format);
347 if ((rule=m_attrMap.find(make_pair(temp1.get(),temp2.get()))) != m_attrMap.end()) {
352 rule->second.first->decode(rule->second.second.c_str(), &attr, assertingParty, application.getString("entityID").second)
358 void XMLExtractor::extractAttributes(
359 const Application& application, const RoleDescriptor* issuer, const XMLObject& xmlObject, multimap<string,Attribute*>& attributes
365 // Check for assertions.
366 if (XMLString::equals(xmlObject.getElementQName().getLocalPart(), saml1::Assertion::LOCAL_NAME)) {
367 const saml2::Assertion* token2 = dynamic_cast<const saml2::Assertion*>(&xmlObject);
369 auto_ptr_char assertingParty(issuer ? dynamic_cast<const EntityDescriptor*>(issuer->getParent())->getEntityID() : NULL);
370 const vector<saml2::AttributeStatement*>& statements = token2->getAttributeStatements();
371 for (vector<saml2::AttributeStatement*>::const_iterator s = statements.begin(); s!=statements.end(); ++s) {
372 const vector<saml2::Attribute*>& attrs = const_cast<const saml2::AttributeStatement*>(*s)->getAttributes();
373 for (vector<saml2::Attribute*>::const_iterator a = attrs.begin(); a!=attrs.end(); ++a)
374 m_impl->extractAttributes(application, assertingParty.get(), *(*a), attributes);
376 const vector<saml2::EncryptedAttribute*>& encattrs = const_cast<const saml2::AttributeStatement*>(*s)->getEncryptedAttributes();
377 for (vector<saml2::EncryptedAttribute*>::const_iterator ea = encattrs.begin(); ea!=encattrs.end(); ++ea)
378 extractAttributes(application, issuer, *(*ea), attributes);
383 const saml1::Assertion* token1 = dynamic_cast<const saml1::Assertion*>(&xmlObject);
385 auto_ptr_char assertingParty(issuer ? dynamic_cast<const EntityDescriptor*>(issuer->getParent())->getEntityID() : NULL);
386 const vector<saml1::AttributeStatement*>& statements = token1->getAttributeStatements();
387 for (vector<saml1::AttributeStatement*>::const_iterator s = statements.begin(); s!=statements.end(); ++s) {
388 const vector<saml1::Attribute*>& attrs = const_cast<const saml1::AttributeStatement*>(*s)->getAttributes();
389 for (vector<saml1::Attribute*>::const_iterator a = attrs.begin(); a!=attrs.end(); ++a)
390 m_impl->extractAttributes(application, assertingParty.get(), *(*a), attributes);
395 throw AttributeExtractionException("Unable to extract attributes, unknown object type.");
398 // Check for attributes.
399 if (XMLString::equals(xmlObject.getElementQName().getLocalPart(), saml1::Attribute::LOCAL_NAME)) {
400 auto_ptr_char assertingParty(issuer ? dynamic_cast<const EntityDescriptor*>(issuer->getParent())->getEntityID() : NULL);
402 const saml2::Attribute* attr2 = dynamic_cast<const saml2::Attribute*>(&xmlObject);
404 return m_impl->extractAttributes(application, assertingParty.get(), *attr2, attributes);
406 const saml1::Attribute* attr1 = dynamic_cast<const saml1::Attribute*>(&xmlObject);
408 return m_impl->extractAttributes(application, assertingParty.get(), *attr1, attributes);
410 throw AttributeExtractionException("Unable to extract attributes, unknown object type.");
413 if (XMLString::equals(xmlObject.getElementQName().getLocalPart(), EncryptedAttribute::LOCAL_NAME)) {
414 const EncryptedAttribute* encattr = dynamic_cast<const EncryptedAttribute*>(&xmlObject);
416 const XMLCh* recipient = application.getXMLString("entityID").second;
417 CredentialResolver* cr = application.getCredentialResolver();
419 m_log.warn("found encrypted attribute, but no CredentialResolver was available");
424 Locker credlocker(cr);
426 MetadataCredentialCriteria mcc(*issuer);
427 auto_ptr<XMLObject> decrypted(encattr->decrypt(*cr, recipient, &mcc));
428 return extractAttributes(application, issuer, *(decrypted.get()), attributes);
431 auto_ptr<XMLObject> decrypted(encattr->decrypt(*cr, recipient));
432 return extractAttributes(application, issuer, *(decrypted.get()), attributes);
435 catch (exception& ex) {
436 m_log.error("caught exception decrypting Attribute: %s", ex.what());
442 // Check for NameIDs.
443 const NameID* name2 = dynamic_cast<const NameID*>(&xmlObject);
445 auto_ptr_char assertingParty(issuer ? dynamic_cast<const EntityDescriptor*>(issuer->getParent())->getEntityID() : NULL);
446 return m_impl->extractAttributes(application, assertingParty.get(), *name2, attributes);
449 const NameIdentifier* name1 = dynamic_cast<const NameIdentifier*>(&xmlObject);
451 auto_ptr_char assertingParty(issuer ? dynamic_cast<const EntityDescriptor*>(issuer->getParent())->getEntityID() : NULL);
452 return m_impl->extractAttributes(application, assertingParty.get(), *name1, attributes);
455 throw AttributeExtractionException("Unable to extract attributes, unknown object type.");
458 pair<bool,DOMElement*> XMLExtractor::load()
460 // Load from source using base class.
461 pair<bool,DOMElement*> raw = ReloadableXMLFile::load();
463 // If we own it, wrap it.
464 XercesJanitor<DOMDocument> docjanitor(raw.first ? raw.second->getOwnerDocument() : NULL);
466 XMLExtractorImpl* impl = new XMLExtractorImpl(raw.second, m_log);
468 // If we held the document, transfer it to the impl. If we didn't, it's a no-op.
469 impl->setDocument(docjanitor.release());
474 return make_pair(false,(DOMElement*)NULL);