VS10 solution files, convert from NULL macro to nullptr.
[shibboleth/sp.git] / shibsp / attribute / Attribute.cpp
1 /*
2  *  Copyright 2001-2010 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  * shibsp/attribute/Attribute.cpp
19  *
20  * A resolved attribute.
21  */
22
23 #include "internal.h"
24 #include "exceptions.h"
25 #include "SPConfig.h"
26 #ifndef SHIBSP_LITE
27 # include "attribute/AttributeDecoder.h"
28 #endif
29 #include "attribute/SimpleAttribute.h"
30 #include "attribute/ScopedAttribute.h"
31 #include "attribute/NameIDAttribute.h"
32 #include "attribute/ExtensibleAttribute.h"
33 #include "attribute/XMLAttribute.h"
34 #include "util/SPConstants.h"
35
36 #include <xercesc/util/XMLUniDefs.hpp>
37 #include <xmltooling/security/SecurityHelper.h>
38
39 using namespace shibsp;
40 using namespace xmltooling;
41 using namespace std;
42
43 namespace shibsp {
44     SHIBSP_DLLLOCAL Attribute* SimpleAttributeFactory(DDF& in);
45     SHIBSP_DLLLOCAL Attribute* ScopedAttributeFactory(DDF& in);
46     SHIBSP_DLLLOCAL Attribute* NameIDAttributeFactory(DDF& in);
47     SHIBSP_DLLLOCAL Attribute* ExtensibleAttributeFactory(DDF& in);
48     SHIBSP_DLLLOCAL Attribute* XMLAttributeFactory(DDF& in);
49
50 #ifndef SHIBSP_LITE
51     SHIBSP_DLLLOCAL PluginManager<AttributeDecoder,xmltooling::QName,const DOMElement*>::Factory StringAttributeDecoderFactory;
52     SHIBSP_DLLLOCAL PluginManager<AttributeDecoder,xmltooling::QName,const DOMElement*>::Factory ScopedAttributeDecoderFactory;
53     SHIBSP_DLLLOCAL PluginManager<AttributeDecoder,xmltooling::QName,const DOMElement*>::Factory NameIDAttributeDecoderFactory;
54     SHIBSP_DLLLOCAL PluginManager<AttributeDecoder,xmltooling::QName,const DOMElement*>::Factory NameIDFromScopedAttributeDecoderFactory;
55     SHIBSP_DLLLOCAL PluginManager<AttributeDecoder,xmltooling::QName,const DOMElement*>::Factory KeyInfoAttributeDecoderFactory;
56     SHIBSP_DLLLOCAL PluginManager<AttributeDecoder,xmltooling::QName,const DOMElement*>::Factory DOMAttributeDecoderFactory;
57     SHIBSP_DLLLOCAL PluginManager<AttributeDecoder,xmltooling::QName,const DOMElement*>::Factory XMLAttributeDecoderFactory;
58
59     static const XMLCh _StringAttributeDecoder[] = UNICODE_LITERAL_22(S,t,r,i,n,g,A,t,t,r,i,b,u,t,e,D,e,c,o,d,e,r);
60     static const XMLCh _ScopedAttributeDecoder[] = UNICODE_LITERAL_22(S,c,o,p,e,d,A,t,t,r,i,b,u,t,e,D,e,c,o,d,e,r);
61     static const XMLCh _NameIDAttributeDecoder[] = UNICODE_LITERAL_22(N,a,m,e,I,D,A,t,t,r,i,b,u,t,e,D,e,c,o,d,e,r);
62     static const XMLCh _NameIDFromScopedAttributeDecoder[] = UNICODE_LITERAL_32(N,a,m,e,I,D,F,r,o,m,S,c,o,p,e,d,A,t,t,r,i,b,u,t,e,D,e,c,o,d,e,r);
63     static const XMLCh _KeyInfoAttributeDecoder[] =UNICODE_LITERAL_23(K,e,y,I,n,f,o,A,t,t,r,i,b,u,t,e,D,e,c,o,d,e,r);
64     static const XMLCh _DOMAttributeDecoder[] =    UNICODE_LITERAL_19(D,O,M,A,t,t,r,i,b,u,t,e,D,e,c,o,d,e,r);
65     static const XMLCh _XMLAttributeDecoder[] =    UNICODE_LITERAL_19(X,M,L,A,t,t,r,i,b,u,t,e,D,e,c,o,d,e,r);
66
67     static const XMLCh caseSensitive[] =           UNICODE_LITERAL_13(c,a,s,e,S,e,n,s,i,t,i,v,e);
68     static const XMLCh hashAlg[] =                 UNICODE_LITERAL_7(h,a,s,h,A,l,g);
69     static const XMLCh internal[] =                UNICODE_LITERAL_8(i,n,t,e,r,n,a,l);
70 #endif
71 };
72
73 #ifndef SHIBSP_LITE
74 xmltooling::QName shibsp::StringAttributeDecoderType(shibspconstants::SHIB2ATTRIBUTEMAP_NS, _StringAttributeDecoder);
75 xmltooling::QName shibsp::ScopedAttributeDecoderType(shibspconstants::SHIB2ATTRIBUTEMAP_NS, _ScopedAttributeDecoder);
76 xmltooling::QName shibsp::NameIDAttributeDecoderType(shibspconstants::SHIB2ATTRIBUTEMAP_NS, _NameIDAttributeDecoder);
77 xmltooling::QName shibsp::NameIDFromScopedAttributeDecoderType(shibspconstants::SHIB2ATTRIBUTEMAP_NS, _NameIDFromScopedAttributeDecoder);
78 xmltooling::QName shibsp::KeyInfoAttributeDecoderType(shibspconstants::SHIB2ATTRIBUTEMAP_NS, _KeyInfoAttributeDecoder);
79 xmltooling::QName shibsp::DOMAttributeDecoderType(shibspconstants::SHIB2ATTRIBUTEMAP_NS, _DOMAttributeDecoder);
80 xmltooling::QName shibsp::XMLAttributeDecoderType(shibspconstants::SHIB2ATTRIBUTEMAP_NS, _XMLAttributeDecoder);
81
82 void shibsp::registerAttributeDecoders()
83 {
84     SPConfig& conf = SPConfig::getConfig();
85     conf.AttributeDecoderManager.registerFactory(StringAttributeDecoderType, StringAttributeDecoderFactory);
86     conf.AttributeDecoderManager.registerFactory(ScopedAttributeDecoderType, ScopedAttributeDecoderFactory);
87     conf.AttributeDecoderManager.registerFactory(NameIDAttributeDecoderType, NameIDAttributeDecoderFactory);
88     conf.AttributeDecoderManager.registerFactory(NameIDFromScopedAttributeDecoderType, NameIDFromScopedAttributeDecoderFactory);
89     conf.AttributeDecoderManager.registerFactory(KeyInfoAttributeDecoderType, KeyInfoAttributeDecoderFactory);
90     conf.AttributeDecoderManager.registerFactory(DOMAttributeDecoderType, DOMAttributeDecoderFactory);
91     conf.AttributeDecoderManager.registerFactory(XMLAttributeDecoderType, XMLAttributeDecoderFactory);
92 }
93
94 AttributeDecoder::AttributeDecoder(const DOMElement *e)
95     : m_caseSensitive(true), m_internal(false), m_hashAlg(e ? e->getAttributeNS(nullptr, hashAlg) : nullptr)
96 {
97     if (e) {
98         const XMLCh* flag = e->getAttributeNS(nullptr, caseSensitive);
99         if (flag && (*flag == chLatin_f || *flag == chDigit_0))
100             m_caseSensitive = false;
101
102         flag = e->getAttributeNS(nullptr, internal);
103         if (flag && (*flag == chLatin_t || *flag == chDigit_1))
104             m_internal = true;
105     }
106 }
107
108 AttributeDecoder::~AttributeDecoder()
109 {
110 }
111
112 Attribute* AttributeDecoder::_decode(Attribute* attr) const
113 {
114     if (attr) {
115         attr->setCaseSensitive(m_caseSensitive);
116         attr->setInternal(m_internal);
117
118         if (m_hashAlg.get() && *m_hashAlg.get()) {
119             // We turn the values into strings using the supplied hash algorithm and return a SimpleAttribute instead.
120             auto_ptr<SimpleAttribute> simple(new SimpleAttribute(attr->getAliases()));
121             simple->setCaseSensitive(false);
122             simple->setInternal(m_internal);
123             vector<string>& newdest = simple->getValues();
124             const vector<string>& serialized = attr->getSerializedValues();
125             for (vector<string>::const_iterator ser = serialized.begin(); ser != serialized.end(); ++ser) {
126                 newdest.push_back(SecurityHelper::doHash(m_hashAlg.get(), ser->data(), ser->length()));
127                 if (newdest.back().empty())
128                     newdest.pop_back();
129             }
130             delete attr;
131             return newdest.empty() ? nullptr : simple.release();
132         }
133
134     }
135     return attr;
136 }
137 #endif
138
139 void shibsp::registerAttributeFactories()
140 {
141     Attribute::registerFactory("", SimpleAttributeFactory);
142     Attribute::registerFactory("Simple", SimpleAttributeFactory);
143     Attribute::registerFactory("Scoped", ScopedAttributeFactory);
144     Attribute::registerFactory("NameID", NameIDAttributeFactory);
145     Attribute::registerFactory("Extensible", ExtensibleAttributeFactory);
146     Attribute::registerFactory("XML", XMLAttributeFactory);
147 }
148
149 map<string,Attribute::AttributeFactory*> Attribute::m_factoryMap;
150
151 void Attribute::registerFactory(const char* type, AttributeFactory* factory)
152 {
153     m_factoryMap[type] = factory;
154 }
155
156 void Attribute::deregisterFactory(const char* type)
157 {
158     m_factoryMap.erase(type);
159 }
160
161 void Attribute::deregisterFactories()
162 {
163     m_factoryMap.clear();
164 }
165
166 Attribute::Attribute(const vector<string>& ids) : m_id(ids), m_caseSensitive(true), m_internal(false)
167 {
168 }
169
170 Attribute::Attribute(DDF& in) : m_caseSensitive(in["case_insensitive"].isnull()), m_internal(!in["internal"].isnull())
171 {
172     const char* id = in.first().name();
173     if (id && *id)
174         m_id.push_back(id);
175     else
176         throw AttributeException("No id found in marshalled attribute content.");
177     DDF aliases = in["aliases"];
178     if (aliases.islist()) {
179         DDF alias = aliases.first();
180         while (alias.isstring()) {
181             m_id.push_back(alias.string());
182             alias = aliases.next();
183         }
184     }
185 }
186
187 Attribute::~Attribute()
188 {
189 }
190
191 const char* Attribute::getId() const
192 {
193     return m_id.front().c_str();
194 }
195
196 const vector<string>& Attribute::getAliases() const
197 {
198     return m_id;
199 }
200
201 vector<string>& Attribute::getAliases()
202 {
203     return m_id;
204 }
205
206 void Attribute::setCaseSensitive(bool caseSensitive)
207 {
208     m_caseSensitive = caseSensitive;
209 }
210
211 void Attribute::setInternal(bool internal)
212 {
213     m_internal = internal;
214 }
215
216 bool Attribute::isCaseSensitive() const
217 {
218     return m_caseSensitive;
219 }
220
221 bool Attribute::isInternal() const
222 {
223     return m_internal;
224 }
225
226 size_t Attribute::valueCount() const
227 {
228     return m_serialized.size();
229 }
230
231 const vector<string>& Attribute::getSerializedValues() const
232 {
233     return m_serialized;
234 }
235
236 const char* Attribute::getString(size_t index) const
237 {
238     return m_serialized[index].c_str();
239 }
240
241 const char* Attribute::getScope(size_t index) const
242 {
243     return nullptr;
244 }
245
246 void Attribute::removeValue(size_t index)
247 {
248     if (index < m_serialized.size())
249         m_serialized.erase(m_serialized.begin() + index);
250 }
251
252 DDF Attribute::marshall() const
253 {
254     DDF ddf(nullptr);
255     ddf.structure().addmember(m_id.front().c_str()).list();
256     if (!m_caseSensitive)
257         ddf.addmember("case_insensitive");
258     if (m_internal)
259         ddf.addmember("internal");
260     if (m_id.size() > 1) {
261         DDF alias;
262         DDF aliases = ddf.addmember("aliases").list();
263         for (std::vector<std::string>::const_iterator a = m_id.begin() + 1; a != m_id.end(); ++a) {
264             alias = DDF(nullptr).string(a->c_str());
265             aliases.add(alias);
266         }
267     }
268     return ddf;
269 }
270
271 Attribute* Attribute::unmarshall(DDF& in)
272 {
273     map<string,AttributeFactory*>::const_iterator i = m_factoryMap.find(in.name() ? in.name() : "");
274     if (i == m_factoryMap.end())
275         throw AttributeException("No registered factory for Attribute of type ($1).", params(1,in.name()));
276     return (i->second)(in);
277 }