VS10 solution files, convert from NULL macro to nullptr.
[shibboleth/sp.git] / shibsp / attribute / Attribute.cpp
index 70bff1f..ceedaef 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright 2001-2009 Internet2
+ *  Copyright 2001-2010 Internet2
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -34,6 +34,7 @@
 #include "util/SPConstants.h"
 
 #include <xercesc/util/XMLUniDefs.hpp>
+#include <xmltooling/security/SecurityHelper.h>
 
 using namespace shibsp;
 using namespace xmltooling;
@@ -64,6 +65,7 @@ namespace shibsp {
     static const XMLCh _XMLAttributeDecoder[] =    UNICODE_LITERAL_19(X,M,L,A,t,t,r,i,b,u,t,e,D,e,c,o,d,e,r);
 
     static const XMLCh caseSensitive[] =           UNICODE_LITERAL_13(c,a,s,e,S,e,n,s,i,t,i,v,e);
+    static const XMLCh hashAlg[] =                 UNICODE_LITERAL_7(h,a,s,h,A,l,g);
     static const XMLCh internal[] =                UNICODE_LITERAL_8(i,n,t,e,r,n,a,l);
 #endif
 };
@@ -89,14 +91,15 @@ void shibsp::registerAttributeDecoders()
     conf.AttributeDecoderManager.registerFactory(XMLAttributeDecoderType, XMLAttributeDecoderFactory);
 }
 
-AttributeDecoder::AttributeDecoder(const DOMElement *e) : m_caseSensitive(true), m_internal(false)
+AttributeDecoder::AttributeDecoder(const DOMElement *e)
+    : m_caseSensitive(true), m_internal(false), m_hashAlg(e ? e->getAttributeNS(nullptr, hashAlg) : nullptr)
 {
     if (e) {
-        const XMLCh* flag = e->getAttributeNS(NULL, caseSensitive);
+        const XMLCh* flag = e->getAttributeNS(nullptr, caseSensitive);
         if (flag && (*flag == chLatin_f || *flag == chDigit_0))
             m_caseSensitive = false;
 
-        flag = e->getAttributeNS(NULL, internal);
+        flag = e->getAttributeNS(nullptr, internal);
         if (flag && (*flag == chLatin_t || *flag == chDigit_1))
             m_internal = true;
     }
@@ -108,8 +111,27 @@ AttributeDecoder::~AttributeDecoder()
 
 Attribute* AttributeDecoder::_decode(Attribute* attr) const
 {
-    attr->setCaseSensitive(m_caseSensitive);
-    attr->setInternal(m_internal);
+    if (attr) {
+        attr->setCaseSensitive(m_caseSensitive);
+        attr->setInternal(m_internal);
+
+        if (m_hashAlg.get() && *m_hashAlg.get()) {
+            // We turn the values into strings using the supplied hash algorithm and return a SimpleAttribute instead.
+            auto_ptr<SimpleAttribute> simple(new SimpleAttribute(attr->getAliases()));
+            simple->setCaseSensitive(false);
+            simple->setInternal(m_internal);
+            vector<string>& newdest = simple->getValues();
+            const vector<string>& serialized = attr->getSerializedValues();
+            for (vector<string>::const_iterator ser = serialized.begin(); ser != serialized.end(); ++ser) {
+                newdest.push_back(SecurityHelper::doHash(m_hashAlg.get(), ser->data(), ser->length()));
+                if (newdest.back().empty())
+                    newdest.pop_back();
+            }
+            delete attr;
+            return newdest.empty() ? nullptr : simple.release();
+        }
+
+    }
     return attr;
 }
 #endif
@@ -218,7 +240,7 @@ const char* Attribute::getString(size_t index) const
 
 const char* Attribute::getScope(size_t index) const
 {
-    return NULL;
+    return nullptr;
 }
 
 void Attribute::removeValue(size_t index)
@@ -229,7 +251,7 @@ void Attribute::removeValue(size_t index)
 
 DDF Attribute::marshall() const
 {
-    DDF ddf(NULL);
+    DDF ddf(nullptr);
     ddf.structure().addmember(m_id.front().c_str()).list();
     if (!m_caseSensitive)
         ddf.addmember("case_insensitive");
@@ -239,7 +261,7 @@ DDF Attribute::marshall() const
         DDF alias;
         DDF aliases = ddf.addmember("aliases").list();
         for (std::vector<std::string>::const_iterator a = m_id.begin() + 1; a != m_id.end(); ++a) {
-            alias = DDF(NULL).string(a->c_str());
+            alias = DDF(nullptr).string(a->c_str());
             aliases.add(alias);
         }
     }