2 * Copyright 2001-2010 Internet2
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * shibsp/attribute/Attribute.cpp
20 * A resolved attribute.
24 #include "exceptions.h"
27 # include "attribute/AttributeDecoder.h"
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"
36 #include <xercesc/util/XMLUniDefs.hpp>
37 #include <xmltooling/security/SecurityHelper.h>
38 #include <xmltooling/util/XMLHelper.h>
40 using namespace shibsp;
41 using namespace xmltooling;
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);
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;
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
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);
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);
90 void shibsp::registerAttributeDecoders()
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);
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))
110 AttributeDecoder::~AttributeDecoder()
114 Attribute* AttributeDecoder::_decode(Attribute* attr) const
117 attr->setCaseSensitive(m_caseSensitive);
118 attr->setInternal(m_internal);
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())
133 return newdest.empty() ? nullptr : simple.release();
141 void shibsp::registerAttributeFactories()
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);
151 map<string,Attribute::AttributeFactory*> Attribute::m_factoryMap;
153 void Attribute::registerFactory(const char* type, AttributeFactory* factory)
155 m_factoryMap[type] = factory;
158 void Attribute::deregisterFactory(const char* type)
160 m_factoryMap.erase(type);
163 void Attribute::deregisterFactories()
165 m_factoryMap.clear();
168 Attribute::Attribute(const vector<string>& ids) : m_id(ids), m_caseSensitive(true), m_internal(false)
172 Attribute::Attribute(DDF& in) : m_caseSensitive(in["case_insensitive"].isnull()), m_internal(!in["internal"].isnull())
174 const char* id = in.first().name();
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();
189 Attribute::~Attribute()
193 const char* Attribute::getId() const
195 return m_id.front().c_str();
198 const vector<string>& Attribute::getAliases() const
203 vector<string>& Attribute::getAliases()
208 void Attribute::setCaseSensitive(bool caseSensitive)
210 m_caseSensitive = caseSensitive;
213 void Attribute::setInternal(bool internal)
215 m_internal = internal;
218 bool Attribute::isCaseSensitive() const
220 return m_caseSensitive;
223 bool Attribute::isInternal() const
228 size_t Attribute::valueCount() const
230 return m_serialized.size();
233 const vector<string>& Attribute::getSerializedValues() const
238 const char* Attribute::getString(size_t index) const
240 return m_serialized[index].c_str();
243 const char* Attribute::getScope(size_t index) const
248 void Attribute::removeValue(size_t index)
250 if (index < m_serialized.size())
251 m_serialized.erase(m_serialized.begin() + index);
254 DDF Attribute::marshall() const
257 ddf.structure().addmember(m_id.front().c_str()).list();
258 if (!m_caseSensitive)
259 ddf.addmember("case_insensitive");
261 ddf.addmember("internal");
262 if (m_id.size() > 1) {
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());
273 Attribute* Attribute::unmarshall(DDF& in)
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);