2 * Copyright 2009-2010 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 * DOMAttributeDecoder.cpp
20 * Decodes a DOM into an ExtensibleAttribute.
24 #include "attribute/AttributeDecoder.h"
25 #include "attribute/ExtensibleAttribute.h"
27 #include <saml/saml1/core/Assertions.h>
28 #include <saml/saml2/core/Assertions.h>
29 #include <xmltooling/util/XMLHelper.h>
31 using namespace shibsp;
32 using namespace opensaml;
33 using namespace xmltooling;
37 class SHIBSP_DLLLOCAL DOMAttributeDecoder : virtual public AttributeDecoder
40 DOMAttributeDecoder(const DOMElement* e);
41 ~DOMAttributeDecoder() {}
44 const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty=nullptr, const char* relyingParty=nullptr
48 DDF convert(DOMElement* e, bool nameit=true) const;
49 auto_ptr_char m_formatter;
50 map<pair<xstring,xstring>,string> m_tagMap;
53 AttributeDecoder* SHIBSP_DLLLOCAL DOMAttributeDecoderFactory(const DOMElement* const & e)
55 return new DOMAttributeDecoder(e);
58 static const XMLCh Mapping[] = UNICODE_LITERAL_7(M,a,p,p,i,n,g);
59 static const XMLCh _from[] = UNICODE_LITERAL_4(f,r,o,m);
60 static const XMLCh _to[] = UNICODE_LITERAL_2(t,o);
61 static const XMLCh formatter[] = UNICODE_LITERAL_9(f,o,r,m,a,t,t,e,r);
64 DOMAttributeDecoder::DOMAttributeDecoder(const DOMElement* e)
65 : AttributeDecoder(e), m_formatter(e ? e->getAttributeNS(nullptr,formatter) : nullptr)
67 Category& log = Category::getInstance(SHIBSP_LOGCAT".AttributeDecoder.DOM");
69 e = e ? XMLHelper::getFirstChildElement(e, Mapping) : nullptr;
71 if (e->hasAttributeNS(nullptr, _from) && e->hasAttributeNS(nullptr, _to)) {
72 auto_ptr<xmltooling::QName> f(XMLHelper::getNodeValueAsQName(e->getAttributeNodeNS(nullptr, _from)));
73 auto_ptr_char t(e->getAttributeNS(nullptr, _to));
74 if (f.get() && t.get() && *t.get()) {
75 if (log.isDebugEnabled())
76 log.debug("mapping (%s) to (%s)", f->toString().c_str(), t.get());
78 pair< const pair<xstring,xstring>,string>(
79 pair<xstring,xstring>(f->getLocalPart(), f->hasNamespaceURI() ? f->getNamespaceURI() : &chNull),
85 e = XMLHelper::getNextSiblingElement(e, Mapping);
89 Attribute* DOMAttributeDecoder::decode(
90 const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty, const char* relyingParty
93 Category& log = Category::getInstance(SHIBSP_LOGCAT".AttributeDecoder.DOM");
95 if (!xmlObject || !XMLString::equals(saml1::Attribute::LOCAL_NAME, xmlObject->getElementQName().getLocalPart())) {
96 log.warn("XMLObject type not recognized by DOMAttributeDecoder, no values returned");
100 auto_ptr<ExtensibleAttribute> attr(new ExtensibleAttribute(ids, m_formatter.get()));
101 DDF dest = attr->getValues();
102 vector<XMLObject*>::const_iterator v,stop;
104 const saml2::Attribute* saml2attr = dynamic_cast<const saml2::Attribute*>(xmlObject);
106 const vector<XMLObject*>& values = saml2attr->getAttributeValues();
109 if (log.isDebugEnabled()) {
110 auto_ptr_char n(saml2attr->getName());
112 "decoding ExtensibleAttribute (%s) from SAML 2 Attribute (%s) with %lu value(s)",
113 ids.front().c_str(), n.get() ? n.get() : "unnamed", values.size()
118 const saml1::Attribute* saml1attr = dynamic_cast<const saml1::Attribute*>(xmlObject);
120 const vector<XMLObject*>& values = saml1attr->getAttributeValues();
123 if (log.isDebugEnabled()) {
124 auto_ptr_char n(saml1attr->getAttributeName());
126 "decoding ExtensibleAttribute (%s) from SAML 1 Attribute (%s) with %lu value(s)",
127 ids.front().c_str(), n.get() ? n.get() : "unnamed", values.size()
132 log.warn("XMLObject type not recognized by DOMAttributeDecoder, no values returned");
137 for (; v!=stop; ++v) {
138 DOMElement* e = (*v)->getDOM();
140 DDF converted = convert(e, false);
141 if (!converted.isnull())
145 log.warn("skipping AttributeValue without a backing DOM");
148 return dest.integer() ? _decode(attr.release()) : nullptr;
151 DDF DOMAttributeDecoder::convert(DOMElement* e, bool nameit) const
155 map<pair<xstring,xstring>,string>::const_iterator mapping;
156 DDF obj = DDF(nullptr).structure();
159 // Name this structure.
160 nsURI = e->getNamespaceURI();
161 local = e->getLocalName();
162 mapping = m_tagMap.find(pair<xstring,xstring>(local,nsURI));
163 if (mapping == m_tagMap.end()) {
164 auto_ptr_char temp(local);
165 obj.name(temp.get());
168 obj.name(mapping->second.c_str());
172 // Process non-xmlns attributes.
173 DOMNamedNodeMap* attrs = e->getAttributes();
174 for (XMLSize_t a = attrs->getLength(); a > 0; --a) {
175 DOMNode* attr = attrs->item(a-1);
176 nsURI = attr->getNamespaceURI();
177 if (XMLString::equals(nsURI, xmlconstants::XMLNS_NS))
179 local = attr->getLocalName();
180 mapping = m_tagMap.find(pair<xstring,xstring>(local, nsURI ? nsURI : &chNull));
181 if (mapping == m_tagMap.end()) {
182 auto_ptr_char temp(local);
183 obj.addmember(temp.get()).string(toUTF8(attr->getNodeValue(), true), false);
186 obj.addmember(mapping->second.c_str()).string(toUTF8(attr->getNodeValue(), true), false);
190 DOMElement* child = XMLHelper::getFirstChildElement(e);
191 if (!child && e->hasChildNodes() && e->getFirstChild()->getNodeType() == DOMNode::TEXT_NODE) {
192 // Attach a _text member if a text node is present.
193 obj.addmember("_string").string(toUTF8(e->getFirstChild()->getNodeValue(), true), false);
197 // Convert the child element.
198 DDF converted = convert(child);
199 if (!converted.isnull()) {
200 // Now identify it and attach it.
201 if (obj[converted.name()].isnull()) {
202 // We're a new child, so just attach as a structure member.
205 else if (obj[converted.name()].islist()) {
206 // We're already a repeating child, so add it to the list.
207 obj[converted.name()].add(converted);
209 else if (obj[converted.name()].isstruct()) {
210 // This is the complex case where we see a child for the second
211 // time and have to convert a structure member into a named list.
212 DDF newlist = DDF(converted.name()).list();
213 newlist.add(obj[converted.name()].remove());
214 newlist.add(converted);
218 child = XMLHelper::getNextSiblingElement(child);
222 // If we're empty, just delete.
223 if (obj.integer() == 0)