Imported Upstream version 2.2.1+dfsg
[shibboleth/sp.git] / shibsp / attribute / Attribute.cpp
1 /*
2  *  Copyright 2001-2009 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 "SPConfig.h"
25 #ifndef SHIBSP_LITE
26 # include "attribute/AttributeDecoder.h"
27 #endif
28 #include "attribute/SimpleAttribute.h"
29 #include "attribute/ScopedAttribute.h"
30 #include "attribute/NameIDAttribute.h"
31 #include "attribute/ExtensibleAttribute.h"
32 #include "attribute/XMLAttribute.h"
33 #include "util/SPConstants.h"
34
35 #include <xercesc/util/Base64.hpp>
36 #include <xercesc/util/XMLUniDefs.hpp>
37
38 using namespace shibsp;
39 using namespace xmltooling;
40 using namespace std;
41
42 namespace shibsp {
43
44     SHIBSP_DLLLOCAL Attribute* SimpleAttributeFactory(DDF& in) {
45         return new SimpleAttribute(in);
46     }
47
48     SHIBSP_DLLLOCAL Attribute* ScopedAttributeFactory(DDF& in) {
49         return new ScopedAttribute(in);
50     }
51
52     SHIBSP_DLLLOCAL Attribute* NameIDAttributeFactory(DDF& in) {
53         return new NameIDAttribute(in);
54     }
55
56     SHIBSP_DLLLOCAL Attribute* ExtensibleAttributeFactory(DDF& in) {
57         return new ExtensibleAttribute(in);
58     }
59
60     SHIBSP_DLLLOCAL Attribute* XMLAttributeFactory(DDF& in) {
61         return new XMLAttribute(in);
62     }
63
64 #ifndef SHIBSP_LITE
65     SHIBSP_DLLLOCAL PluginManager<AttributeDecoder,xmltooling::QName,const DOMElement*>::Factory StringAttributeDecoderFactory;
66     SHIBSP_DLLLOCAL PluginManager<AttributeDecoder,xmltooling::QName,const DOMElement*>::Factory ScopedAttributeDecoderFactory;
67     SHIBSP_DLLLOCAL PluginManager<AttributeDecoder,xmltooling::QName,const DOMElement*>::Factory NameIDAttributeDecoderFactory;
68     SHIBSP_DLLLOCAL PluginManager<AttributeDecoder,xmltooling::QName,const DOMElement*>::Factory NameIDFromScopedAttributeDecoderFactory;
69     SHIBSP_DLLLOCAL PluginManager<AttributeDecoder,xmltooling::QName,const DOMElement*>::Factory KeyInfoAttributeDecoderFactory;
70     SHIBSP_DLLLOCAL PluginManager<AttributeDecoder,xmltooling::QName,const DOMElement*>::Factory DOMAttributeDecoderFactory;
71     SHIBSP_DLLLOCAL PluginManager<AttributeDecoder,xmltooling::QName,const DOMElement*>::Factory XMLAttributeDecoderFactory;
72
73     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);
74     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);
75     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);
76     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);
77     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);
78     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);
79     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);
80
81     static const XMLCh caseSensitive[] =           UNICODE_LITERAL_13(c,a,s,e,S,e,n,s,i,t,i,v,e);
82     static const XMLCh internal[] =                UNICODE_LITERAL_8(i,n,t,e,r,n,a,l);
83 #endif
84 };
85
86 #ifndef SHIBSP_LITE
87 xmltooling::QName shibsp::StringAttributeDecoderType(shibspconstants::SHIB2ATTRIBUTEMAP_NS, _StringAttributeDecoder);
88 xmltooling::QName shibsp::ScopedAttributeDecoderType(shibspconstants::SHIB2ATTRIBUTEMAP_NS, _ScopedAttributeDecoder);
89 xmltooling::QName shibsp::NameIDAttributeDecoderType(shibspconstants::SHIB2ATTRIBUTEMAP_NS, _NameIDAttributeDecoder);
90 xmltooling::QName shibsp::NameIDFromScopedAttributeDecoderType(shibspconstants::SHIB2ATTRIBUTEMAP_NS, _NameIDFromScopedAttributeDecoder);
91 xmltooling::QName shibsp::KeyInfoAttributeDecoderType(shibspconstants::SHIB2ATTRIBUTEMAP_NS, _KeyInfoAttributeDecoder);
92 xmltooling::QName shibsp::DOMAttributeDecoderType(shibspconstants::SHIB2ATTRIBUTEMAP_NS, _DOMAttributeDecoder);
93 xmltooling::QName shibsp::XMLAttributeDecoderType(shibspconstants::SHIB2ATTRIBUTEMAP_NS, _XMLAttributeDecoder);
94
95 void shibsp::registerAttributeDecoders()
96 {
97     SPConfig& conf = SPConfig::getConfig();
98     conf.AttributeDecoderManager.registerFactory(StringAttributeDecoderType, StringAttributeDecoderFactory);
99     conf.AttributeDecoderManager.registerFactory(ScopedAttributeDecoderType, ScopedAttributeDecoderFactory);
100     conf.AttributeDecoderManager.registerFactory(NameIDAttributeDecoderType, NameIDAttributeDecoderFactory);
101     conf.AttributeDecoderManager.registerFactory(NameIDFromScopedAttributeDecoderType, NameIDFromScopedAttributeDecoderFactory);
102     conf.AttributeDecoderManager.registerFactory(KeyInfoAttributeDecoderType, KeyInfoAttributeDecoderFactory);
103     conf.AttributeDecoderManager.registerFactory(DOMAttributeDecoderType, DOMAttributeDecoderFactory);
104     conf.AttributeDecoderManager.registerFactory(XMLAttributeDecoderType, XMLAttributeDecoderFactory);
105 }
106
107 AttributeDecoder::AttributeDecoder(const DOMElement *e) : m_caseSensitive(true), m_internal(false)
108 {
109     if (e) {
110         const XMLCh* flag = e->getAttributeNS(NULL, caseSensitive);
111         if (flag && (*flag == chLatin_f || *flag == chDigit_0))
112             m_caseSensitive = false;
113
114         flag = e->getAttributeNS(NULL, internal);
115         if (flag && (*flag == chLatin_t || *flag == chDigit_1))
116             m_internal = true;
117     }
118 }
119 #endif
120
121 void shibsp::registerAttributeFactories()
122 {
123     Attribute::registerFactory("", SimpleAttributeFactory);
124     Attribute::registerFactory("Simple", SimpleAttributeFactory);
125     Attribute::registerFactory("Scoped", ScopedAttributeFactory);
126     Attribute::registerFactory("NameID", NameIDAttributeFactory);
127     Attribute::registerFactory("Extensible", ExtensibleAttributeFactory);
128     Attribute::registerFactory("XML", XMLAttributeFactory);
129 }
130
131 map<string,Attribute::AttributeFactory*> Attribute::m_factoryMap;
132
133 Attribute::Attribute(DDF& in) : m_caseSensitive(in["case_insensitive"].isnull()), m_internal(!in["internal"].isnull())
134 {
135     const char* id = in.first().name();
136     if (id && *id)
137         m_id.push_back(id);
138     else
139         throw AttributeException("No id found in marshalled attribute content.");
140     DDF aliases = in["aliases"];
141     if (aliases.islist()) {
142         DDF alias = aliases.first();
143         while (alias.isstring()) {
144             m_id.push_back(alias.string());
145             alias = aliases.next();
146         }
147     }
148 }
149
150 DDF Attribute::marshall() const
151 {
152     DDF ddf(NULL);
153     ddf.structure().addmember(m_id.front().c_str()).list();
154     if (!m_caseSensitive)
155         ddf.addmember("case_insensitive");
156     if (m_internal)
157         ddf.addmember("internal");
158     if (m_id.size() > 1) {
159         DDF alias;
160         DDF aliases = ddf.addmember("aliases").list();
161         for (std::vector<std::string>::const_iterator a = m_id.begin() + 1; a != m_id.end(); ++a) {
162             alias = DDF(NULL).string(a->c_str());
163             aliases.add(alias);
164         }
165     }
166     return ddf;
167 }
168
169 Attribute* Attribute::unmarshall(DDF& in)
170 {
171     map<string,AttributeFactory*>::const_iterator i = m_factoryMap.find(in.name() ? in.name() : "");
172     if (i == m_factoryMap.end())
173         throw AttributeException("No registered factory for Attribute of type ($1).", params(1,in.name()));
174     return (i->second)(in);
175 }
176
177 const vector<string>& XMLAttribute::getSerializedValues() const
178 {
179     xsecsize_t len;
180     XMLByte *pos, *pos2;\r
181     if (m_serialized.empty()) {
182         for (vector<string>::const_iterator i=m_values.begin(); i!=m_values.end(); ++i) {
183             XMLByte* enc = Base64::encode(reinterpret_cast<const XMLByte*>(i->data()), i->size(), &len);
184             if (enc) {
185                 for (pos=enc, pos2=enc; *pos2; pos2++)\r
186                     if (isgraph(*pos2))\r
187                         *pos++=*pos2;\r
188                 *pos=0;\r
189                 m_serialized.push_back(reinterpret_cast<char*>(enc));
190 #ifdef SHIBSP_XERCESC_HAS_XMLBYTE_RELEASE
191                 XMLString::release(&enc);
192 #else
193                 XMLString::release((char**)&enc);
194 #endif
195             }
196         }
197     }
198     return Attribute::getSerializedValues();
199 }