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.
24 * An Attribute whose values are relations of a value and a scope.
28 #include "attribute/ScopedAttribute.h"
30 using namespace shibsp;
34 SHIBSP_DLLLOCAL Attribute* ScopedAttributeFactory(DDF& in) {
35 return new ScopedAttribute(in);
39 ScopedAttribute::ScopedAttribute(const vector<string>& ids, char delimeter) : Attribute(ids), m_delimeter(delimeter)
43 ScopedAttribute::ScopedAttribute(DDF& in) : Attribute(in), m_delimeter('@')
45 DDF val = in["_delimeter"];
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
54 if (val.islist() && val.integer() == 2) {
55 m_values.push_back(make_pair(string(val.first().string()), string(val.last().string())));
57 else if (val.name() && val.string()) {
58 m_values.push_back(make_pair(string(val.name()), string(val.string())));
60 val = in.first().next();
64 ScopedAttribute::~ScopedAttribute()
68 vector< pair<string,string> >& ScopedAttribute::getValues()
73 const vector< pair<string,string> >& ScopedAttribute::getValues() const
78 size_t ScopedAttribute::valueCount() const
80 return m_values.size();
83 void ScopedAttribute::clearSerializedValues()
88 const char* ScopedAttribute::getString(size_t index) const
90 return m_values[index].first.c_str();
93 const char* ScopedAttribute::getScope(size_t index) const
95 return m_values[index].second.c_str();
98 void ScopedAttribute::removeValue(size_t index)
100 Attribute::removeValue(index);
101 if (index < m_values.size())
102 m_values.erase(m_values.begin() + index);
105 const vector<string>& ScopedAttribute::getSerializedValues() const
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);
111 return Attribute::getSerializedValues();
114 DDF ScopedAttribute::marshall() const
116 DDF ddf = Attribute::marshall();
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();