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