Change license header.
[shibboleth/cpp-sp.git] / shibsp / attribute / DOMAttributeDecoder.cpp
1 /**
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.
6  *
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
10  * License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  */
20
21 /**
22  * DOMAttributeDecoder.cpp
23  *
24  * Decodes a DOM into an ExtensibleAttribute.
25  */
26
27 #include "internal.h"
28 #include "attribute/AttributeDecoder.h"
29 #include "attribute/ExtensibleAttribute.h"
30
31 #include <saml/saml1/core/Assertions.h>
32 #include <saml/saml2/core/Assertions.h>
33 #include <xmltooling/util/XMLHelper.h>
34
35 using namespace shibsp;
36 using namespace opensaml;
37 using namespace xmltooling;
38 using namespace std;
39
40 namespace shibsp {
41     class SHIBSP_DLLLOCAL DOMAttributeDecoder : virtual public AttributeDecoder
42     {
43     public:
44         DOMAttributeDecoder(const DOMElement* e);
45         ~DOMAttributeDecoder() {}
46
47         Attribute* decode(
48             const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty=nullptr, const char* relyingParty=nullptr
49             ) const;
50
51     private:
52         DDF convert(DOMElement* e, bool nameit=true) const;
53         string m_formatter;
54         map<pair<xstring,xstring>,string> m_tagMap;
55     };
56
57     AttributeDecoder* SHIBSP_DLLLOCAL DOMAttributeDecoderFactory(const DOMElement* const & e)
58     {
59         return new DOMAttributeDecoder(e);
60     }
61
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);
66 };
67
68 DOMAttributeDecoder::DOMAttributeDecoder(const DOMElement* e)
69     : AttributeDecoder(e), m_formatter(XMLHelper::getAttrString(e, nullptr, formatter))
70 {
71     Category& log = Category::getInstance(SHIBSP_LOGCAT".AttributeDecoder.DOM");
72
73     e = XMLHelper::getFirstChildElement(e, Mapping);
74     while (e) {
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());
81                 m_tagMap.insert(
82                     pair< const pair<xstring,xstring>,string>(
83                         pair<xstring,xstring>(f->getLocalPart(), f->hasNamespaceURI() ? f->getNamespaceURI() : &chNull),
84                         t.get()
85                         )
86                     );
87             }
88         }
89         e = XMLHelper::getNextSiblingElement(e, Mapping);
90     }
91 }
92
93 Attribute* DOMAttributeDecoder::decode(
94     const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty, const char* relyingParty
95     ) const
96 {
97     Category& log = Category::getInstance(SHIBSP_LOGCAT".AttributeDecoder.DOM");
98
99     if (!xmlObject || !XMLString::equals(saml1::Attribute::LOCAL_NAME, xmlObject->getElementQName().getLocalPart())) {
100         log.warn("XMLObject type not recognized by DOMAttributeDecoder, no values returned");
101         return nullptr;
102     }
103
104     auto_ptr<ExtensibleAttribute> attr(new ExtensibleAttribute(ids, m_formatter.c_str()));
105     DDF dest = attr->getValues();
106     vector<XMLObject*>::const_iterator v,stop;
107
108     const saml2::Attribute* saml2attr = dynamic_cast<const saml2::Attribute*>(xmlObject);
109     if (saml2attr) {
110         const vector<XMLObject*>& values = saml2attr->getAttributeValues();
111         v = values.begin();
112         stop = values.end();
113         if (log.isDebugEnabled()) {
114             auto_ptr_char n(saml2attr->getName());
115             log.debug(
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()
118                 );
119         }
120     }
121     else {
122         const saml1::Attribute* saml1attr = dynamic_cast<const saml1::Attribute*>(xmlObject);
123         if (saml1attr) {
124             const vector<XMLObject*>& values = saml1attr->getAttributeValues();
125             v = values.begin();
126             stop = values.end();
127             if (log.isDebugEnabled()) {
128                 auto_ptr_char n(saml1attr->getAttributeName());
129                 log.debug(
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()
132                     );
133             }
134         }
135         else {
136             log.warn("XMLObject type not recognized by DOMAttributeDecoder, no values returned");
137             return nullptr;
138         }
139     }
140
141     for (; v!=stop; ++v) {
142         DOMElement* e = (*v)->getDOM();
143         if (e) {
144             DDF converted = convert(e, false);
145             if (!converted.isnull())
146                 dest.add(converted);
147         }
148         else
149             log.warn("skipping AttributeValue without a backing DOM");
150     }
151
152     return dest.integer() ? _decode(attr.release()) : nullptr;
153 }
154
155 DDF DOMAttributeDecoder::convert(DOMElement* e, bool nameit) const
156 {
157     const XMLCh* nsURI;
158     const XMLCh* local;
159     map<pair<xstring,xstring>,string>::const_iterator mapping;
160     DDF obj = DDF(nullptr).structure();
161
162     if (nameit) {
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());
170         }
171         else {
172             obj.name(mapping->second.c_str());
173         }
174     }
175
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))
182             continue;
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);
188         }
189         else {
190             obj.addmember(mapping->second.c_str()).string(toUTF8(attr->getNodeValue(), true), false);
191         }
192     }
193
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);
198     }
199     else {
200         while (child) {
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.
207                     obj.add(converted);
208                 }
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);
212                 }
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);
219                     obj.add(newlist);
220                 }
221             }
222             child = XMLHelper::getNextSiblingElement(child);
223         }
224     }
225
226     // If we're empty, just delete.
227     if (obj.integer() == 0)
228         obj.destroy();
229     return obj;
230 }