Add back alias support for attributes.
[shibboleth/sp.git] / shibsp / attribute / NameIDAttribute.h
1 /*
2  *  Copyright 2001-2007 Internet2
3  * 
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 /**
18  * @file shibsp/attribute/NameIDAttribute.h
19  * 
20  * An Attribute whose values are relations of a value and a scope.
21  */
22
23 #ifndef __shibsp_nameidattr_h__
24 #define __shibsp_nameidattr_h__
25
26 #include <shibsp/attribute/Attribute.h>
27 #include <xmltooling/exceptions.h>
28
29 namespace shibsp {
30
31 #if defined (_MSC_VER)
32     #pragma warning( push )
33     #pragma warning( disable : 4251 )
34 #endif
35
36     /** Default serialization format for NameIDs */
37     #define DEFAULT_NAMEID_FORMATTER    "$Name!!$NameQualifier!!$SPNameQualifier"
38
39     /**
40      * An Attribute whose values are derived from or mappable to a SAML NameID.
41      */
42     class SHIBSP_API NameIDAttribute : public Attribute
43     {
44     public:
45         /**
46          * Constructor.
47          * 
48          * @param ids   array with primary identifier in first position, followed by any aliases
49          */
50         NameIDAttribute(const std::vector<std::string>& ids, const char* formatter=DEFAULT_NAMEID_FORMATTER)
51             : Attribute(ids), m_formatter(formatter) {
52         }
53
54         /**
55          * Constructs based on a remoted NameIDAttribute.
56          * 
57          * @param in    input object containing marshalled NameIDAttribute
58          */
59         NameIDAttribute(DDF& in) : Attribute(in) {
60             DDF val = in["_formatter"];
61             if (val.isstring())
62                 m_formatter = val.string();
63             else
64                 m_formatter = DEFAULT_NAMEID_FORMATTER;
65             const char* pch;
66             val = in.first().first();
67             while (val.name()) {
68                 m_values.push_back(Value());
69                 Value& v = m_values.back();
70                 v.m_Name = val.name();
71                 pch = val["Format"].string();
72                 if (pch)
73                     v.m_Format = pch;
74                 pch = val["NameQualifier"].string();
75                 if (pch)
76                     v.m_NameQualifier = pch;
77                 pch = val["SPNameQualifier"].string();
78                 if (pch)
79                     v.m_SPNameQualifier = pch;
80                 pch = val["SPProvidedID"].string();
81                 if (pch)
82                     v.m_SPProvidedID = pch;
83                 val = in.first().next();
84             }
85         }
86         
87         virtual ~NameIDAttribute() {}
88         
89         /**
90          * Holds all the fields associated with a NameID.
91          */
92         struct SHIBSP_API Value
93         {
94             std::string m_Name;
95             std::string m_Format;
96             std::string m_NameQualifier;
97             std::string m_SPNameQualifier;
98             std::string m_SPProvidedID;
99         };
100         
101         /**
102          * Returns the set of values encoded as UTF-8 strings.
103          * 
104          * <p>Each compound value is a pair containing the simple value and the scope. 
105          * 
106          * @return  a mutable vector of the values
107          */
108         std::vector<Value>& getValues() {
109             return m_values;
110         }
111         
112         size_t valueCount() const {
113             return m_values.size();
114         }
115         
116         void clearSerializedValues() {
117             m_serialized.clear();
118         }
119
120         const char* getString(size_t index) const {
121             return m_values[index].m_Name.c_str();
122         }
123
124         const char* getScope(size_t index) const {
125             return m_values[index].m_NameQualifier.c_str();
126         }
127
128         void removeValue(size_t index) {
129             Attribute::removeValue(index);
130             if (index < m_values.size())
131                 m_values.erase(m_values.begin() + index);
132         }
133
134         const std::vector<std::string>& getSerializedValues() const {
135             if (m_serialized.empty()) {
136                 for (std::vector<Value>::const_iterator i=m_values.begin(); i!=m_values.end(); ++i) {
137                     // This is kind of a hack, but it's a good way to reuse some code.
138                     xmltooling::XMLToolingException e(
139                         m_formatter,
140                         xmltooling::namedparams(
141                             5,
142                             "Name", i->m_Name.c_str(),
143                             "Format", i->m_Format.c_str(),
144                             "NameQualifier", i->m_NameQualifier.c_str(),
145                             "SPNameQualifier", i->m_SPNameQualifier.c_str(),
146                             "SPProvidedID", i->m_SPProvidedID.c_str()
147                             )
148                         );
149                     m_serialized.push_back(e.what());
150                 }
151             }
152             return Attribute::getSerializedValues();
153         }
154     
155         DDF marshall() const {
156             DDF ddf = Attribute::marshall();
157             ddf.name("NameID");
158             ddf.addmember("_formatter").string(m_formatter.c_str());
159             DDF vlist = ddf.first();
160             for (std::vector<Value>::const_iterator i=m_values.begin(); i!=m_values.end(); ++i) {
161                 DDF val = DDF(i->m_Name.c_str()).structure();
162                 if (!i->m_Format.empty())
163                     val.addmember("Format").string(i->m_Format.c_str());
164                 if (!i->m_NameQualifier.empty())
165                     val.addmember("NameQualifier").string(i->m_NameQualifier.c_str());
166                 if (!i->m_SPNameQualifier.empty())
167                     val.addmember("SPNameQualifier").string(i->m_SPNameQualifier.c_str());
168                 if (!i->m_SPProvidedID.empty())
169                     val.addmember("SPProvidedID").string(i->m_SPProvidedID.c_str());
170                 vlist.add(val);
171             }
172             return ddf;
173         }
174     
175     private:
176         std::vector<Value> m_values;
177         std::string m_formatter;
178     };
179
180 #if defined (_MSC_VER)
181     #pragma warning( pop )
182 #endif
183
184 };
185
186 #endif /* __shibsp_nameidattr_h__ */