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
82 void getAttributeIds(vector<string>& attributes) const {
83 attributes.insert(attributes.end(), m_attributeIds.begin(), m_attributeIds.end());
88 DOMDocument* m_document;
90 typedef map< pair<xstring,xstring>,pair<AttributeDecoder*,string> > attrmap_t;
92 typedef map< pair<string,string>,pair<AttributeDecoder*,string> > attrmap_t;
95 vector<string> m_attributeIds;
98 class XMLExtractor : public AttributeExtractor, public ReloadableXMLFile
101 XMLExtractor(const DOMElement* e) : ReloadableXMLFile(e, Category::getInstance(SHIBSP_LOGCAT".AttributeExtractor")), m_impl(NULL) {
108 void extractAttributes(
109 const Application& application, const RoleDescriptor* issuer, const XMLObject& xmlObject, vector<Attribute*>& attributes
112 void getAttributeIds(std::vector<std::string>& attributes) const {
114 m_impl->getAttributeIds(attributes);
118 pair<bool,DOMElement*> load();
121 XMLExtractorImpl* m_impl;
124 #if defined (_MSC_VER)
125 #pragma warning( pop )
128 AttributeExtractor* SHIBSP_DLLLOCAL XMLAttributeExtractorFactory(const DOMElement* const & e)
130 return new XMLExtractor(e);
133 static const XMLCh _AttributeDecoder[] = UNICODE_LITERAL_16(A,t,t,r,i,b,u,t,e,D,e,c,o,d,e,r);
134 static const XMLCh Attributes[] = UNICODE_LITERAL_10(A,t,t,r,i,b,u,t,e,s);
135 static const XMLCh _id[] = UNICODE_LITERAL_2(i,d);
136 static const XMLCh _name[] = UNICODE_LITERAL_4(n,a,m,e);
137 static const XMLCh nameFormat[] = UNICODE_LITERAL_10(n,a,m,e,F,o,r,m,a,t);
140 void SHIBSP_API shibsp::registerAttributeExtractors()
142 SPConfig::getConfig().AttributeExtractorManager.registerFactory(XML_ATTRIBUTE_EXTRACTOR, XMLAttributeExtractorFactory);
145 XMLExtractorImpl::XMLExtractorImpl(const DOMElement* e, Category& log) : m_log(log), m_document(NULL)
148 xmltooling::NDC ndc("XMLExtractorImpl");
151 if (!XMLHelper::isNodeNamed(e, shibspconstants::SHIB2ATTRIBUTEMAP_NS, Attributes))
152 throw ConfigurationException("XML AttributeExtractor requires am:Attributes at root of configuration.");
154 DOMElement* child = XMLHelper::getFirstChildElement(e, shibspconstants::SHIB2ATTRIBUTEMAP_NS, saml1::Attribute::LOCAL_NAME);
156 // Check for missing name or id.
157 const XMLCh* name = child->getAttributeNS(NULL, _name);
158 if (!name || !*name) {
159 m_log.warn("skipping Attribute with no name");
160 child = XMLHelper::getNextSiblingElement(child, shibspconstants::SHIB2ATTRIBUTEMAP_NS, saml1::Attribute::LOCAL_NAME);
164 auto_ptr_char id(child->getAttributeNS(NULL, _id));
165 if (!id.get() || !*id.get()) {
166 m_log.warn("skipping Attribute with no id");
167 child = XMLHelper::getNextSiblingElement(child, shibspconstants::SHIB2ATTRIBUTEMAP_NS, saml1::Attribute::LOCAL_NAME);
170 else if (!strcmp(id.get(), "REMOTE_USER")) {
171 m_log.warn("skipping Attribute, id of REMOTE_USER is a reserved name");
172 child = XMLHelper::getNextSiblingElement(child, shibspconstants::SHIB2ATTRIBUTEMAP_NS, saml1::Attribute::LOCAL_NAME);
176 AttributeDecoder* decoder=NULL;
178 DOMElement* dchild = XMLHelper::getFirstChildElement(child, shibspconstants::SHIB2ATTRIBUTEMAP_NS, _AttributeDecoder);
180 auto_ptr<QName> q(XMLHelper::getXSIType(dchild));
182 decoder = SPConfig::getConfig().AttributeDecoderManager.newPlugin(*q.get(), dchild);
185 decoder = SPConfig::getConfig().AttributeDecoderManager.newPlugin(StringAttributeDecoderType, NULL);
187 catch (exception& ex) {
188 m_log.error("skipping Attribute (%s), error building AttributeDecoder: %s", id.get(), ex.what());
192 child = XMLHelper::getNextSiblingElement(child, shibspconstants::SHIB2ATTRIBUTEMAP_NS, saml1::Attribute::LOCAL_NAME);
196 // Empty NameFormat implies the usual Shib URI naming defaults.
197 const XMLCh* format = child->getAttributeNS(NULL, nameFormat);
198 if (!format || XMLString::equals(format, shibspconstants::SHIB1_ATTRIBUTE_NAMESPACE_URI) ||
199 XMLString::equals(format, saml2::Attribute::URI_REFERENCE))
200 format = &chNull; // ignore default Format/Namespace values
202 // Fetch/create the map entry and see if it's a duplicate rule.
204 pair<AttributeDecoder*,string>& decl = m_attrMap[make_pair(name,format)];
206 auto_ptr_char n(name);
207 auto_ptr_char f(format);
208 pair<AttributeDecoder*,string>& decl = m_attrMap[make_pair(n.get(),f.get())];
211 m_log.warn("skipping duplicate Attribute mapping (same name and nameFormat)");
213 child = XMLHelper::getNextSiblingElement(child, shibspconstants::SHIB2ATTRIBUTEMAP_NS, saml1::Attribute::LOCAL_NAME);
217 if (m_log.isInfoEnabled()) {
219 auto_ptr_char n(name);
220 auto_ptr_char f(format);
222 m_log.info("creating mapping for Attribute %s%s%s", n.get(), *f.get() ? ", Format/Namespace:" : "", f.get());
225 decl.first = decoder;
226 decl.second = id.get();
227 m_attributeIds.push_back(id.get());
229 child = XMLHelper::getNextSiblingElement(child, shibspconstants::SHIB2ATTRIBUTEMAP_NS, saml1::Attribute::LOCAL_NAME);
233 void XMLExtractorImpl::extractAttributes(
234 const Application& application, const char* assertingParty, const NameIdentifier& nameid, vector<Attribute*>& attributes
238 map< pair<xstring,xstring>,pair<AttributeDecoder*,string> >::const_iterator rule;
240 map< pair<string,string>,pair<AttributeDecoder*,string> >::const_iterator rule;
243 const XMLCh* format = nameid.getFormat();
244 if (!format || !*format)
245 format = NameIdentifier::UNSPECIFIED;
247 if ((rule=m_attrMap.find(make_pair(format,xstring()))) != m_attrMap.end()) {
249 auto_ptr_char temp(format);
250 if ((rule=m_attrMap.find(make_pair(temp.get(),string()))) != m_attrMap.end()) {
252 Attribute* a = rule->second.first->decode(rule->second.second.c_str(), &nameid, assertingParty, application.getString("entityID").second);
254 attributes.push_back(a);
258 void XMLExtractorImpl::extractAttributes(
259 const Application& application, const char* assertingParty, const NameID& nameid, vector<Attribute*>& attributes
263 map< pair<xstring,xstring>,pair<AttributeDecoder*,string> >::const_iterator rule;
265 map< pair<string,string>,pair<AttributeDecoder*,string> >::const_iterator rule;
268 const XMLCh* format = nameid.getFormat();
269 if (!format || !*format)
270 format = NameID::UNSPECIFIED;
272 if ((rule=m_attrMap.find(make_pair(format,xstring()))) != m_attrMap.end()) {
274 auto_ptr_char temp(format);
275 if ((rule=m_attrMap.find(make_pair(temp.get(),string()))) != m_attrMap.end()) {
277 Attribute* a = rule->second.first->decode(rule->second.second.c_str(), &nameid, assertingParty, application.getString("entityID").second);
279 attributes.push_back(a);
283 void XMLExtractorImpl::extractAttributes(
284 const Application& application, const char* assertingParty, const saml1::Attribute& attr, vector<Attribute*>& attributes
288 map< pair<xstring,xstring>,pair<AttributeDecoder*,string> >::const_iterator rule;
290 map< pair<string,string>,pair<AttributeDecoder*,string> >::const_iterator rule;
293 const XMLCh* name = attr.getAttributeName();
294 const XMLCh* format = attr.getAttributeNamespace();
297 if (!format || XMLString::equals(format, shibspconstants::SHIB1_ATTRIBUTE_NAMESPACE_URI))
300 if ((rule=m_attrMap.find(make_pair(name,format))) != m_attrMap.end()) {
302 auto_ptr_char temp1(name);
303 auto_ptr_char temp2(format);
304 if ((rule=m_attrMap.find(make_pair(temp1.get(),temp2.get()))) != m_attrMap.end()) {
306 Attribute* a = rule->second.first->decode(rule->second.second.c_str(), &attr, assertingParty, application.getString("entityID").second);
308 attributes.push_back(a);
312 void XMLExtractorImpl::extractAttributes(
313 const Application& application, const char* assertingParty, const saml2::Attribute& attr, vector<Attribute*>& attributes
317 map< pair<xstring,xstring>,pair<AttributeDecoder*,string> >::const_iterator rule;
319 map< pair<string,string>,pair<AttributeDecoder*,string> >::const_iterator rule;
322 const XMLCh* name = attr.getName();
323 const XMLCh* format = attr.getNameFormat();
326 if (!format || !*format)
327 format = saml2::Attribute::UNSPECIFIED;
328 else if (XMLString::equals(format, saml2::Attribute::URI_REFERENCE))
331 if ((rule=m_attrMap.find(make_pair(name,format))) != m_attrMap.end()) {
333 auto_ptr_char temp1(name);
334 auto_ptr_char temp2(format);
335 if ((rule=m_attrMap.find(make_pair(temp1.get(),temp2.get()))) != m_attrMap.end()) {
337 Attribute* a = rule->second.first->decode(rule->second.second.c_str(), &attr, assertingParty, application.getString("entityID").second);
339 attributes.push_back(a);
343 void XMLExtractor::extractAttributes(
344 const Application& application, const RoleDescriptor* issuer, const XMLObject& xmlObject, vector<Attribute*>& attributes
350 // Check for assertions.
351 if (XMLString::equals(xmlObject.getElementQName().getLocalPart(), saml1::Assertion::LOCAL_NAME)) {
352 const saml2::Assertion* token2 = dynamic_cast<const saml2::Assertion*>(&xmlObject);
354 auto_ptr_char assertingParty(issuer ? dynamic_cast<const EntityDescriptor*>(issuer->getParent())->getEntityID() : NULL);
355 const vector<saml2::AttributeStatement*>& statements = token2->getAttributeStatements();
356 for (vector<saml2::AttributeStatement*>::const_iterator s = statements.begin(); s!=statements.end(); ++s) {
357 const vector<saml2::Attribute*>& attrs = const_cast<const saml2::AttributeStatement*>(*s)->getAttributes();
358 for (vector<saml2::Attribute*>::const_iterator a = attrs.begin(); a!=attrs.end(); ++a)
359 m_impl->extractAttributes(application, assertingParty.get(), *(*a), attributes);
361 const vector<saml2::EncryptedAttribute*>& encattrs = const_cast<const saml2::AttributeStatement*>(*s)->getEncryptedAttributes();
362 for (vector<saml2::EncryptedAttribute*>::const_iterator ea = encattrs.begin(); ea!=encattrs.end(); ++ea)
363 extractAttributes(application, issuer, *(*ea), attributes);
368 const saml1::Assertion* token1 = dynamic_cast<const saml1::Assertion*>(&xmlObject);
370 auto_ptr_char assertingParty(issuer ? dynamic_cast<const EntityDescriptor*>(issuer->getParent())->getEntityID() : NULL);
371 const vector<saml1::AttributeStatement*>& statements = token1->getAttributeStatements();
372 for (vector<saml1::AttributeStatement*>::const_iterator s = statements.begin(); s!=statements.end(); ++s) {
373 const vector<saml1::Attribute*>& attrs = const_cast<const saml1::AttributeStatement*>(*s)->getAttributes();
374 for (vector<saml1::Attribute*>::const_iterator a = attrs.begin(); a!=attrs.end(); ++a)
375 m_impl->extractAttributes(application, assertingParty.get(), *(*a), attributes);
380 throw AttributeExtractionException("Unable to extract attributes, unknown object type.");
383 // Check for attributes.
384 if (XMLString::equals(xmlObject.getElementQName().getLocalPart(), saml1::Attribute::LOCAL_NAME)) {
385 auto_ptr_char assertingParty(issuer ? dynamic_cast<const EntityDescriptor*>(issuer->getParent())->getEntityID() : NULL);
387 const saml2::Attribute* attr2 = dynamic_cast<const saml2::Attribute*>(&xmlObject);
389 return m_impl->extractAttributes(application, assertingParty.get(), *attr2, attributes);
391 const saml1::Attribute* attr1 = dynamic_cast<const saml1::Attribute*>(&xmlObject);
393 return m_impl->extractAttributes(application, assertingParty.get(), *attr1, attributes);
395 throw AttributeExtractionException("Unable to extract attributes, unknown object type.");
398 if (XMLString::equals(xmlObject.getElementQName().getLocalPart(), EncryptedAttribute::LOCAL_NAME)) {
399 const EncryptedAttribute* encattr = dynamic_cast<const EncryptedAttribute*>(&xmlObject);
401 const XMLCh* recipient = application.getXMLString("entityID").second;
402 CredentialResolver* cr = application.getCredentialResolver();
404 m_log.warn("found encrypted attribute, but no CredentialResolver was available");
409 Locker credlocker(cr);
411 MetadataCredentialCriteria mcc(*issuer);
412 auto_ptr<XMLObject> decrypted(encattr->decrypt(*cr, recipient, &mcc));
413 return extractAttributes(application, issuer, *(decrypted.get()), attributes);
416 auto_ptr<XMLObject> decrypted(encattr->decrypt(*cr, recipient));
417 return extractAttributes(application, issuer, *(decrypted.get()), attributes);
420 catch (exception& ex) {
421 m_log.error("caught exception decrypting Attribute: %s", ex.what());
427 // Check for NameIDs.
428 const NameID* name2 = dynamic_cast<const NameID*>(&xmlObject);
430 auto_ptr_char assertingParty(issuer ? dynamic_cast<const EntityDescriptor*>(issuer->getParent())->getEntityID() : NULL);
431 return m_impl->extractAttributes(application, assertingParty.get(), *name2, attributes);
434 const NameIdentifier* name1 = dynamic_cast<const NameIdentifier*>(&xmlObject);
436 auto_ptr_char assertingParty(issuer ? dynamic_cast<const EntityDescriptor*>(issuer->getParent())->getEntityID() : NULL);
437 return m_impl->extractAttributes(application, assertingParty.get(), *name1, attributes);
440 throw AttributeExtractionException("Unable to extract attributes, unknown object type.");
443 pair<bool,DOMElement*> XMLExtractor::load()
445 // Load from source using base class.
446 pair<bool,DOMElement*> raw = ReloadableXMLFile::load();
448 // If we own it, wrap it.
449 XercesJanitor<DOMDocument> docjanitor(raw.first ? raw.second->getOwnerDocument() : NULL);
451 XMLExtractorImpl* impl = new XMLExtractorImpl(raw.second, m_log);
453 // If we held the document, transfer it to the impl. If we didn't, it's a no-op.
454 impl->setDocument(docjanitor.release());
459 return make_pair(false,(DOMElement*)NULL);