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;
42 using namespace log4cpp;
44 using saml1::NameIdentifier;
46 using saml2::EncryptedAttribute;
50 #if defined (_MSC_VER)
51 #pragma warning( push )
52 #pragma warning( disable : 4250 )
55 class XMLExtractorImpl
58 XMLExtractorImpl(const DOMElement* e, Category& log);
60 for (attrmap_t::iterator i = m_attrMap.begin(); i!=m_attrMap.end(); ++i)
61 delete i->second.first;
63 m_document->release();
66 void setDocument(DOMDocument* doc) {
70 void extractAttributes(
71 const Application& application, const char* assertingParty, const NameIdentifier& nameid, multimap<string,Attribute*>& attributes
73 void extractAttributes(
74 const Application& application, const char* assertingParty, const NameID& nameid, multimap<string,Attribute*>& attributes
76 void extractAttributes(
77 const Application& application, const char* assertingParty, const saml1::Attribute& attr, multimap<string,Attribute*>& attributes
79 void extractAttributes(
80 const Application& application, const char* assertingParty, const saml2::Attribute& attr, multimap<string,Attribute*>& attributes
83 void getAttributeIds(vector<string>& attributes) const {
84 attributes.insert(attributes.end(), m_attributeIds.begin(), m_attributeIds.end());
89 DOMDocument* m_document;
91 typedef map< pair<xstring,xstring>,pair<AttributeDecoder*,string> > attrmap_t;
93 typedef map< pair<string,string>,pair<AttributeDecoder*,string> > attrmap_t;
96 vector<string> m_attributeIds;
99 class XMLExtractor : public AttributeExtractor, public ReloadableXMLFile
102 XMLExtractor(const DOMElement* e) : ReloadableXMLFile(e, Category::getInstance(SHIBSP_LOGCAT".AttributeExtractor")), m_impl(NULL) {
109 void extractAttributes(
110 const Application& application, const RoleDescriptor* issuer, const XMLObject& xmlObject, multimap<string,Attribute*>& attributes
113 void getAttributeIds(std::vector<std::string>& attributes) const {
115 m_impl->getAttributeIds(attributes);
119 pair<bool,DOMElement*> load();
122 XMLExtractorImpl* m_impl;
125 #if defined (_MSC_VER)
126 #pragma warning( pop )
129 AttributeExtractor* SHIBSP_DLLLOCAL XMLAttributeExtractorFactory(const DOMElement* const & e)
131 return new XMLExtractor(e);
134 static const XMLCh _AttributeDecoder[] = UNICODE_LITERAL_16(A,t,t,r,i,b,u,t,e,D,e,c,o,d,e,r);
135 static const XMLCh Attributes[] = UNICODE_LITERAL_10(A,t,t,r,i,b,u,t,e,s);
136 static const XMLCh _id[] = UNICODE_LITERAL_2(i,d);
137 static const XMLCh _name[] = UNICODE_LITERAL_4(n,a,m,e);
138 static const XMLCh nameFormat[] = UNICODE_LITERAL_10(n,a,m,e,F,o,r,m,a,t);
141 void SHIBSP_API shibsp::registerAttributeExtractors()
143 SPConfig::getConfig().AttributeExtractorManager.registerFactory(XML_ATTRIBUTE_EXTRACTOR, XMLAttributeExtractorFactory);
146 XMLExtractorImpl::XMLExtractorImpl(const DOMElement* e, Category& log) : m_log(log), m_document(NULL)
149 xmltooling::NDC ndc("XMLExtractorImpl");
152 if (!XMLHelper::isNodeNamed(e, shibspconstants::SHIB2ATTRIBUTEMAP_NS, Attributes))
153 throw ConfigurationException("XML AttributeExtractor requires am:Attributes at root of configuration.");
155 DOMElement* child = XMLHelper::getFirstChildElement(e, shibspconstants::SHIB2ATTRIBUTEMAP_NS, saml1::Attribute::LOCAL_NAME);
157 // Check for missing name or id.
158 const XMLCh* name = child->getAttributeNS(NULL, _name);
159 if (!name || !*name) {
160 m_log.warn("skipping Attribute with no name");
161 child = XMLHelper::getNextSiblingElement(child, shibspconstants::SHIB2ATTRIBUTEMAP_NS, saml1::Attribute::LOCAL_NAME);
165 auto_ptr_char id(child->getAttributeNS(NULL, _id));
166 if (!id.get() || !*id.get()) {
167 m_log.warn("skipping Attribute with no id");
168 child = XMLHelper::getNextSiblingElement(child, shibspconstants::SHIB2ATTRIBUTEMAP_NS, saml1::Attribute::LOCAL_NAME);
171 else if (!strcmp(id.get(), "REMOTE_USER")) {
172 m_log.warn("skipping Attribute, id of REMOTE_USER is a reserved name");
173 child = XMLHelper::getNextSiblingElement(child, shibspconstants::SHIB2ATTRIBUTEMAP_NS, saml1::Attribute::LOCAL_NAME);
177 AttributeDecoder* decoder=NULL;
179 DOMElement* dchild = XMLHelper::getFirstChildElement(child, shibspconstants::SHIB2ATTRIBUTEMAP_NS, _AttributeDecoder);
181 auto_ptr<QName> q(XMLHelper::getXSIType(dchild));
183 decoder = SPConfig::getConfig().AttributeDecoderManager.newPlugin(*q.get(), dchild);
186 decoder = SPConfig::getConfig().AttributeDecoderManager.newPlugin(StringAttributeDecoderType, NULL);
188 catch (exception& ex) {
189 m_log.error("skipping Attribute (%s), error building AttributeDecoder: %s", id.get(), ex.what());
193 child = XMLHelper::getNextSiblingElement(child, shibspconstants::SHIB2ATTRIBUTEMAP_NS, saml1::Attribute::LOCAL_NAME);
197 // Empty NameFormat implies the usual Shib URI naming defaults.
198 const XMLCh* format = child->getAttributeNS(NULL, nameFormat);
199 if (!format || XMLString::equals(format, shibspconstants::SHIB1_ATTRIBUTE_NAMESPACE_URI) ||
200 XMLString::equals(format, saml2::Attribute::URI_REFERENCE))
201 format = &chNull; // ignore default Format/Namespace values
203 // Fetch/create the map entry and see if it's a duplicate rule.
205 pair<AttributeDecoder*,string>& decl = m_attrMap[make_pair(name,format)];
207 auto_ptr_char n(name);
208 auto_ptr_char f(format);
209 pair<AttributeDecoder*,string>& decl = m_attrMap[make_pair(n.get(),f.get())];
212 m_log.warn("skipping duplicate Attribute mapping (same name and nameFormat)");
214 child = XMLHelper::getNextSiblingElement(child, shibspconstants::SHIB2ATTRIBUTEMAP_NS, saml1::Attribute::LOCAL_NAME);
218 if (m_log.isInfoEnabled()) {
220 auto_ptr_char n(name);
221 auto_ptr_char f(format);
223 m_log.info("creating mapping for Attribute %s%s%s", n.get(), *f.get() ? ", Format/Namespace:" : "", f.get());
226 decl.first = decoder;
227 decl.second = id.get();
228 m_attributeIds.push_back(id.get());
230 child = XMLHelper::getNextSiblingElement(child, shibspconstants::SHIB2ATTRIBUTEMAP_NS, saml1::Attribute::LOCAL_NAME);
234 void XMLExtractorImpl::extractAttributes(
235 const Application& application, const char* assertingParty, const NameIdentifier& nameid, multimap<string,Attribute*>& attributes
239 map< pair<xstring,xstring>,pair<AttributeDecoder*,string> >::const_iterator rule;
241 map< pair<string,string>,pair<AttributeDecoder*,string> >::const_iterator rule;
244 const XMLCh* format = nameid.getFormat();
245 if (!format || !*format)
246 format = NameIdentifier::UNSPECIFIED;
248 if ((rule=m_attrMap.find(make_pair(format,xstring()))) != m_attrMap.end()) {
250 auto_ptr_char temp(format);
251 if ((rule=m_attrMap.find(make_pair(temp.get(),string()))) != m_attrMap.end()) {
256 rule->second.first->decode(rule->second.second.c_str(), &nameid, assertingParty, application.getString("entityID").second)
262 void XMLExtractorImpl::extractAttributes(
263 const Application& application, const char* assertingParty, const NameID& nameid, multimap<string,Attribute*>& attributes
267 map< pair<xstring,xstring>,pair<AttributeDecoder*,string> >::const_iterator rule;
269 map< pair<string,string>,pair<AttributeDecoder*,string> >::const_iterator rule;
272 const XMLCh* format = nameid.getFormat();
273 if (!format || !*format)
274 format = NameID::UNSPECIFIED;
276 if ((rule=m_attrMap.find(make_pair(format,xstring()))) != m_attrMap.end()) {
278 auto_ptr_char temp(format);
279 if ((rule=m_attrMap.find(make_pair(temp.get(),string()))) != m_attrMap.end()) {
284 rule->second.first->decode(rule->second.second.c_str(), &nameid, assertingParty, application.getString("entityID").second)
290 void XMLExtractorImpl::extractAttributes(
291 const Application& application, const char* assertingParty, const saml1::Attribute& attr, multimap<string,Attribute*>& attributes
295 map< pair<xstring,xstring>,pair<AttributeDecoder*,string> >::const_iterator rule;
297 map< pair<string,string>,pair<AttributeDecoder*,string> >::const_iterator rule;
300 const XMLCh* name = attr.getAttributeName();
301 const XMLCh* format = attr.getAttributeNamespace();
304 if (!format || XMLString::equals(format, shibspconstants::SHIB1_ATTRIBUTE_NAMESPACE_URI))
307 if ((rule=m_attrMap.find(make_pair(name,format))) != m_attrMap.end()) {
309 auto_ptr_char temp1(name);
310 auto_ptr_char temp2(format);
311 if ((rule=m_attrMap.find(make_pair(temp1.get(),temp2.get()))) != m_attrMap.end()) {
316 rule->second.first->decode(rule->second.second.c_str(), &attr, assertingParty, application.getString("entityID").second)
322 void XMLExtractorImpl::extractAttributes(
323 const Application& application, const char* assertingParty, const saml2::Attribute& attr, multimap<string,Attribute*>& attributes
327 map< pair<xstring,xstring>,pair<AttributeDecoder*,string> >::const_iterator rule;
329 map< pair<string,string>,pair<AttributeDecoder*,string> >::const_iterator rule;
332 const XMLCh* name = attr.getName();
333 const XMLCh* format = attr.getNameFormat();
336 if (!format || !*format)
337 format = saml2::Attribute::UNSPECIFIED;
338 else if (XMLString::equals(format, saml2::Attribute::URI_REFERENCE))
341 if ((rule=m_attrMap.find(make_pair(name,format))) != m_attrMap.end()) {
343 auto_ptr_char temp1(name);
344 auto_ptr_char temp2(format);
345 if ((rule=m_attrMap.find(make_pair(temp1.get(),temp2.get()))) != m_attrMap.end()) {
350 rule->second.first->decode(rule->second.second.c_str(), &attr, assertingParty, application.getString("entityID").second)
356 void XMLExtractor::extractAttributes(
357 const Application& application, const RoleDescriptor* issuer, const XMLObject& xmlObject, multimap<string,Attribute*>& attributes
363 // Check for assertions.
364 if (XMLString::equals(xmlObject.getElementQName().getLocalPart(), saml1::Assertion::LOCAL_NAME)) {
365 const saml2::Assertion* token2 = dynamic_cast<const saml2::Assertion*>(&xmlObject);
367 auto_ptr_char assertingParty(issuer ? dynamic_cast<const EntityDescriptor*>(issuer->getParent())->getEntityID() : NULL);
368 const vector<saml2::AttributeStatement*>& statements = token2->getAttributeStatements();
369 for (vector<saml2::AttributeStatement*>::const_iterator s = statements.begin(); s!=statements.end(); ++s) {
370 const vector<saml2::Attribute*>& attrs = const_cast<const saml2::AttributeStatement*>(*s)->getAttributes();
371 for (vector<saml2::Attribute*>::const_iterator a = attrs.begin(); a!=attrs.end(); ++a)
372 m_impl->extractAttributes(application, assertingParty.get(), *(*a), attributes);
374 const vector<saml2::EncryptedAttribute*>& encattrs = const_cast<const saml2::AttributeStatement*>(*s)->getEncryptedAttributes();
375 for (vector<saml2::EncryptedAttribute*>::const_iterator ea = encattrs.begin(); ea!=encattrs.end(); ++ea)
376 extractAttributes(application, issuer, *(*ea), attributes);
381 const saml1::Assertion* token1 = dynamic_cast<const saml1::Assertion*>(&xmlObject);
383 auto_ptr_char assertingParty(issuer ? dynamic_cast<const EntityDescriptor*>(issuer->getParent())->getEntityID() : NULL);
384 const vector<saml1::AttributeStatement*>& statements = token1->getAttributeStatements();
385 for (vector<saml1::AttributeStatement*>::const_iterator s = statements.begin(); s!=statements.end(); ++s) {
386 const vector<saml1::Attribute*>& attrs = const_cast<const saml1::AttributeStatement*>(*s)->getAttributes();
387 for (vector<saml1::Attribute*>::const_iterator a = attrs.begin(); a!=attrs.end(); ++a)
388 m_impl->extractAttributes(application, assertingParty.get(), *(*a), attributes);
393 throw AttributeExtractionException("Unable to extract attributes, unknown object type.");
396 // Check for attributes.
397 if (XMLString::equals(xmlObject.getElementQName().getLocalPart(), saml1::Attribute::LOCAL_NAME)) {
398 auto_ptr_char assertingParty(issuer ? dynamic_cast<const EntityDescriptor*>(issuer->getParent())->getEntityID() : NULL);
400 const saml2::Attribute* attr2 = dynamic_cast<const saml2::Attribute*>(&xmlObject);
402 return m_impl->extractAttributes(application, assertingParty.get(), *attr2, attributes);
404 const saml1::Attribute* attr1 = dynamic_cast<const saml1::Attribute*>(&xmlObject);
406 return m_impl->extractAttributes(application, assertingParty.get(), *attr1, attributes);
408 throw AttributeExtractionException("Unable to extract attributes, unknown object type.");
411 if (XMLString::equals(xmlObject.getElementQName().getLocalPart(), EncryptedAttribute::LOCAL_NAME)) {
412 const EncryptedAttribute* encattr = dynamic_cast<const EncryptedAttribute*>(&xmlObject);
414 const XMLCh* recipient = application.getXMLString("entityID").second;
415 CredentialResolver* cr = application.getCredentialResolver();
417 m_log.warn("found encrypted attribute, but no CredentialResolver was available");
422 Locker credlocker(cr);
424 MetadataCredentialCriteria mcc(*issuer);
425 auto_ptr<XMLObject> decrypted(encattr->decrypt(*cr, recipient, &mcc));
426 return extractAttributes(application, issuer, *(decrypted.get()), attributes);
429 auto_ptr<XMLObject> decrypted(encattr->decrypt(*cr, recipient));
430 return extractAttributes(application, issuer, *(decrypted.get()), attributes);
433 catch (exception& ex) {
434 m_log.error("caught exception decrypting Attribute: %s", ex.what());
440 // Check for NameIDs.
441 const NameID* name2 = dynamic_cast<const NameID*>(&xmlObject);
443 auto_ptr_char assertingParty(issuer ? dynamic_cast<const EntityDescriptor*>(issuer->getParent())->getEntityID() : NULL);
444 return m_impl->extractAttributes(application, assertingParty.get(), *name2, attributes);
447 const NameIdentifier* name1 = dynamic_cast<const NameIdentifier*>(&xmlObject);
449 auto_ptr_char assertingParty(issuer ? dynamic_cast<const EntityDescriptor*>(issuer->getParent())->getEntityID() : NULL);
450 return m_impl->extractAttributes(application, assertingParty.get(), *name1, attributes);
453 throw AttributeExtractionException("Unable to extract attributes, unknown object type.");
456 pair<bool,DOMElement*> XMLExtractor::load()
458 // Load from source using base class.
459 pair<bool,DOMElement*> raw = ReloadableXMLFile::load();
461 // If we own it, wrap it.
462 XercesJanitor<DOMDocument> docjanitor(raw.first ? raw.second->getOwnerDocument() : NULL);
464 XMLExtractorImpl* impl = new XMLExtractorImpl(raw.second, m_log);
466 // If we held the document, transfer it to the impl. If we didn't, it's a no-op.
467 impl->setDocument(docjanitor.release());
472 return make_pair(false,(DOMElement*)NULL);