d30a32a25fda079fbc77428c3d948d35d982e7af
[shibboleth/cpp-sp.git] / shibsp / attribute / ScopedAttributeDecoder.cpp
1 /**
2  * Licensed to the University Corporation for Advanced Internet
3  * Development, Inc. (UCAID) under one or more contributor license
4  * agreements. See the NOTICE file distributed with this work for
5  * additional information regarding copyright ownership.
6  *
7  * UCAID licenses this file to you under the Apache License,
8  * Version 2.0 (the "License"); you may not use this file except
9  * in compliance with the License. You may obtain a copy of the
10  * License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17  * either express or implied. See the License for the specific
18  * language governing permissions and limitations under the License.
19  */
20
21 /**
22  * ScopedAttributeDecoder.cpp
23  *
24  * Decodes SAML into ScopedAttributes.
25  */
26
27 #include "internal.h"
28 #include "attribute/AttributeDecoder.h"
29 #include "attribute/ScopedAttribute.h"
30
31 #include <saml/saml1/core/Assertions.h>
32 #include <saml/saml2/core/Assertions.h>
33
34 using namespace shibsp;
35 using namespace opensaml::saml1;
36 using namespace opensaml::saml2;
37 using namespace xmltooling;
38 using namespace std;
39
40 namespace shibsp {
41     static const XMLCh Scope[] =            UNICODE_LITERAL_5(S,c,o,p,e);
42     static const XMLCh scopeDelimiter[] =   UNICODE_LITERAL_14(s,c,o,p,e,D,e,l,i,m,i,t,e,r);
43
44     class SHIBSP_DLLLOCAL ScopedAttributeDecoder : virtual public AttributeDecoder
45     {
46     public:
47         ScopedAttributeDecoder(const DOMElement* e) : AttributeDecoder(e), m_delimiter('@') {
48             if (e && e->hasAttributeNS(nullptr,scopeDelimiter)) {
49                 auto_ptr_char d(e->getAttributeNS(nullptr,scopeDelimiter));
50                 m_delimiter = *(d.get());
51             }
52         }
53         ~ScopedAttributeDecoder() {}
54
55         // deprecated method
56         shibsp::Attribute* decode(
57             const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty=nullptr, const char* relyingParty=nullptr
58             ) const {
59             return decode(nullptr, ids, xmlObject, assertingParty, relyingParty);
60         }
61
62         shibsp::Attribute* decode(
63             const GenericRequest*, const vector<string>&, const XMLObject*, const char* assertingParty=nullptr, const char* relyingParty=nullptr
64             ) const;
65
66     private:
67         char m_delimiter;
68     };
69
70     AttributeDecoder* SHIBSP_DLLLOCAL ScopedAttributeDecoderFactory(const DOMElement* const & e)
71     {
72         return new ScopedAttributeDecoder(e);
73     }
74 };
75
76 shibsp::Attribute* ScopedAttributeDecoder::decode(
77     const GenericRequest* request, const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty, const char* relyingParty
78     ) const
79 {
80     char* val;
81     char* scope;
82     const XMLCh* xmlscope;
83     xmltooling::QName scopeqname(nullptr,Scope);
84     auto_ptr<ScopedAttribute> scoped(new ScopedAttribute(ids, m_delimiter));
85     vector< pair<string,string> >& dest = scoped->getValues();
86     pair<vector<XMLObject*>::const_iterator,vector<XMLObject*>::const_iterator> valrange;
87
88     Category& log = Category::getInstance(SHIBSP_LOGCAT".AttributeDecoder.Scoped");
89
90     if (xmlObject && XMLString::equals(opensaml::saml1::Attribute::LOCAL_NAME,xmlObject->getElementQName().getLocalPart())) {
91         const opensaml::saml2::Attribute* saml2attr = dynamic_cast<const opensaml::saml2::Attribute*>(xmlObject);
92         if (saml2attr) {
93             const vector<XMLObject*>& values = saml2attr->getAttributeValues();
94             valrange = valueRange(request, values);
95             if (log.isDebugEnabled()) {
96                 auto_ptr_char n(saml2attr->getName());
97                 log.debug(
98                     "decoding ScopedAttribute (%s) from SAML 2 Attribute (%s) with %lu value(s)",
99                     ids.front().c_str(), n.get() ? n.get() : "unnamed", values.size()
100                     );
101             }
102         }
103         else {
104             const opensaml::saml1::Attribute* saml1attr = dynamic_cast<const opensaml::saml1::Attribute*>(xmlObject);
105             if (saml1attr) {
106                 const vector<XMLObject*>& values = saml1attr->getAttributeValues();
107                 valrange = valueRange(request, values);
108                 if (log.isDebugEnabled()) {
109                     auto_ptr_char n(saml1attr->getAttributeName());
110                     log.debug(
111                         "decoding ScopedAttribute (%s) from SAML 1 Attribute (%s) with %lu value(s)",
112                         ids.front().c_str(), n.get() ? n.get() : "unnamed", values.size()
113                         );
114                 }
115             }
116             else {
117                 log.warn("XMLObject type not recognized by ScopedAttributeDecoder, no values returned");
118                 return nullptr;
119             }
120         }
121
122         for (; valrange.first != valrange.second; ++valrange.first) {
123             if (!(*valrange.first)->hasChildren()) {
124                 val = toUTF8((*valrange.first)->getTextContent());
125                 if (val && *val) {
126                     const AttributeExtensibleXMLObject* aexo=dynamic_cast<const AttributeExtensibleXMLObject*>(*valrange.first);
127                     xmlscope = aexo ? aexo->getAttribute(scopeqname) : nullptr;
128                     if (xmlscope && *xmlscope) {
129                         auto_arrayptr<char> noninlinescope(toUTF8(xmlscope));
130                         dest.push_back(pair<string,string>(val,noninlinescope.get()));
131                     }
132                     else {
133                         scope = strchr(val, m_delimiter);
134                         if (scope) {
135                             *scope++ = 0;
136                             if (*scope)
137                                 dest.push_back(pair<string,string>(val,scope));
138                             else
139                                 log.warn("ignoring unscoped AttributeValue");
140                         }
141                         else {
142                             log.warn("ignoring unscoped AttributeValue");
143                         }
144                     }
145                 }
146                 else {
147                     log.warn("skipping empty AttributeValue");
148                 }
149                 delete[] val;
150             }
151             else {
152                 log.warn("skipping complex AttributeValue");
153             }
154         }
155
156         return dest.empty() ? nullptr : _decode(scoped.release());
157     }
158
159     const NameID* saml2name = dynamic_cast<const NameID*>(xmlObject);
160     if (saml2name) {
161         if (log.isDebugEnabled()) {
162             auto_ptr_char f(saml2name->getFormat());
163             log.debug("decoding ScopedAttribute (%s) from SAML 2 NameID with Format (%s)", ids.front().c_str(), f.get() ? f.get() : "unspecified");
164         }
165         val = toUTF8(saml2name->getName());
166     }
167     else {
168         const NameIdentifier* saml1name = dynamic_cast<const NameIdentifier*>(xmlObject);
169         if (saml1name) {
170             if (log.isDebugEnabled()) {
171                 auto_ptr_char f(saml1name->getFormat());
172                 log.debug(
173                     "decoding ScopedAttribute (%s) from SAML 1 NameIdentifier with Format (%s)",
174                     ids.front().c_str(), f.get() ? f.get() : "unspecified"
175                     );
176             }
177             val = toUTF8(saml1name->getName());
178         }
179         else {
180             log.warn("XMLObject type not recognized by ScopedAttributeDecoder, no values returned");
181             return nullptr;
182         }
183     }
184
185     if (val && *val && *val != m_delimiter) {
186         scope = strchr(val, m_delimiter);
187         if (scope) {
188             *scope++ = 0;
189             if (*scope)
190                 dest.push_back(pair<string,string>(val,scope));
191             else
192                 log.warn("ignoring NameID with no scope");
193         }
194         else {
195             log.warn("ignoring NameID with no scope delimiter (%c)", m_delimiter);
196         }
197     }
198     else {
199         log.warn("ignoring empty NameID");
200     }
201     delete[] val;
202     return dest.empty() ? nullptr : _decode(scoped.release());
203 }