Change license header.
[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         shibsp::Attribute* decode(
64             const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty=nullptr, const char* relyingParty=nullptr
65             ) const;
66
67     private:
68         char m_delimeter;
69         string m_format,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.empty()) ? m_formatter.c_str() : 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                     destval.m_Format = m_format;
145                     if (m_defaultQualifiers && assertingParty)
146                         destval.m_NameQualifier = assertingParty;
147                     if (m_defaultQualifiers && relyingParty)
148                         destval.m_SPNameQualifier = relyingParty;
149                 }
150                 else {
151                     log.warn("skipping empty AttributeValue");
152                 }
153                 delete[] val;
154             }
155             else {
156                 log.warn("skipping complex AttributeValue");
157             }
158         }
159
160         return dest.empty() ? nullptr : _decode(nameid.release());
161     }
162
163     log.warn("XMLObject type not recognized by NameIDFromScopedAttributeDecoder, no values returned");
164     return nullptr;
165 }