SSPCPP-616 - clean up concatenated string literals
[shibboleth/cpp-sp.git] / shibsp / attribute / StringAttributeDecoder.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  * StringAttributeDecoder.cpp
23  *
24  * Decodes SAML into SimpleAttributes.
25  */
26
27 #include "internal.h"
28 #include "attribute/AttributeDecoder.h"
29 #include "attribute/SimpleAttribute.h"
30
31 #include <saml/saml1/core/Assertions.h>
32 #include <saml/saml2/core/Assertions.h>
33
34 using namespace shibsp;
35 using namespace opensaml::saml1;
36 using namespace opensaml::saml2;
37 using namespace xmltooling;
38 using namespace std;
39
40 namespace shibsp {
41     class SHIBSP_DLLLOCAL StringAttributeDecoder : virtual public AttributeDecoder
42     {
43     public:
44         StringAttributeDecoder(const DOMElement* e) : AttributeDecoder(e) {}
45         ~StringAttributeDecoder() {}
46
47         // deprecated method
48         shibsp::Attribute* decode(
49             const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty=nullptr, const char* relyingParty=nullptr
50             ) const {
51             return decode(nullptr, ids, xmlObject, assertingParty, relyingParty);
52         }
53
54         shibsp::Attribute* decode(
55             const GenericRequest*, const vector<string>&, const XMLObject*, const char* assertingParty=nullptr, const char* relyingParty=nullptr
56             ) const;
57     };
58
59     AttributeDecoder* SHIBSP_DLLLOCAL StringAttributeDecoderFactory(const DOMElement* const & e)
60     {
61         return new StringAttributeDecoder(e);
62     }
63 };
64
65 shibsp::Attribute* StringAttributeDecoder::decode(
66     const GenericRequest* request, const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty, const char* relyingParty
67     ) const
68 {
69     auto_ptr<SimpleAttribute> simple(new SimpleAttribute(ids));
70     vector<string>& dest = simple->getValues();
71     pair<vector<XMLObject*>::const_iterator,vector<XMLObject*>::const_iterator> valrange;
72
73     Category& log = Category::getInstance(SHIBSP_LOGCAT ".AttributeDecoder.String");
74
75     if (xmlObject && XMLString::equals(opensaml::saml1::Attribute::LOCAL_NAME,xmlObject->getElementQName().getLocalPart())) {
76         const opensaml::saml2::Attribute* saml2attr = dynamic_cast<const opensaml::saml2::Attribute*>(xmlObject);
77         if (saml2attr) {
78             const vector<XMLObject*>& values = saml2attr->getAttributeValues();
79             valrange = valueRange(request, values);
80             if (log.isDebugEnabled()) {
81                 auto_ptr_char n(saml2attr->getName());
82                 log.debug(
83                     "decoding SimpleAttribute (%s) from SAML 2 Attribute (%s) with %lu value(s)",
84                     ids.front().c_str(), n.get() ? n.get() : "unnamed", values.size()
85                     );
86             }
87         }
88         else {
89             const opensaml::saml1::Attribute* saml1attr = dynamic_cast<const opensaml::saml1::Attribute*>(xmlObject);
90             if (saml1attr) {
91                 const vector<XMLObject*>& values = saml1attr->getAttributeValues();
92                 valrange = valueRange(request, values);
93                 if (log.isDebugEnabled()) {
94                     auto_ptr_char n(saml1attr->getAttributeName());
95                 log.debug(
96                     "decoding SimpleAttribute (%s) from SAML 1 Attribute (%s) with %lu value(s)",
97                     ids.front().c_str(), n.get() ? n.get() : "unnamed", values.size()
98                     );
99                 }
100             }
101             else {
102                 log.warn("XMLObject type not recognized by StringAttributeDecoder, no values returned");
103                 return nullptr;
104             }
105         }
106
107         for (; valrange.first != valrange.second; ++valrange.first) {
108             if (!(*valrange.first)->hasChildren()) {
109                 auto_arrayptr<char> val(toUTF8((*valrange.first)->getTextContent()));
110                 if (val.get() && *val.get())
111                     dest.push_back(val.get());
112                 else
113                     log.warn("skipping empty AttributeValue");
114             }
115             else {
116                 log.warn("skipping complex AttributeValue");
117             }
118         }
119
120         return dest.empty() ? nullptr : _decode(simple.release());
121     }
122
123     const NameID* saml2name = dynamic_cast<const NameID*>(xmlObject);
124     if (saml2name) {
125         if (log.isDebugEnabled()) {
126             auto_ptr_char f(saml2name->getFormat());
127             log.debug("decoding SimpleAttribute (%s) from SAML 2 NameID with Format (%s)", ids.front().c_str(), f.get() ? f.get() : "unspecified");
128         }
129         auto_arrayptr<char> val(toUTF8(saml2name->getName()));
130         if (val.get() && *val.get())
131             dest.push_back(val.get());
132         else
133             log.warn("ignoring empty NameID");
134     }
135     else {
136         const NameIdentifier* saml1name = dynamic_cast<const NameIdentifier*>(xmlObject);
137         if (saml1name) {
138             if (log.isDebugEnabled()) {
139                 auto_ptr_char f(saml1name->getFormat());
140                 log.debug(
141                     "decoding SimpleAttribute (%s) from SAML 1 NameIdentifier with Format (%s)",
142                     ids.front().c_str(), f.get() ? f.get() : "unspecified"
143                     );
144             }
145             auto_arrayptr<char> val(toUTF8(saml1name->getName()));
146             if (val.get() && *val.get())
147                 dest.push_back(val.get());
148             else
149                 log.warn("ignoring empty NameIdentifier");
150         }
151         else {
152             log.warn("XMLObject type not recognized by StringAttributeDecoder, no values returned");
153             return nullptr;
154         }
155     }
156
157     return dest.empty() ? nullptr : _decode(simple.release());
158 }