18a713700d220e856b6ce885befc9a02744dab07
[shibboleth/sp.git] / shibsp / attribute / NameIDAttribute.h
1 /*
2  *  Copyright 2001-2006 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     /** Default serialization format for NameIDs */
32     #define DEFAULT_NAMEID_FORMATTER    "$Name!!$NameQualifier!!$SPNameQualifier"
33
34     /**
35      * An Attribute whose values are derived from or mappable to a SAML NameID.
36      */
37     class SHIBSP_API NameIDAttribute : public Attribute
38     {
39     public:
40         /**
41          * Constructor
42          * 
43          * @param id    Attribute identifier
44          */
45         NameIDAttribute(const char* id, const char* formatter=DEFAULT_NAMEID_FORMATTER)
46             : Attribute(id), m_formatter(formatter) {
47         }
48         
49         virtual ~NameIDAttribute() {}
50         
51         /**
52          * Holds all the fields associated with a NameID.
53          */
54         struct SHIBSP_API Value
55         {
56             std::string m_Name;
57             std::string m_Format;
58             std::string m_NameQualifier;
59             std::string m_SPNameQualifier;
60             std::string m_SPProvidedID;
61         };
62         
63         /**
64          * Returns the set of values encoded as UTF-8 strings.
65          * 
66          * <p>Each compound value is a pair containing the simple value and the scope. 
67          * 
68          * @return  a mutable vector of the values
69          */
70         std::vector<Value>& getValues() {
71             return m_values;
72         }
73         
74         size_t valueCount() const {
75             return m_values.size();
76         }
77         
78         void clearSerializedValues() {
79             m_serialized.clear();
80         }
81         
82         const std::vector<std::string>& getSerializedValues() const {
83             if (m_serialized.empty()) {
84                 for (std::vector<Value>::const_iterator i=m_values.begin(); i!=m_values.end(); ++i) {
85                     // This is kind of a hack, but it's a good way to reuse some code.
86                     xmltooling::XMLToolingException e(
87                         m_formatter,
88                         xmltooling::namedparams(
89                             5,
90                             "Name", i->m_Name,
91                             "Format", i->m_Format,
92                             "NameQualifier", i->m_NameQualifier,
93                             "SPNameQualifier", i->m_SPNameQualifier,
94                             "SPProvidedID", i->m_SPProvidedID
95                             )
96                         );
97                     m_serialized.push_back(e.what());
98                 }
99             }
100             return Attribute::getSerializedValues();
101         }
102     
103         DDF marshall() const {
104             DDF ddf = Attribute::marshall();
105             ddf.name("NameIDAttribute");
106             DDF vlist = ddf.addmember("values").list();
107             for (std::vector<Value>::const_iterator i=m_values.begin(); i!=m_values.end(); ++i) {
108                 DDF val = DDF(NULL).structure();
109                 val.addmember("Name").string(i->m_Name.c_str());
110                 if (!i->m_Format.empty())
111                     val.addmember("Format").string(i->m_Format.c_str());
112                 if (!i->m_NameQualifier.empty())
113                     val.addmember("NameQualifier").string(i->m_NameQualifier.c_str());
114                 if (!i->m_SPNameQualifier.empty())
115                     val.addmember("SPNameQualifier").string(i->m_SPNameQualifier.c_str());
116                 if (!i->m_SPProvidedID.empty())
117                     val.addmember("SPProvidedID").string(i->m_SPProvidedID.c_str());
118                 vlist.add(val);
119             }
120             return ddf;
121         }
122     
123     private:
124         std::vector<Value> m_values;
125         std::string m_formatter;
126     };
127
128 };
129
130 #endif /* __shibsp_nameidattr_h__ */