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