424ee7c5604d595a2608a3851a8526a3fa3354ff
[shibboleth/sp.git] / shibsp / attribute / ScopedAttributeDecoder.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  * 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 scopeDelimiter[] =   UNICODE_LITERAL_14(s,c,o,p,e,D,e,l,i,m,i,t,e,r);
39
40     class SHIBSP_DLLLOCAL ScopedAttributeDecoder : virtual public AttributeDecoder
41     {
42     public:
43         ScopedAttributeDecoder(const DOMElement* e) : AttributeDecoder(e), m_delimiter('@') {
44             if (e && e->hasAttributeNS(NULL,scopeDelimiter)) {
45                 auto_ptr_char d(e->getAttributeNS(NULL,scopeDelimiter));
46                 m_delimiter = *(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_delimiter;
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     xmltooling::QName scopeqname(NULL,Scope);
73     auto_ptr<ScopedAttribute> scoped(new ScopedAttribute(ids, m_delimiter));
74     vector< pair<string,string> >& dest = scoped->getValues();
75     vector<XMLObject*>::const_iterator v,stop;
76
77     Category& log = Category::getInstance(SHIBSP_LOGCAT".AttributeDecoder.Scoped");
78
79     if (xmlObject && XMLString::equals(opensaml::saml1::Attribute::LOCAL_NAME,xmlObject->getElementQName().getLocalPart())) {
80         const opensaml::saml2::Attribute* saml2attr = dynamic_cast<const opensaml::saml2::Attribute*>(xmlObject);
81         if (saml2attr) {
82             const vector<XMLObject*>& values = saml2attr->getAttributeValues();
83             v = values.begin();
84             stop = values.end();
85             if (log.isDebugEnabled()) {
86                 auto_ptr_char n(saml2attr->getName());
87                 log.debug(
88                     "decoding ScopedAttribute (%s) from SAML 2 Attribute (%s) with %lu value(s)",
89                     ids.front().c_str(), n.get() ? n.get() : "unnamed", values.size()
90                     );
91             }
92         }
93         else {
94             const opensaml::saml1::Attribute* saml1attr = dynamic_cast<const opensaml::saml1::Attribute*>(xmlObject);
95             if (saml1attr) {
96                 const vector<XMLObject*>& values = saml1attr->getAttributeValues();
97                 v = values.begin();
98                 stop = values.end();
99                 if (log.isDebugEnabled()) {
100                     auto_ptr_char n(saml1attr->getAttributeName());
101                     log.debug(
102                         "decoding ScopedAttribute (%s) from SAML 1 Attribute (%s) with %lu value(s)",
103                         ids.front().c_str(), n.get() ? n.get() : "unnamed", values.size()
104                         );
105                 }
106             }
107             else {
108                 log.warn("XMLObject type not recognized by ScopedAttributeDecoder, no values returned");
109                 return NULL;
110             }
111         }
112
113         for (; v!=stop; ++v) {
114             if (!(*v)->hasChildren()) {
115                 val = toUTF8((*v)->getTextContent());
116                 if (val && *val) {
117                     const AttributeExtensibleXMLObject* aexo=dynamic_cast<const AttributeExtensibleXMLObject*>(*v);
118                     xmlscope = aexo ? aexo->getAttribute(scopeqname) : NULL;
119                     if (xmlscope && *xmlscope) {
120                         scope = toUTF8(xmlscope);
121                         dest.push_back(pair<string,string>(val,scope));
122                         delete[] scope;
123                     }
124                     else {
125                         scope = strchr(val, m_delimiter);
126                         if (scope) {
127                             *scope++ = 0;
128                             if (*scope)
129                                 dest.push_back(pair<string,string>(val,scope));
130                             else
131                                 log.warn("ignoring unscoped AttributeValue");
132                         }
133                         else {
134                             log.warn("ignoring unscoped AttributeValue");
135                         }
136                     }
137                 }
138                 else {
139                     log.warn("skipping empty AttributeValue");
140                 }
141                 delete[] val;
142             }
143             else {
144                 log.warn("skipping complex AttributeValue");
145             }
146         }
147
148         return dest.empty() ? NULL : _decode(scoped.release());
149     }
150
151     const NameID* saml2name = dynamic_cast<const NameID*>(xmlObject);
152     if (saml2name) {
153         if (log.isDebugEnabled()) {
154             auto_ptr_char f(saml2name->getFormat());
155             log.debug("decoding ScopedAttribute (%s) from SAML 2 NameID with Format (%s)", ids.front().c_str(), f.get() ? f.get() : "unspecified");
156         }
157         val = toUTF8(saml2name->getName());
158     }
159     else {
160         const NameIdentifier* saml1name = dynamic_cast<const NameIdentifier*>(xmlObject);
161         if (saml1name) {
162             if (log.isDebugEnabled()) {
163                 auto_ptr_char f(saml1name->getFormat());
164                 log.debug(
165                     "decoding ScopedAttribute (%s) from SAML 1 NameIdentifier with Format (%s)",
166                     ids.front().c_str(), f.get() ? f.get() : "unspecified"
167                     );
168             }
169             val = toUTF8(saml1name->getName());
170         }
171         else {
172             log.warn("XMLObject type not recognized by ScopedAttributeDecoder, no values returned");
173             return NULL;
174         }
175     }
176
177     if (val && *val && *val!=m_delimiter) {
178         scope = strchr(val, m_delimiter);
179         if (scope) {
180             *scope++ = 0;
181             if (*scope)
182                 dest.push_back(pair<string,string>(val,scope));
183             else
184                 log.warn("ignoring NameID with no scope");
185         }
186         else {
187             log.warn("ignoring NameID with no scope delimiter (%c)", m_delimiter);
188         }
189     }
190     else {
191         log.warn("ignoring empty NameID");
192     }
193     delete[] val;
194     return dest.empty() ? NULL : _decode(scoped.release());
195 }