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