2 * Copyright 2001-2007 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 * @file shibsp/attribute/NameIDAttribute.h
20 * An Attribute whose values are relations of a value and a scope.
23 #ifndef __shibsp_nameidattr_h__
24 #define __shibsp_nameidattr_h__
26 #include <shibsp/attribute/Attribute.h>
27 #include <xmltooling/exceptions.h>
31 #if defined (_MSC_VER)
32 #pragma warning( push )
33 #pragma warning( disable : 4251 )
36 /** Default serialization format for NameIDs */
37 #define DEFAULT_NAMEID_FORMATTER "$Name!!$NameQualifier!!$SPNameQualifier"
40 * An Attribute whose values are derived from or mappable to a SAML NameID.
42 class SHIBSP_API NameIDAttribute : public Attribute
48 * @param ids array with primary identifier in first position, followed by any aliases
49 * @param formatter template for serialization of tuple
51 NameIDAttribute(const std::vector<std::string>& ids, const char* formatter=DEFAULT_NAMEID_FORMATTER)
52 : Attribute(ids), m_formatter(formatter) {
56 * Constructs based on a remoted NameIDAttribute.
58 * @param in input object containing marshalled NameIDAttribute
60 NameIDAttribute(DDF& in) : Attribute(in) {
61 DDF val = in["_formatter"];
63 m_formatter = val.string();
65 m_formatter = DEFAULT_NAMEID_FORMATTER;
67 val = in.first().first();
69 m_values.push_back(Value());
70 Value& v = m_values.back();
71 v.m_Name = val.name();
72 pch = val["Format"].string();
75 pch = val["NameQualifier"].string();
77 v.m_NameQualifier = pch;
78 pch = val["SPNameQualifier"].string();
80 v.m_SPNameQualifier = pch;
81 pch = val["SPProvidedID"].string();
83 v.m_SPProvidedID = pch;
84 val = in.first().next();
88 virtual ~NameIDAttribute() {}
91 * Holds all the fields associated with a NameID.
93 struct SHIBSP_API Value
97 std::string m_NameQualifier;
98 std::string m_SPNameQualifier;
99 std::string m_SPProvidedID;
103 * Returns the set of values encoded as UTF-8 strings.
105 * <p>Each compound value is a pair containing the simple value and the scope.
107 * @return a mutable vector of the values
109 std::vector<Value>& getValues() {
113 size_t valueCount() const {
114 return m_values.size();
117 void clearSerializedValues() {
118 m_serialized.clear();
121 const char* getString(size_t index) const {
122 return m_values[index].m_Name.c_str();
125 const char* getScope(size_t index) const {
126 return m_values[index].m_NameQualifier.c_str();
129 void removeValue(size_t index) {
130 Attribute::removeValue(index);
131 if (index < m_values.size())
132 m_values.erase(m_values.begin() + index);
135 const std::vector<std::string>& getSerializedValues() const {
136 if (m_serialized.empty()) {
137 for (std::vector<Value>::const_iterator i=m_values.begin(); i!=m_values.end(); ++i) {
138 // This is kind of a hack, but it's a good way to reuse some code.
139 xmltooling::XMLToolingException e(
141 xmltooling::namedparams(
143 "Name", i->m_Name.c_str(),
144 "Format", i->m_Format.c_str(),
145 "NameQualifier", i->m_NameQualifier.c_str(),
146 "SPNameQualifier", i->m_SPNameQualifier.c_str(),
147 "SPProvidedID", i->m_SPProvidedID.c_str()
150 m_serialized.push_back(e.what());
153 return Attribute::getSerializedValues();
156 DDF marshall() const {
157 DDF ddf = Attribute::marshall();
159 ddf.addmember("_formatter").string(m_formatter.c_str());
160 DDF vlist = ddf.first();
161 for (std::vector<Value>::const_iterator i=m_values.begin(); i!=m_values.end(); ++i) {
162 DDF val = DDF(i->m_Name.c_str()).structure();
163 if (!i->m_Format.empty())
164 val.addmember("Format").string(i->m_Format.c_str());
165 if (!i->m_NameQualifier.empty())
166 val.addmember("NameQualifier").string(i->m_NameQualifier.c_str());
167 if (!i->m_SPNameQualifier.empty())
168 val.addmember("SPNameQualifier").string(i->m_SPNameQualifier.c_str());
169 if (!i->m_SPProvidedID.empty())
170 val.addmember("SPProvidedID").string(i->m_SPProvidedID.c_str());
177 std::vector<Value> m_values;
178 std::string m_formatter;
181 #if defined (_MSC_VER)
182 #pragma warning( pop )
187 #endif /* __shibsp_nameidattr_h__ */