9cc59dbf242d8f2f1627a0166a1de0a349cc53ab
[shibboleth/sp.git] / shibsp / attribute / NameIDAttribute.cpp
1 /**
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.
6  *
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
10  * License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  */
20
21 /**
22  * NameIDAttribute.cpp
23  *
24  * An Attribute whose values are derived from or mappable to a SAML NameID.
25  */
26
27 #include "internal.h"
28 #include "attribute/NameIDAttribute.h"
29
30 #include <xmltooling/exceptions.h>
31
32 using namespace shibsp;
33 using namespace xmltooling;
34 using namespace std;
35
36 namespace shibsp {
37     SHIBSP_DLLLOCAL Attribute* NameIDAttributeFactory(DDF& in) {
38         return new NameIDAttribute(in);
39     }
40 };
41
42 NameIDAttribute::NameIDAttribute(const vector<string>& ids, const char* formatter) : Attribute(ids), m_formatter(formatter)
43 {
44 }
45
46 NameIDAttribute::NameIDAttribute(DDF& in) : Attribute(in)
47 {
48     DDF val = in["_formatter"];
49     if (val.isstring())
50         m_formatter = val.string();
51     else
52         m_formatter = DEFAULT_NAMEID_FORMATTER;
53     const char* pch;
54     val = in.first().first();
55     while (val.name()) {
56         m_values.push_back(Value());
57         Value& v = m_values.back();
58         v.m_Name = val.name();
59         pch = val["Format"].string();
60         if (pch)
61             v.m_Format = pch;
62         pch = val["NameQualifier"].string();
63         if (pch)
64             v.m_NameQualifier = pch;
65         pch = val["SPNameQualifier"].string();
66         if (pch)
67             v.m_SPNameQualifier = pch;
68         pch = val["SPProvidedID"].string();
69         if (pch)
70             v.m_SPProvidedID = pch;
71         val = in.first().next();
72     }
73 }
74
75 NameIDAttribute::~NameIDAttribute()
76 {
77 }
78
79 vector<NameIDAttribute::Value>& NameIDAttribute::getValues()
80 {
81     return m_values;
82 }
83
84 const vector<NameIDAttribute::Value>& NameIDAttribute::getValues() const
85 {
86     return m_values;
87 }
88
89 size_t NameIDAttribute::valueCount() const
90 {
91     return m_values.size();
92 }
93
94 void NameIDAttribute::clearSerializedValues()
95 {
96     m_serialized.clear();
97 }
98
99 const char* NameIDAttribute::getString(size_t index) const
100 {
101     return m_values[index].m_Name.c_str();
102 }
103
104 const char* NameIDAttribute::getScope(size_t index) const
105 {
106     return m_values[index].m_NameQualifier.c_str();
107 }
108
109 void NameIDAttribute::removeValue(size_t index)
110 {
111     Attribute::removeValue(index);
112     if (index < m_values.size())
113         m_values.erase(m_values.begin() + index);
114 }
115
116 const vector<string>& NameIDAttribute::getSerializedValues() const
117 {
118     if (m_serialized.empty()) {
119         for (vector<Value>::const_iterator i=m_values.begin(); i!=m_values.end(); ++i) {
120             // This is kind of a hack, but it's a good way to reuse some code.
121             XMLToolingException e(
122                 m_formatter,
123                 namedparams(
124                     5,
125                     "Name", i->m_Name.c_str(),
126                     "Format", i->m_Format.c_str(),
127                     "NameQualifier", i->m_NameQualifier.c_str(),
128                     "SPNameQualifier", i->m_SPNameQualifier.c_str(),
129                     "SPProvidedID", i->m_SPProvidedID.c_str()
130                     )
131                 );
132             m_serialized.push_back(e.what());
133         }
134     }
135     return Attribute::getSerializedValues();
136 }
137
138 DDF NameIDAttribute::marshall() const
139 {
140     DDF ddf = Attribute::marshall();
141     ddf.name("NameID");
142     ddf.addmember("_formatter").string(m_formatter.c_str());
143     DDF vlist = ddf.first();
144     for (vector<Value>::const_iterator i=m_values.begin(); i!=m_values.end(); ++i) {
145         DDF val = DDF(i->m_Name.c_str()).structure();
146         if (!i->m_Format.empty())
147             val.addmember("Format").string(i->m_Format.c_str());
148         if (!i->m_NameQualifier.empty())
149             val.addmember("NameQualifier").string(i->m_NameQualifier.c_str());
150         if (!i->m_SPNameQualifier.empty())
151             val.addmember("SPNameQualifier").string(i->m_SPNameQualifier.c_str());
152         if (!i->m_SPProvidedID.empty())
153             val.addmember("SPProvidedID").string(i->m_SPProvidedID.c_str());
154         vlist.add(val);
155     }
156     return ddf;
157 }