Imported Upstream version 2.2.1+dfsg
[shibboleth/sp.git] / shibsp / attribute / NameIDAttribute.h
1 /*
2  *  Copyright 2001-2009 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 derived from or mappable to a SAML NameID.
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          * @param formatter template for serialization of tuple
50          */
51         NameIDAttribute(const std::vector<std::string>& ids, const char* formatter=DEFAULT_NAMEID_FORMATTER)
52             : Attribute(ids), m_formatter(formatter) {
53         }
54
55         /**
56          * Constructs based on a remoted NameIDAttribute.
57          * 
58          * @param in    input object containing marshalled NameIDAttribute
59          */
60         NameIDAttribute(DDF& in) : Attribute(in) {
61             DDF val = in["_formatter"];
62             if (val.isstring())
63                 m_formatter = val.string();
64             else
65                 m_formatter = DEFAULT_NAMEID_FORMATTER;
66             const char* pch;
67             val = in.first().first();
68             while (val.name()) {
69                 m_values.push_back(Value());
70                 Value& v = m_values.back();
71                 v.m_Name = val.name();
72                 pch = val["Format"].string();
73                 if (pch)
74                     v.m_Format = pch;
75                 pch = val["NameQualifier"].string();
76                 if (pch)
77                     v.m_NameQualifier = pch;
78                 pch = val["SPNameQualifier"].string();
79                 if (pch)
80                     v.m_SPNameQualifier = pch;
81                 pch = val["SPProvidedID"].string();
82                 if (pch)
83                     v.m_SPProvidedID = pch;
84                 val = in.first().next();
85             }
86         }
87         
88         virtual ~NameIDAttribute() {}
89         
90         /**
91          * Holds all the fields associated with a NameID.
92          */
93         struct SHIBSP_API Value
94         {
95             std::string m_Name;
96             std::string m_Format;
97             std::string m_NameQualifier;
98             std::string m_SPNameQualifier;
99             std::string m_SPProvidedID;
100         };
101         
102         /**
103          * Returns the set of values encoded as UTF-8 strings.
104          * 
105          * @return  a mutable vector of the values
106          */
107         std::vector<Value>& getValues() {
108             return m_values;
109         }
110
111         /**
112          * Returns the set of values encoded as UTF-8 strings.
113          * 
114          * @return  an immutable vector of the values
115          */
116         const std::vector<Value>& getValues() const {
117             return m_values;
118         }
119
120         size_t valueCount() const {
121             return m_values.size();
122         }
123         
124         void clearSerializedValues() {
125             m_serialized.clear();
126         }
127
128         const char* getString(size_t index) const {
129             return m_values[index].m_Name.c_str();
130         }
131
132         const char* getScope(size_t index) const {
133             return m_values[index].m_NameQualifier.c_str();
134         }
135
136         void removeValue(size_t index) {
137             Attribute::removeValue(index);
138             if (index < m_values.size())
139                 m_values.erase(m_values.begin() + index);
140         }
141
142         const std::vector<std::string>& getSerializedValues() const {
143             if (m_serialized.empty()) {
144                 for (std::vector<Value>::const_iterator i=m_values.begin(); i!=m_values.end(); ++i) {
145                     // This is kind of a hack, but it's a good way to reuse some code.
146                     xmltooling::XMLToolingException e(
147                         m_formatter,
148                         xmltooling::namedparams(
149                             5,
150                             "Name", i->m_Name.c_str(),
151                             "Format", i->m_Format.c_str(),
152                             "NameQualifier", i->m_NameQualifier.c_str(),
153                             "SPNameQualifier", i->m_SPNameQualifier.c_str(),
154                             "SPProvidedID", i->m_SPProvidedID.c_str()
155                             )
156                         );
157                     m_serialized.push_back(e.what());
158                 }
159             }
160             return Attribute::getSerializedValues();
161         }
162     
163         DDF marshall() const {
164             DDF ddf = Attribute::marshall();
165             ddf.name("NameID");
166             ddf.addmember("_formatter").string(m_formatter.c_str());
167             DDF vlist = ddf.first();
168             for (std::vector<Value>::const_iterator i=m_values.begin(); i!=m_values.end(); ++i) {
169                 DDF val = DDF(i->m_Name.c_str()).structure();
170                 if (!i->m_Format.empty())
171                     val.addmember("Format").string(i->m_Format.c_str());
172                 if (!i->m_NameQualifier.empty())
173                     val.addmember("NameQualifier").string(i->m_NameQualifier.c_str());
174                 if (!i->m_SPNameQualifier.empty())
175                     val.addmember("SPNameQualifier").string(i->m_SPNameQualifier.c_str());
176                 if (!i->m_SPProvidedID.empty())
177                     val.addmember("SPProvidedID").string(i->m_SPProvidedID.c_str());
178                 vlist.add(val);
179             }
180             return ddf;
181         }
182     
183     private:
184         std::vector<Value> m_values;
185         std::string m_formatter;
186     };
187
188 #if defined (_MSC_VER)
189     #pragma warning( pop )
190 #endif
191
192 };
193
194 #endif /* __shibsp_nameidattr_h__ */