Simplify initialization.
[shibboleth/cpp-sp.git] / shibsp / attribute / ScopedAttributeDecoder.cpp
1 /*
2  *  Copyright 2001-2007 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  * ScopedAttributeDecoder.cpp
19  *
20  * Decodes SAML into ScopedAttributes
21  */
22
23 #include "internal.h"
24 #include "attribute/AttributeDecoder.h"
25 #include "attribute/ScopedAttribute.h"
26
27 #include <saml/saml1/core/Assertions.h>
28 #include <saml/saml2/core/Assertions.h>
29
30 using namespace shibsp;
31 using namespace opensaml::saml1;
32 using namespace opensaml::saml2;
33 using namespace xmltooling;
34 using namespace std;
35
36 namespace shibsp {
37     static const XMLCh Scope[] =            UNICODE_LITERAL_5(S,c,o,p,e);
38     static const XMLCh scopeDelimeter[] =   UNICODE_LITERAL_14(s,c,o,p,e,D,e,l,i,m,e,t,e,r);
39
40     class SHIBSP_DLLLOCAL ScopedAttributeDecoder : virtual public AttributeDecoder
41     {
42     public:
43         ScopedAttributeDecoder(const DOMElement* e) : AttributeDecoder(e), m_delimeter('@') {
44             if (e && e->hasAttributeNS(NULL,scopeDelimeter)) {
45                 auto_ptr_char d(e->getAttributeNS(NULL,scopeDelimeter));
46                 m_delimeter = *(d.get());
47             }
48         }
49         ~ScopedAttributeDecoder() {}
50
51         shibsp::Attribute* decode(
52             const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty=NULL, const char* relyingParty=NULL
53             ) const;
54
55     private:
56         char m_delimeter;
57     };
58
59     AttributeDecoder* SHIBSP_DLLLOCAL ScopedAttributeDecoderFactory(const DOMElement* const & e)
60     {
61         return new ScopedAttributeDecoder(e);
62     }
63 };
64
65 shibsp::Attribute* ScopedAttributeDecoder::decode(
66     const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty, const char* relyingParty
67     ) const
68 {
69     char* val;
70     char* scope;
71     const XMLCh* xmlscope;
72     QName scopeqname(NULL,Scope);
73     auto_ptr<ScopedAttribute> scoped(new ScopedAttribute(ids, m_delimeter));
74     scoped->setCaseSensitive(m_caseSensitive);
75     vector< pair<string,string> >& dest = scoped->getValues();
76     vector<XMLObject*>::const_iterator v,stop;
77
78     Category& log = Category::getInstance(SHIBSP_LOGCAT".AttributeDecoder");
79
80     if (xmlObject && XMLString::equals(opensaml::saml1::Attribute::LOCAL_NAME,xmlObject->getElementQName().getLocalPart())) {
81         const opensaml::saml2::Attribute* saml2attr = dynamic_cast<const opensaml::saml2::Attribute*>(xmlObject);
82         if (saml2attr) {
83             const vector<XMLObject*>& values = saml2attr->getAttributeValues();
84             v = values.begin();
85             stop = values.end();
86             if (log.isDebugEnabled()) {
87                 auto_ptr_char n(saml2attr->getName());
88                 log.debug(
89                     "decoding ScopedAttribute (%s) from SAML 2 Attribute (%s) with %lu value(s)",
90                     ids.front().c_str(), n.get() ? n.get() : "unnamed", values.size()
91                     );
92             }
93         }
94         else {
95             const opensaml::saml1::Attribute* saml1attr = dynamic_cast<const opensaml::saml1::Attribute*>(xmlObject);
96             if (saml1attr) {
97                 const vector<XMLObject*>& values = saml1attr->getAttributeValues();
98                 v = values.begin();
99                 stop = values.end();
100                 if (log.isDebugEnabled()) {
101                     auto_ptr_char n(saml1attr->getAttributeName());
102                     log.debug(
103                         "decoding ScopedAttribute (%s) from SAML 1 Attribute (%s) with %lu value(s)",
104                         ids.front().c_str(), n.get() ? n.get() : "unnamed", values.size()
105                         );
106                 }
107             }
108             else {
109                 log.warn("XMLObject type not recognized by ScopedAttributeDecoder, no values returned");
110                 return NULL;
111             }
112         }
113
114         for (; v!=stop; ++v) {
115             if (!(*v)->hasChildren()) {
116                 val = toUTF8((*v)->getTextContent());
117                 if (val && *val) {
118                     const AttributeExtensibleXMLObject* aexo=dynamic_cast<const AttributeExtensibleXMLObject*>(*v);
119                     xmlscope = aexo->getAttribute(scopeqname);
120                     if (xmlscope && *xmlscope) {
121                         scope = toUTF8(xmlscope);
122                         dest.push_back(pair<string,string>(val,scope));
123                         delete[] scope;
124                     }
125                     else {
126                         scope = strchr(val, m_delimeter);
127                         if (scope) {
128                             *scope++ = 0;
129                             if (*scope)
130                                 dest.push_back(pair<string,string>(val,scope));
131                             else
132                                 log.warn("ignoring unscoped AttributeValue");
133                         }
134                         else {
135                             log.warn("ignoring unscoped AttributeValue");
136                         }
137                     }
138                 }
139                 else {
140                     log.warn("skipping empty AttributeValue");
141                 }
142                 delete[] val;
143             }
144             else {
145                 log.warn("skipping complex AttributeValue");
146             }
147         }
148
149         return dest.empty() ? NULL : scoped.release();
150     }
151
152     const NameID* saml2name = dynamic_cast<const NameID*>(xmlObject);
153     if (saml2name) {
154         if (log.isDebugEnabled()) {
155             auto_ptr_char f(saml2name->getFormat());
156             log.debug("decoding ScopedAttribute (%s) from SAML 2 NameID with Format (%s)", ids.front().c_str(), f.get() ? f.get() : "unspecified");
157         }
158         val = toUTF8(saml2name->getName());
159     }
160     else {
161         const NameIdentifier* saml1name = dynamic_cast<const NameIdentifier*>(xmlObject);
162         if (saml1name) {
163             if (log.isDebugEnabled()) {
164                 auto_ptr_char f(saml1name->getFormat());
165                 log.debug(
166                     "decoding ScopedAttribute (%s) from SAML 1 NameIdentifier with Format (%s)",
167                     ids.front().c_str(), f.get() ? f.get() : "unspecified"
168                     );
169             }
170             val = toUTF8(saml1name->getName());
171         }
172         else {
173             log.warn("XMLObject type not recognized by ScopedAttributeDecoder, no values returned");
174             return NULL;
175         }
176     }
177
178     if (val && *val && *val!=m_delimeter) {
179         scope = strchr(val, m_delimeter);
180         if (scope) {
181             *scope++ = 0;
182             if (*scope)
183                 dest.push_back(pair<string,string>(val,scope));
184             else
185                 log.warn("ignoring NameID with no scope");
186         }
187         else {
188             log.warn("ignoring NameID with no scope delimiter (%c)", m_delimeter);
189         }
190     }
191     else {
192         log.warn("ignoring empty NameID");
193     }
194     delete[] val;
195     return dest.empty() ? NULL : scoped.release();
196 }