SSPCPP-616 - fix tuple namespaces and string literal errors
[shibboleth/cpp-sp.git] / shibsp / attribute / ScopedAttribute.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  * ScopedAttribute.cpp
23  *
24  * An Attribute whose values are relations of a value and a scope.
25  */
26
27 #include "internal.h"
28 #include "attribute/ScopedAttribute.h"
29
30 using namespace shibsp;
31 using namespace std;
32
33 namespace shibsp {
34     SHIBSP_DLLLOCAL Attribute* ScopedAttributeFactory(DDF& in) {
35         return new ScopedAttribute(in);
36     }
37 };
38
39 ScopedAttribute::ScopedAttribute(const vector<string>& ids, char delimeter) : Attribute(ids), m_delimeter(delimeter)
40 {
41 }
42
43 ScopedAttribute::ScopedAttribute(DDF& in) : Attribute(in), m_delimeter('@')
44 {
45     DDF val = in["_delimeter"];
46     if (val.isint())
47         m_delimeter = static_cast<char>(val.integer());
48     val = in.first().first();
49     while (!val.isnull()) {
50         // There are two serializations supported. The new one is in 2.5.1 and fixes SPPCPP-504.
51         // The original is the second branch and was vulnerable to non-ASCII characters in the value.
52         // Supporting both means at least minimal support for rolling upgrades if a shibd instance is
53         // shared.
54         if (val.islist() && val.integer() == 2) {
55             m_values.push_back(make_pair(string(val.first().string()), string(val.last().string())));
56         }
57         else if (val.name() && val.string()) {
58             m_values.push_back(make_pair(string(val.name()), string(val.string())));
59         }
60         val = in.first().next();
61     }
62 }
63
64 ScopedAttribute::~ScopedAttribute()
65 {
66 }
67
68 vector< pair<string,string> >& ScopedAttribute::getValues()
69 {
70     return m_values;
71 }
72
73 const vector< pair<string,string> >& ScopedAttribute::getValues() const
74 {
75     return m_values;
76 }
77
78 size_t ScopedAttribute::valueCount() const
79 {
80     return m_values.size();
81 }
82
83 void ScopedAttribute::clearSerializedValues()
84 {
85     m_serialized.clear();
86 }
87
88 const char* ScopedAttribute::getString(size_t index) const
89 {
90     return m_values[index].first.c_str();
91 }
92
93 const char* ScopedAttribute::getScope(size_t index) const
94 {
95     return m_values[index].second.c_str();
96 }
97
98 void ScopedAttribute::removeValue(size_t index)
99 {
100     Attribute::removeValue(index);
101     if (index < m_values.size())
102         m_values.erase(m_values.begin() + index);
103 }
104
105 const vector<string>& ScopedAttribute::getSerializedValues() const
106 {
107     if (m_serialized.empty()) {
108         for (vector< pair<string,string> >::const_iterator i=m_values.begin(); i!=m_values.end(); ++i)
109             m_serialized.push_back(i->first + m_delimeter + i->second);
110     }
111     return Attribute::getSerializedValues();
112 }
113
114 DDF ScopedAttribute::marshall() const
115 {
116     DDF ddf = Attribute::marshall();
117     ddf.name("Scoped");
118     if (m_delimeter != '@')
119         ddf.addmember("_delimeter").integer(m_delimeter);
120     DDF vlist = ddf.first();
121     for (vector< pair<string,string> >::const_iterator i=m_values.begin(); i!=m_values.end(); ++i) {
122         DDF one = DDF(nullptr).string(i->first.c_str());
123         DDF two = DDF(nullptr).string(i->second.c_str());
124         DDF val = DDF(nullptr).list();
125         val.add(one);
126         val.add(two);
127         vlist.add(val);
128     }
129     return ddf;
130 }