https://issues.shibboleth.net/jira/browse/SSPCPP-472
[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 "ServiceProvider.h"
29 #include "attribute/NameIDAttribute.h"
30 #include "remoting/ListenerService.h"
31
32 #include <boost/algorithm/string/trim.hpp>
33 #include <xmltooling/exceptions.h>
34 #include <xmltooling/security/SecurityHelper.h>
35
36 using namespace shibsp;
37 using namespace xmltooling::logging;
38 using namespace xmltooling;
39 using namespace std;
40
41 namespace shibsp {
42     SHIBSP_DLLLOCAL Attribute* NameIDAttributeFactory(DDF& in) {
43         return new NameIDAttribute(in);
44     }
45 };
46
47 NameIDAttribute::NameIDAttribute(const vector<string>& ids, const char* formatter, const char* hashAlg)
48     : Attribute(ids), m_formatter(formatter), m_hashAlg(hashAlg ? hashAlg : "")
49 {
50 }
51
52 NameIDAttribute::NameIDAttribute(DDF& in) : Attribute(in)
53 {
54     DDF val = in["_formatter"];
55     if (val.isstring() && val.string())
56         m_formatter = val.string();
57     else
58         m_formatter = DEFAULT_NAMEID_FORMATTER;
59     val = in["_hashalg"];
60     if (val.isstring() && val.string())
61         m_hashAlg = val.string();
62     const char* pch;
63     val = in.first().first();
64     while (val.name()) {
65         m_values.push_back(Value());
66         Value& v = m_values.back();
67         v.m_Name = val.name();
68         pch = val["Format"].string();
69         if (pch)
70             v.m_Format = pch;
71         pch = val["NameQualifier"].string();
72         if (pch)
73             v.m_NameQualifier = pch;
74         pch = val["SPNameQualifier"].string();
75         if (pch)
76             v.m_SPNameQualifier = pch;
77         pch = val["SPProvidedID"].string();
78         if (pch)
79             v.m_SPProvidedID = pch;
80         val = in.first().next();
81     }
82 }
83
84 NameIDAttribute::~NameIDAttribute()
85 {
86 }
87
88 vector<NameIDAttribute::Value>& NameIDAttribute::getValues()
89 {
90     return m_values;
91 }
92
93 const vector<NameIDAttribute::Value>& NameIDAttribute::getValues() const
94 {
95     return m_values;
96 }
97
98 size_t NameIDAttribute::valueCount() const
99 {
100     return m_values.size();
101 }
102
103 void NameIDAttribute::clearSerializedValues()
104 {
105     m_serialized.clear();
106 }
107
108 const char* NameIDAttribute::getString(size_t index) const
109 {
110     return m_values[index].m_Name.c_str();
111 }
112
113 const char* NameIDAttribute::getScope(size_t index) const
114 {
115     return m_values[index].m_NameQualifier.c_str();
116 }
117
118 void NameIDAttribute::removeValue(size_t index)
119 {
120     Attribute::removeValue(index);
121     if (index < m_values.size())
122         m_values.erase(m_values.begin() + index);
123 }
124
125 const vector<string>& NameIDAttribute::getSerializedValues() const
126 {
127     if (m_serialized.empty()) {
128         for (vector<Value>::const_iterator i = m_values.begin(); i != m_values.end(); ++i) {
129             // This is kind of a hack, but it's a good way to reuse some code.
130             XMLToolingException e(
131                 m_formatter,
132                 namedparams(
133                     5,
134                     "Name", i->m_Name.c_str(),
135                     "Format", i->m_Format.c_str(),
136                     "NameQualifier", i->m_NameQualifier.c_str(),
137                     "SPNameQualifier", i->m_SPNameQualifier.c_str(),
138                     "SPProvidedID", i->m_SPProvidedID.c_str()
139                     )
140                 );
141             if (m_hashAlg.empty()) {
142                 m_serialized.push_back(e.what());
143                 boost::trim(m_serialized.back());
144             }
145             else {
146                 string trimmed(e.what());
147                 boost::trim(trimmed);
148 #ifndef SHIBSP_LITE
149                 m_serialized.push_back(SecurityHelper::doHash(m_hashAlg.c_str(), trimmed.c_str(), strlen(e.what())));
150 #else
151                 try {
152                     DDF out, in("hash");
153                     DDFJanitor jin(in), jout(out);
154                     in.addmember("alg").string(m_hashAlg.c_str());
155                     in.addmember("data").unsafe_string(trimmed.c_str());
156                     out = SPConfig::getConfig().getServiceProvider()->getListenerService()->send(in);
157                     if (out.isstring() && out.string())
158                         m_serialized.push_back(out.string());
159                 }
160                 catch (exception& ex) {
161                     Category::getInstance(SHIBSP_LOGCAT".Attribute.NameID").error("exception remoting hash operation: %s", ex.what());
162                 }
163 #endif
164             }
165         }
166     }
167     return Attribute::getSerializedValues();
168 }
169
170 DDF NameIDAttribute::marshall() const
171 {
172     DDF ddf = Attribute::marshall();
173     ddf.name("NameID");
174     ddf.addmember("_formatter").string(m_formatter.c_str());
175     if (!m_hashAlg.empty())
176         ddf.addmember("_hashalg").string(m_hashAlg.c_str());
177     DDF vlist = ddf.first();
178     for (vector<Value>::const_iterator i=m_values.begin(); i!=m_values.end(); ++i) {
179         DDF val = DDF(i->m_Name.c_str()).structure();
180         if (!i->m_Format.empty())
181             val.addmember("Format").string(i->m_Format.c_str());
182         if (!i->m_NameQualifier.empty())
183             val.addmember("NameQualifier").string(i->m_NameQualifier.c_str());
184         if (!i->m_SPNameQualifier.empty())
185             val.addmember("SPNameQualifier").string(i->m_SPNameQualifier.c_str());
186         if (!i->m_SPProvidedID.empty())
187             val.addmember("SPProvidedID").string(i->m_SPProvidedID.c_str());
188         vlist.add(val);
189     }
190     return ddf;
191 }