SSPCPP-616 - fix tuple namespaces and string literal errors
[shibboleth/cpp-sp.git] / shibsp / attribute / NameIDAttributeDecoder.cpp
index 6b03fae..d3a198b 100644 (file)
@@ -1,17 +1,21 @@
-/*
- *  Copyright 2001-2010 Internet2
+/**
+ * Licensed to the University Corporation for Advanced Internet
+ * Development, Inc. (UCAID) under one or more contributor license
+ * agreements. See the NOTICE file distributed with this work for
+ * additional information regarding copyright ownership.
  *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
+ * UCAID licenses this file to you under the Apache License,
+ * Version 2.0 (the "License"); you may not use this file except
+ * in compliance with the License. You may obtain a copy of the
+ * License at
  *
- *     http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
  *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
+ * either express or implied. See the License for the specific
+ * language governing permissions and limitations under the License.
  */
 
 /**
@@ -41,15 +45,21 @@ namespace shibsp {
     {
     public:
         NameIDAttributeDecoder(const DOMElement* e)
-                : AttributeDecoder(e), m_formatter(e ? e->getAttributeNS(nullptr, formatter) : nullptr), m_defaultQualifiers(false) {
-            const XMLCh* flag = e ? e->getAttributeNS(nullptr, defaultQualifiers) : nullptr;
-            if (flag && (*flag == chLatin_t || *flag == chDigit_1))
-                m_defaultQualifiers = true;
+            : AttributeDecoder(e),
+                m_formatter(XMLHelper::getAttrString(e, nullptr, formatter)),
+                m_defaultQualifiers(XMLHelper::getAttrBool(e, false, defaultQualifiers)) {
         }
         ~NameIDAttributeDecoder() {}
 
+        // deprecated method
         shibsp::Attribute* decode(
             const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty=nullptr, const char* relyingParty=nullptr
+            ) const {
+            return decode(nullptr, ids, xmlObject, assertingParty, relyingParty);
+        }
+
+        shibsp::Attribute* decode(
+            const GenericRequest*, const vector<string>&, const XMLObject*, const char* assertingParty=nullptr, const char* relyingParty=nullptr
             ) const;
 
     private:
@@ -59,7 +69,7 @@ namespace shibsp {
         void extract(
             const NameIdentifier* n, vector<NameIDAttribute::Value>& dest, const char* assertingParty, const char* relyingParty
             ) const;
-        auto_ptr_char m_formatter;
+        string m_formatter;
         bool m_defaultQualifiers;
     };
 
@@ -70,16 +80,16 @@ namespace shibsp {
 };
 
 shibsp::Attribute* NameIDAttributeDecoder::decode(
-    const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty, const char* relyingParty
+    const GenericRequest*, const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty, const char* relyingParty
     ) const
 {
     auto_ptr<NameIDAttribute> nameid(
-        new NameIDAttribute(ids, (m_formatter.get() && *m_formatter.get()) ? m_formatter.get() : DEFAULT_NAMEID_FORMATTER)
+        new NameIDAttribute(ids, (!m_formatter.empty()) ? m_formatter.c_str() : DEFAULT_NAMEID_FORMATTER, m_hashAlg.c_str())
         );
     vector<NameIDAttribute::Value>& dest = nameid->getValues();
     vector<XMLObject*>::const_iterator v,stop;
 
-    Category& log = Category::getInstance(SHIBSP_LOGCAT".AttributeDecoder.NameID");
+    Category& log = Category::getInstance(SHIBSP_LOGCAT ".AttributeDecoder.NameID");
 
     if (xmlObject && XMLString::equals(opensaml::saml1::Attribute::LOCAL_NAME,xmlObject->getElementQName().getLocalPart())) {
         const opensaml::saml2::Attribute* saml2attr = dynamic_cast<const opensaml::saml2::Attribute*>(xmlObject);
@@ -115,29 +125,41 @@ shibsp::Attribute* NameIDAttributeDecoder::decode(
             }
         }
 
-        for (; v!=stop; ++v) {
+        for (; v != stop; ++v) {
             const NameIDType* n2 = dynamic_cast<const NameIDType*>(*v);
-            if (n2)
+            if (n2) {
+                log.debug("decoding AttributeValue element of saml2:NameIDType type");
                 extract(n2, dest, assertingParty, relyingParty);
+            }
             else {
                 const NameIdentifier* n1=dynamic_cast<const NameIdentifier*>(*v);
-                if (n1)
+                if (n1) {
+                    log.debug("decoding AttributeValue element of saml1:NameIdentifier type");
                     extract(n1, dest, assertingParty, relyingParty);
+                }
                 else if ((*v)->hasChildren()) {
                     const list<XMLObject*>& values = (*v)->getOrderedChildren();
                     for (list<XMLObject*>::const_iterator vv = values.begin(); vv!=values.end(); ++vv) {
-                        if (n2=dynamic_cast<const NameIDType*>(*vv))
+                        if (n2=dynamic_cast<const NameIDType*>(*vv)) {
+                            log.debug("decoding saml2:NameID child element of AttributeValue");
                             extract(n2, dest, assertingParty, relyingParty);
-                        else if (n1=dynamic_cast<const NameIdentifier*>(*vv))
+                        }
+                        else if (n1=dynamic_cast<const NameIdentifier*>(*vv)) {
+                            log.debug("decoding saml1:NameIdentifier child element of AttributeValue");
                             extract(n1, dest, assertingParty, relyingParty);
-                        else
-                            log.warn("skipping AttributeValue without a recognizable NameID/NameIdentifier");
+                        }
+                        else {
+                            log.warn("skipping AttributeValue child element not recognizable as NameID/NameIdentifier");
+                        }
                     }
                 }
+                else {
+                    log.warn("AttributeValue was not of a supported type and contains no child elements");
+                }
             }
         }
 
-        return dest.empty() ? nullptr : _decode(nameid.release());
+        return dest.empty() ? nullptr : nameid.release();
     }
 
     const NameIDType* saml2name = dynamic_cast<const NameIDType*>(xmlObject);
@@ -166,7 +188,7 @@ shibsp::Attribute* NameIDAttributeDecoder::decode(
         }
     }
 
-    return dest.empty() ? nullptr : _decode(nameid.release());
+    return dest.empty() ? nullptr : nameid.release();
 }
 
 void NameIDAttributeDecoder::extract(
@@ -178,31 +200,26 @@ void NameIDAttributeDecoder::extract(
         dest.push_back(NameIDAttribute::Value());
         NameIDAttribute::Value& val = dest.back();
         val.m_Name = name.get();
-        char* str = toUTF8(n->getFormat());
-        if (str) {
-            val.m_Format = str;
-            delete[] str;
-        }
 
-        str = toUTF8(n->getNameQualifier());
-        if (str && *str)
-            val.m_NameQualifier = str;
+        auto_arrayptr<char> format(toUTF8(n->getFormat()));
+        if (format.get())
+            val.m_Format = format.get();
+
+        auto_arrayptr<char> nameQualifier(toUTF8(n->getNameQualifier()));
+        if (nameQualifier.get() && *nameQualifier.get())
+            val.m_NameQualifier = nameQualifier.get();
         else if (m_defaultQualifiers && assertingParty)
             val.m_NameQualifier = assertingParty;
-        delete[] str;
 
-        str = toUTF8(n->getSPNameQualifier());
-        if (str && *str)
-            val.m_SPNameQualifier = str;
+        auto_arrayptr<char> spNameQualifier(toUTF8(n->getSPNameQualifier()));
+        if (spNameQualifier.get() && *spNameQualifier.get())
+            val.m_SPNameQualifier = spNameQualifier.get();
         else if (m_defaultQualifiers && relyingParty)
             val.m_SPNameQualifier = relyingParty;
-        delete[] str;
 
-        str = toUTF8(n->getSPProvidedID());
-        if (str) {
-            val.m_SPProvidedID = str;
-            delete[] str;
-        }
+        auto_arrayptr<char> spProvidedID(toUTF8(n->getSPProvidedID()));
+        if (spProvidedID.get())
+            val.m_SPProvidedID = spProvidedID.get();
     }
 }
 
@@ -215,18 +232,16 @@ void NameIDAttributeDecoder::extract(
         dest.push_back(NameIDAttribute::Value());
         NameIDAttribute::Value& val = dest.back();
         val.m_Name = name.get();
-        char* str = toUTF8(n->getFormat());
-        if (str) {
-            val.m_Format = str;
-            delete[] str;
-        }
 
-        str = toUTF8(n->getNameQualifier());
-        if (str && *str)
-            val.m_NameQualifier = str;
+        auto_arrayptr<char> format(toUTF8(n->getFormat()));
+        if (format.get())
+            val.m_Format = format.get();
+
+        auto_arrayptr<char> nameQualifier(toUTF8(n->getNameQualifier()));
+        if (nameQualifier.get() && *nameQualifier.get())
+            val.m_NameQualifier = nameQualifier.get();
         else if (m_defaultQualifiers && assertingParty)
             val.m_NameQualifier = assertingParty;
-        delete[] str;
 
         if (m_defaultQualifiers && relyingParty)
             val.m_SPNameQualifier = relyingParty;