Merge commit '2.5.0' into moonshot-packaging-fixes
[shibboleth/sp.git] / shibsp / attribute / StringAttributeDecoder.cpp
index 1fc5d22..e52d8f1 100644 (file)
@@ -44,8 +44,15 @@ namespace shibsp {
         StringAttributeDecoder(const DOMElement* e) : AttributeDecoder(e) {}
         ~StringAttributeDecoder() {}
 
+        // 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;
     };
 
@@ -56,13 +63,12 @@ namespace shibsp {
 };
 
 shibsp::Attribute* StringAttributeDecoder::decode(
-    const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty, const char* relyingParty
+    const GenericRequest* request, const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty, const char* relyingParty
     ) const
 {
-    char* val;
     auto_ptr<SimpleAttribute> simple(new SimpleAttribute(ids));
     vector<string>& dest = simple->getValues();
-    vector<XMLObject*>::const_iterator v,stop;
+    pair<vector<XMLObject*>::const_iterator,vector<XMLObject*>::const_iterator> valrange;
 
     Category& log = Category::getInstance(SHIBSP_LOGCAT".AttributeDecoder.String");
 
@@ -70,8 +76,7 @@ shibsp::Attribute* StringAttributeDecoder::decode(
         const opensaml::saml2::Attribute* saml2attr = dynamic_cast<const opensaml::saml2::Attribute*>(xmlObject);
         if (saml2attr) {
             const vector<XMLObject*>& values = saml2attr->getAttributeValues();
-            v = values.begin();
-            stop = values.end();
+            valrange = valueRange(request, values);
             if (log.isDebugEnabled()) {
                 auto_ptr_char n(saml2attr->getName());
                 log.debug(
@@ -84,8 +89,7 @@ shibsp::Attribute* StringAttributeDecoder::decode(
             const opensaml::saml1::Attribute* saml1attr = dynamic_cast<const opensaml::saml1::Attribute*>(xmlObject);
             if (saml1attr) {
                 const vector<XMLObject*>& values = saml1attr->getAttributeValues();
-                v = values.begin();
-                stop = values.end();
+                valrange = valueRange(request, values);
                 if (log.isDebugEnabled()) {
                     auto_ptr_char n(saml1attr->getAttributeName());
                 log.debug(
@@ -100,14 +104,13 @@ shibsp::Attribute* StringAttributeDecoder::decode(
             }
         }
 
-        for (; v!=stop; ++v) {
-            if (!(*v)->hasChildren()) {
-                val = toUTF8((*v)->getTextContent());
-                if (val && *val)
-                    dest.push_back(val);
+        for (; valrange.first != valrange.second; ++valrange.first) {
+            if (!(*valrange.first)->hasChildren()) {
+                auto_arrayptr<char> val(toUTF8((*valrange.first)->getTextContent()));
+                if (val.get() && *val.get())
+                    dest.push_back(val.get());
                 else
                     log.warn("skipping empty AttributeValue");
-                delete[] val;
             }
             else {
                 log.warn("skipping complex AttributeValue");
@@ -123,7 +126,11 @@ shibsp::Attribute* StringAttributeDecoder::decode(
             auto_ptr_char f(saml2name->getFormat());
             log.debug("decoding SimpleAttribute (%s) from SAML 2 NameID with Format (%s)", ids.front().c_str(), f.get() ? f.get() : "unspecified");
         }
-        val = toUTF8(saml2name->getName());
+        auto_arrayptr<char> val(toUTF8(saml2name->getName()));
+        if (val.get() && *val.get())
+            dest.push_back(val.get());
+        else
+            log.warn("ignoring empty NameID");
     }
     else {
         const NameIdentifier* saml1name = dynamic_cast<const NameIdentifier*>(xmlObject);
@@ -135,7 +142,11 @@ shibsp::Attribute* StringAttributeDecoder::decode(
                     ids.front().c_str(), f.get() ? f.get() : "unspecified"
                     );
             }
-            val = toUTF8(saml1name->getName());
+            auto_arrayptr<char> val(toUTF8(saml1name->getName()));
+            if (val.get() && *val.get())
+                dest.push_back(val.get());
+            else
+                log.warn("ignoring empty NameIdentifier");
         }
         else {
             log.warn("XMLObject type not recognized by StringAttributeDecoder, no values returned");
@@ -143,10 +154,5 @@ shibsp::Attribute* StringAttributeDecoder::decode(
         }
     }
 
-    if (val && *val)
-        dest.push_back(val);
-    else
-        log.warn("ignoring empty NameID");
-    delete[] val;
     return dest.empty() ? nullptr : _decode(simple.release());
 }