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