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 * DOMAttributeDecoder.cpp
24 * Decodes a DOM into an ExtensibleAttribute.
28 #include "attribute/AttributeDecoder.h"
29 #include "attribute/ExtensibleAttribute.h"
31 #include <saml/saml1/core/Assertions.h>
32 #include <saml/saml2/core/Assertions.h>
33 #include <xmltooling/util/XMLHelper.h>
35 using namespace shibsp;
36 using namespace opensaml;
37 using namespace xmltooling;
41 class SHIBSP_DLLLOCAL DOMAttributeDecoder : virtual public AttributeDecoder
44 DOMAttributeDecoder(const DOMElement* e);
45 ~DOMAttributeDecoder() {}
48 const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty=nullptr, const char* relyingParty=nullptr
52 DDF convert(DOMElement* e, bool nameit=true) const;
54 map<pair<xstring,xstring>,string> m_tagMap;
57 AttributeDecoder* SHIBSP_DLLLOCAL DOMAttributeDecoderFactory(const DOMElement* const & e)
59 return new DOMAttributeDecoder(e);
62 static const XMLCh Mapping[] = UNICODE_LITERAL_7(M,a,p,p,i,n,g);
63 static const XMLCh _from[] = UNICODE_LITERAL_4(f,r,o,m);
64 static const XMLCh _to[] = UNICODE_LITERAL_2(t,o);
65 static const XMLCh formatter[] =UNICODE_LITERAL_9(f,o,r,m,a,t,t,e,r);
68 DOMAttributeDecoder::DOMAttributeDecoder(const DOMElement* e)
69 : AttributeDecoder(e), m_formatter(XMLHelper::getAttrString(e, nullptr, formatter))
71 Category& log = Category::getInstance(SHIBSP_LOGCAT".AttributeDecoder.DOM");
73 e = XMLHelper::getFirstChildElement(e, Mapping);
75 if (e->hasAttributeNS(nullptr, _from) && e->hasAttributeNS(nullptr, _to)) {
76 auto_ptr<xmltooling::QName> f(XMLHelper::getNodeValueAsQName(e->getAttributeNodeNS(nullptr, _from)));
77 auto_ptr_char t(e->getAttributeNS(nullptr, _to));
78 if (f.get() && t.get() && *t.get()) {
79 if (log.isDebugEnabled())
80 log.debug("mapping (%s) to (%s)", f->toString().c_str(), t.get());
82 pair< const pair<xstring,xstring>,string>(
83 pair<xstring,xstring>(f->getLocalPart(), f->hasNamespaceURI() ? f->getNamespaceURI() : &chNull),
89 e = XMLHelper::getNextSiblingElement(e, Mapping);
93 Attribute* DOMAttributeDecoder::decode(
94 const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty, const char* relyingParty
97 Category& log = Category::getInstance(SHIBSP_LOGCAT".AttributeDecoder.DOM");
99 if (!xmlObject || !XMLString::equals(saml1::Attribute::LOCAL_NAME, xmlObject->getElementQName().getLocalPart())) {
100 log.warn("XMLObject type not recognized by DOMAttributeDecoder, no values returned");
104 auto_ptr<ExtensibleAttribute> attr(new ExtensibleAttribute(ids, m_formatter.c_str()));
105 DDF dest = attr->getValues();
106 vector<XMLObject*>::const_iterator v,stop;
108 const saml2::Attribute* saml2attr = dynamic_cast<const saml2::Attribute*>(xmlObject);
110 const vector<XMLObject*>& values = saml2attr->getAttributeValues();
113 if (log.isDebugEnabled()) {
114 auto_ptr_char n(saml2attr->getName());
116 "decoding ExtensibleAttribute (%s) from SAML 2 Attribute (%s) with %lu value(s)",
117 ids.front().c_str(), n.get() ? n.get() : "unnamed", values.size()
122 const saml1::Attribute* saml1attr = dynamic_cast<const saml1::Attribute*>(xmlObject);
124 const vector<XMLObject*>& values = saml1attr->getAttributeValues();
127 if (log.isDebugEnabled()) {
128 auto_ptr_char n(saml1attr->getAttributeName());
130 "decoding ExtensibleAttribute (%s) from SAML 1 Attribute (%s) with %lu value(s)",
131 ids.front().c_str(), n.get() ? n.get() : "unnamed", values.size()
136 log.warn("XMLObject type not recognized by DOMAttributeDecoder, no values returned");
141 for (; v!=stop; ++v) {
142 DOMElement* e = (*v)->getDOM();
144 DDF converted = convert(e, false);
145 if (!converted.isnull())
149 log.warn("skipping AttributeValue without a backing DOM");
152 return dest.integer() ? _decode(attr.release()) : nullptr;
155 DDF DOMAttributeDecoder::convert(DOMElement* e, bool nameit) const
159 map<pair<xstring,xstring>,string>::const_iterator mapping;
160 DDF obj = DDF(nullptr).structure();
163 // Name this structure.
164 nsURI = e->getNamespaceURI();
165 local = e->getLocalName();
166 mapping = m_tagMap.find(pair<xstring,xstring>(local,nsURI));
167 if (mapping == m_tagMap.end()) {
168 auto_ptr_char temp(local);
169 obj.name(temp.get());
172 obj.name(mapping->second.c_str());
176 // Process non-xmlns attributes.
177 DOMNamedNodeMap* attrs = e->getAttributes();
178 for (XMLSize_t a = attrs->getLength(); a > 0; --a) {
179 DOMNode* attr = attrs->item(a-1);
180 nsURI = attr->getNamespaceURI();
181 if (XMLString::equals(nsURI, xmlconstants::XMLNS_NS))
183 local = attr->getLocalName();
184 mapping = m_tagMap.find(pair<xstring,xstring>(local, nsURI ? nsURI : &chNull));
185 if (mapping == m_tagMap.end()) {
186 auto_ptr_char temp(local);
187 obj.addmember(temp.get()).string(toUTF8(attr->getNodeValue(), true), false);
190 obj.addmember(mapping->second.c_str()).string(toUTF8(attr->getNodeValue(), true), false);
194 DOMElement* child = XMLHelper::getFirstChildElement(e);
195 if (!child && e->hasChildNodes() && e->getFirstChild()->getNodeType() == DOMNode::TEXT_NODE) {
196 // Attach a _text member if a text node is present.
197 obj.addmember("_string").string(toUTF8(e->getFirstChild()->getNodeValue(), true), false);
201 // Convert the child element.
202 DDF converted = convert(child);
203 if (!converted.isnull()) {
204 // Now identify it and attach it.
205 if (obj[converted.name()].isnull()) {
206 // We're a new child, so just attach as a structure member.
209 else if (obj[converted.name()].islist()) {
210 // We're already a repeating child, so add it to the list.
211 obj[converted.name()].add(converted);
213 else if (obj[converted.name()].isstruct()) {
214 // This is the complex case where we see a child for the second
215 // time and have to convert a structure member into a named list.
216 DDF newlist = DDF(converted.name()).list();
217 newlist.add(obj[converted.name()].remove());
218 newlist.add(converted);
222 child = XMLHelper::getNextSiblingElement(child);
226 // If we're empty, just delete.
227 if (obj.integer() == 0)