VS10 solution files, convert from NULL macro to nullptr.
[shibboleth/sp.git] / shibsp / attribute / NameIDFromScopedAttributeDecoder.cpp
1 /*
2  *  Copyright 2001-2010 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  * NameIDFromNameIDFromScopedAttributeDecoder.cpp
19  *
20  * Decodes SAML "scoped" attributes into NameIDAttributes.
21  */
22
23 #include "internal.h"
24 #include "attribute/AttributeDecoder.h"
25 #include "attribute/NameIDAttribute.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 defaultQualifiers[] =UNICODE_LITERAL_17(d,e,f,a,u,l,t,Q,u,a,l,i,f,i,e,r,s);
38     static const XMLCh format[] =           UNICODE_LITERAL_6(f,o,r,m,a,t);
39     static const XMLCh formatter[] =        UNICODE_LITERAL_9(f,o,r,m,a,t,t,e,r);
40     static const XMLCh Scope[] =            UNICODE_LITERAL_5(S,c,o,p,e);
41     static const XMLCh scopeDelimeter[] =   UNICODE_LITERAL_14(s,c,o,p,e,D,e,l,i,m,e,t,e,r);
42
43     class SHIBSP_DLLLOCAL NameIDFromScopedAttributeDecoder : virtual public AttributeDecoder
44     {
45     public:
46         NameIDFromScopedAttributeDecoder(const DOMElement* e)
47             : AttributeDecoder(e),
48                 m_delimeter('@'),
49                 m_format(e ? e->getAttributeNS(nullptr,format) : nullptr),
50                 m_formatter(e ? e->getAttributeNS(nullptr,formatter) : nullptr),
51                 m_defaultQualifiers(false) {
52             if (e && e->hasAttributeNS(nullptr,scopeDelimeter)) {
53                 auto_ptr_char d(e->getAttributeNS(nullptr,scopeDelimeter));
54                 m_delimeter = *(d.get());
55             }
56             const XMLCh* flag = e ? e->getAttributeNS(nullptr, defaultQualifiers) : nullptr;
57             if (flag && (*flag == chLatin_t || *flag == chDigit_1))
58                 m_defaultQualifiers = true;
59         }
60         ~NameIDFromScopedAttributeDecoder() {}
61
62         shibsp::Attribute* decode(
63             const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty=nullptr, const char* relyingParty=nullptr
64             ) const;
65
66     private:
67         char m_delimeter;
68         auto_ptr_char m_format;
69         auto_ptr_char m_formatter;
70         bool m_defaultQualifiers;
71     };
72
73     AttributeDecoder* SHIBSP_DLLLOCAL NameIDFromScopedAttributeDecoderFactory(const DOMElement* const & e)
74     {
75         return new NameIDFromScopedAttributeDecoder(e);
76     }
77 };
78
79 shibsp::Attribute* NameIDFromScopedAttributeDecoder::decode(
80     const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty, const char* relyingParty
81     ) const
82 {
83
84     char* val;
85     char* scope;
86     const XMLCh* xmlscope;
87     xmltooling::QName scopeqname(nullptr,Scope);
88     auto_ptr<NameIDAttribute> nameid(
89         new NameIDAttribute(ids, (m_formatter.get() && *m_formatter.get()) ? m_formatter.get() : DEFAULT_NAMEID_FORMATTER)
90         );
91     vector<NameIDAttribute::Value>& dest = nameid->getValues();
92     vector<XMLObject*>::const_iterator v,stop;
93
94     Category& log = Category::getInstance(SHIBSP_LOGCAT".AttributeDecoder.NameIDFromScoped");
95
96     if (xmlObject && XMLString::equals(opensaml::saml1::Attribute::LOCAL_NAME,xmlObject->getElementQName().getLocalPart())) {
97         const opensaml::saml2::Attribute* saml2attr = dynamic_cast<const opensaml::saml2::Attribute*>(xmlObject);
98         if (saml2attr) {
99             const vector<XMLObject*>& values = saml2attr->getAttributeValues();
100             v = values.begin();
101             stop = values.end();
102             if (log.isDebugEnabled()) {
103                 auto_ptr_char n(saml2attr->getName());
104                 log.debug(
105                     "decoding NameIDAttribute (%s) from SAML 2 Attribute (%s) with %lu value(s)",
106                     ids.front().c_str(), n.get() ? n.get() : "unnamed", values.size()
107                     );
108             }
109         }
110         else {
111             const opensaml::saml1::Attribute* saml1attr = dynamic_cast<const opensaml::saml1::Attribute*>(xmlObject);
112             if (saml1attr) {
113                 const vector<XMLObject*>& values = saml1attr->getAttributeValues();
114                 v = values.begin();
115                 stop = values.end();
116                 if (log.isDebugEnabled()) {
117                     auto_ptr_char n(saml1attr->getAttributeName());
118                     log.debug(
119                         "decoding NameIDAttribute (%s) from SAML 1 Attribute (%s) with %lu value(s)",
120                         ids.front().c_str(), n.get() ? n.get() : "unnamed", values.size()
121                         );
122                 }
123             }
124             else {
125                 log.warn("XMLObject type not recognized by NameIDFromScopedAttributeDecoder, no values returned");
126                 return nullptr;
127             }
128         }
129
130         for (; v!=stop; ++v) {
131             if (!(*v)->hasChildren()) {
132                 val = toUTF8((*v)->getTextContent());
133                 if (val && *val) {
134                     dest.push_back(NameIDAttribute::Value());
135                     NameIDAttribute::Value& destval = dest.back();
136                     const AttributeExtensibleXMLObject* aexo=dynamic_cast<const AttributeExtensibleXMLObject*>(*v);
137                     xmlscope = aexo ? aexo->getAttribute(scopeqname) : nullptr;
138                     if (!xmlscope || !*xmlscope) {
139                         // Terminate the value at the scope delimiter.
140                         if (scope = strchr(val, m_delimeter))
141                             *scope++ = 0;
142                     }
143                     destval.m_Name = val;
144                     if (m_format.get() && *m_format.get())
145                         destval.m_Format = m_format.get();
146                     if (m_defaultQualifiers && assertingParty)
147                         destval.m_NameQualifier = assertingParty;
148                     if (m_defaultQualifiers && relyingParty)
149                         destval.m_SPNameQualifier = relyingParty;
150                 }
151                 else {
152                     log.warn("skipping empty AttributeValue");
153                 }
154                 delete[] val;
155             }
156             else {
157                 log.warn("skipping complex AttributeValue");
158             }
159         }
160
161         return dest.empty() ? nullptr : _decode(nameid.release());
162     }
163
164     log.warn("XMLObject type not recognized by NameIDFromScopedAttributeDecoder, no values returned");
165     return nullptr;
166 }