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;
60 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);
61 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);
62 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);
63 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);
64 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);
65 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);
66 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 caseSensitive[] = UNICODE_LITERAL_13(c,a,s,e,S,e,n,s,i,t,i,v,e);
69 static const XMLCh hashAlg[] = UNICODE_LITERAL_7(h,a,s,h,A,l,g);
70 static const XMLCh internal[] = UNICODE_LITERAL_8(i,n,t,e,r,n,a,l);
75 xmltooling::QName shibsp::StringAttributeDecoderType(shibspconstants::SHIB2ATTRIBUTEMAP_NS, _StringAttributeDecoder);
76 xmltooling::QName shibsp::ScopedAttributeDecoderType(shibspconstants::SHIB2ATTRIBUTEMAP_NS, _ScopedAttributeDecoder);
77 xmltooling::QName shibsp::NameIDAttributeDecoderType(shibspconstants::SHIB2ATTRIBUTEMAP_NS, _NameIDAttributeDecoder);
78 xmltooling::QName shibsp::NameIDFromScopedAttributeDecoderType(shibspconstants::SHIB2ATTRIBUTEMAP_NS, _NameIDFromScopedAttributeDecoder);
79 xmltooling::QName shibsp::KeyInfoAttributeDecoderType(shibspconstants::SHIB2ATTRIBUTEMAP_NS, _KeyInfoAttributeDecoder);
80 xmltooling::QName shibsp::DOMAttributeDecoderType(shibspconstants::SHIB2ATTRIBUTEMAP_NS, _DOMAttributeDecoder);
81 xmltooling::QName shibsp::XMLAttributeDecoderType(shibspconstants::SHIB2ATTRIBUTEMAP_NS, _XMLAttributeDecoder);
83 void shibsp::registerAttributeDecoders()
85 SPConfig& conf = SPConfig::getConfig();
86 conf.AttributeDecoderManager.registerFactory(StringAttributeDecoderType, StringAttributeDecoderFactory);
87 conf.AttributeDecoderManager.registerFactory(ScopedAttributeDecoderType, ScopedAttributeDecoderFactory);
88 conf.AttributeDecoderManager.registerFactory(NameIDAttributeDecoderType, NameIDAttributeDecoderFactory);
89 conf.AttributeDecoderManager.registerFactory(NameIDFromScopedAttributeDecoderType, NameIDFromScopedAttributeDecoderFactory);
90 conf.AttributeDecoderManager.registerFactory(KeyInfoAttributeDecoderType, KeyInfoAttributeDecoderFactory);
91 conf.AttributeDecoderManager.registerFactory(DOMAttributeDecoderType, DOMAttributeDecoderFactory);
92 conf.AttributeDecoderManager.registerFactory(XMLAttributeDecoderType, XMLAttributeDecoderFactory);
95 AttributeDecoder::AttributeDecoder(const DOMElement *e)
96 : m_caseSensitive(XMLHelper::getAttrBool(e, true, caseSensitive)),
97 m_internal(XMLHelper::getAttrBool(e, false, internal)),
98 m_hashAlg(XMLHelper::getAttrString(e, nullptr, hashAlg))
102 AttributeDecoder::~AttributeDecoder()
106 Attribute* AttributeDecoder::_decode(Attribute* attr) const
109 attr->setCaseSensitive(m_caseSensitive);
110 attr->setInternal(m_internal);
112 if (!m_hashAlg.empty()) {
113 // We turn the values into strings using the supplied hash algorithm and return a SimpleAttribute instead.
114 auto_ptr<SimpleAttribute> simple(new SimpleAttribute(attr->getAliases()));
115 simple->setCaseSensitive(false);
116 simple->setInternal(m_internal);
117 vector<string>& newdest = simple->getValues();
118 const vector<string>& serialized = attr->getSerializedValues();
119 for (vector<string>::const_iterator ser = serialized.begin(); ser != serialized.end(); ++ser) {
120 newdest.push_back(SecurityHelper::doHash(m_hashAlg.c_str(), ser->data(), ser->length()));
121 if (newdest.back().empty())
125 return newdest.empty() ? nullptr : simple.release();
133 void shibsp::registerAttributeFactories()
135 Attribute::registerFactory("", SimpleAttributeFactory);
136 Attribute::registerFactory("Simple", SimpleAttributeFactory);
137 Attribute::registerFactory("Scoped", ScopedAttributeFactory);
138 Attribute::registerFactory("NameID", NameIDAttributeFactory);
139 Attribute::registerFactory("Extensible", ExtensibleAttributeFactory);
140 Attribute::registerFactory("XML", XMLAttributeFactory);
143 map<string,Attribute::AttributeFactory*> Attribute::m_factoryMap;
145 void Attribute::registerFactory(const char* type, AttributeFactory* factory)
147 m_factoryMap[type] = factory;
150 void Attribute::deregisterFactory(const char* type)
152 m_factoryMap.erase(type);
155 void Attribute::deregisterFactories()
157 m_factoryMap.clear();
160 Attribute::Attribute(const vector<string>& ids) : m_id(ids), m_caseSensitive(true), m_internal(false)
164 Attribute::Attribute(DDF& in) : m_caseSensitive(in["case_insensitive"].isnull()), m_internal(!in["internal"].isnull())
166 const char* id = in.first().name();
170 throw AttributeException("No id found in marshalled attribute content.");
171 DDF aliases = in["aliases"];
172 if (aliases.islist()) {
173 DDF alias = aliases.first();
174 while (alias.isstring()) {
175 m_id.push_back(alias.string());
176 alias = aliases.next();
181 Attribute::~Attribute()
185 const char* Attribute::getId() const
187 return m_id.front().c_str();
190 const vector<string>& Attribute::getAliases() const
195 vector<string>& Attribute::getAliases()
200 void Attribute::setCaseSensitive(bool caseSensitive)
202 m_caseSensitive = caseSensitive;
205 void Attribute::setInternal(bool internal)
207 m_internal = internal;
210 bool Attribute::isCaseSensitive() const
212 return m_caseSensitive;
215 bool Attribute::isInternal() const
220 size_t Attribute::valueCount() const
222 return m_serialized.size();
225 const vector<string>& Attribute::getSerializedValues() const
230 const char* Attribute::getString(size_t index) const
232 return m_serialized[index].c_str();
235 const char* Attribute::getScope(size_t index) const
240 void Attribute::removeValue(size_t index)
242 if (index < m_serialized.size())
243 m_serialized.erase(m_serialized.begin() + index);
246 DDF Attribute::marshall() const
249 ddf.structure().addmember(m_id.front().c_str()).list();
250 if (!m_caseSensitive)
251 ddf.addmember("case_insensitive");
253 ddf.addmember("internal");
254 if (m_id.size() > 1) {
256 DDF aliases = ddf.addmember("aliases").list();
257 for (std::vector<std::string>::const_iterator a = m_id.begin() + 1; a != m_id.end(); ++a) {
258 alias = DDF(nullptr).string(a->c_str());
265 Attribute* Attribute::unmarshall(DDF& in)
267 map<string,AttributeFactory*>::const_iterator i = m_factoryMap.find(in.name() ? in.name() : "");
268 if (i == m_factoryMap.end())
269 throw AttributeException("No registered factory for Attribute of type ($1).", params(1,in.name()));
270 return (i->second)(in);