SSPCPP-341 - Transform resolver
authorScott Cantor <cantor.2@osu.edu>
Fri, 6 Apr 2012 18:18:20 +0000 (18:18 +0000)
committerScott Cantor <cantor.2@osu.edu>
Fri, 6 Apr 2012 18:18:20 +0000 (18:18 +0000)
plugins/Makefile.am
plugins/TransformAttributeResolver.cpp [new file with mode: 0644]
plugins/plugins.cpp
plugins/plugins.vcxproj
plugins/plugins.vcxproj.filters

index ab08e71..5ccd500 100644 (file)
@@ -11,7 +11,8 @@ common_sources = \
 
 plugins_la_SOURCES = \
        ${common_sources} \
-       GSSAPIAttributeExtractor.cpp
+       GSSAPIAttributeExtractor.cpp \
+    TransformAttributeResolver.cpp
 
 #plugins_lite_la_SOURCES = \
 #      ${common_sources}
diff --git a/plugins/TransformAttributeResolver.cpp b/plugins/TransformAttributeResolver.cpp
new file mode 100644 (file)
index 0000000..2176fe8
--- /dev/null
@@ -0,0 +1,249 @@
+/**
+ * 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.
+ *
+ * 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
+ *
+ * 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.
+ */
+
+/**
+ * TransformAttributeResolver.cpp
+ * 
+ * Attribute Resolver plugin for transforming input values.
+ */
+
+#include "internal.h"
+
+#include <algorithm>
+#include <boost/shared_ptr.hpp>
+#include <shibsp/exceptions.h>
+#include <shibsp/SessionCache.h>
+#include <shibsp/attribute/SimpleAttribute.h>
+#include <shibsp/attribute/resolver/AttributeResolver.h>
+#include <shibsp/attribute/resolver/ResolutionContext.h>
+#include <xmltooling/XMLToolingConfig.h>
+#include <xmltooling/util/XMLHelper.h>
+#include <xercesc/util/XMLUniDefs.hpp>
+#include <xercesc/util/regx/RegularExpression.hpp>
+
+using namespace shibsp;
+using namespace xmltooling;
+using namespace xercesc;
+using namespace std;
+
+namespace shibsp {
+
+    class SHIBSP_DLLLOCAL TransformContext : public ResolutionContext
+    {
+    public:
+        TransformContext(const Session& session) : m_inputAttributes(&session.getAttributes()) {
+        }
+
+        TransformContext(const vector<shibsp::Attribute*>* attributes) : m_inputAttributes(attributes) {
+        }
+
+        ~TransformContext() {
+            for_each(m_attributes.begin(), m_attributes.end(), xmltooling::cleanup<shibsp::Attribute>());
+        }
+
+        const vector<shibsp::Attribute*>* getInputAttributes() const {
+            return m_inputAttributes;
+        }
+        vector<shibsp::Attribute*>& getResolvedAttributes() {
+            return m_attributes;
+        }
+        vector<opensaml::Assertion*>& getResolvedAssertions() {
+            return m_assertions;
+        }
+
+    private:
+        const vector<shibsp::Attribute*>* m_inputAttributes;
+        vector<shibsp::Attribute*> m_attributes;
+        static vector<opensaml::Assertion*> m_assertions;   // empty dummy
+    };
+
+
+    class SHIBSP_DLLLOCAL TransformAttributeResolver : public AttributeResolver
+    {
+    public:
+        TransformAttributeResolver(const DOMElement* e);
+        virtual ~TransformAttributeResolver() {}
+
+        Lockable* lock() {
+            return this;
+        }
+        void unlock() {
+        }
+
+        ResolutionContext* createResolutionContext(
+            const Application& application,
+            const opensaml::saml2md::EntityDescriptor* issuer,
+            const XMLCh* protocol,
+            const opensaml::saml2::NameID* nameid=nullptr,
+            const XMLCh* authncontext_class=nullptr,
+            const XMLCh* authncontext_decl=nullptr,
+            const vector<const opensaml::Assertion*>* tokens=nullptr,
+            const vector<shibsp::Attribute*>* attributes=nullptr
+            ) const {
+            // Make sure new method gets run.
+            return createResolutionContext(application, nullptr, issuer, protocol, nameid, authncontext_class, authncontext_decl, tokens, attributes);
+        }
+
+        ResolutionContext* createResolutionContext(
+            const Application& application,
+            const GenericRequest* request,
+            const opensaml::saml2md::EntityDescriptor* issuer,
+            const XMLCh* protocol,
+            const opensaml::saml2::NameID* nameid=nullptr,
+            const XMLCh* authncontext_class=nullptr,
+            const XMLCh* authncontext_decl=nullptr,
+            const vector<const opensaml::Assertion*>* tokens=nullptr,
+            const vector<shibsp::Attribute*>* attributes=nullptr
+            ) const {
+            return new TransformContext(attributes);
+        }
+
+        ResolutionContext* createResolutionContext(const Application& application, const Session& session) const {
+            return new TransformContext(session);
+        }
+
+        void resolveAttributes(ResolutionContext& ctx) const;
+
+        void getAttributeIds(vector<string>& attributes) const {
+            if (!m_dest.empty())
+                attributes.push_back(m_dest.front());
+        }
+
+    private:
+        Category& m_log;
+        string m_source;
+        vector<string> m_dest;
+        vector< pair<boost::shared_ptr<RegularExpression>,const XMLCh*> > m_regex;
+    };
+
+    static const XMLCh dest[] =         UNICODE_LITERAL_4(d,e,s,t);
+    static const XMLCh match[] =        UNICODE_LITERAL_5(m,a,t,c,h);
+    static const XMLCh source[] =       UNICODE_LITERAL_6(s,o,u,r,c,e);
+    static const XMLCh Regex[] =        UNICODE_LITERAL_5(R,e,g,e,x);
+
+    AttributeResolver* SHIBSP_DLLLOCAL TransformAttributeResolverFactory(const DOMElement* const & e)
+    {
+        return new TransformAttributeResolver(e);
+    }
+
+};
+
+vector<opensaml::Assertion*> TransformContext::m_assertions;
+
+TransformAttributeResolver::TransformAttributeResolver(const DOMElement* e)
+    : m_log(Category::getInstance(SHIBSP_LOGCAT".AttributeResolver.Transform")),
+        m_source(XMLHelper::getAttrString(e, nullptr, source)),
+        m_dest(1, XMLHelper::getAttrString(e, nullptr, dest))
+{
+    if (m_source.empty())
+        throw ConfigurationException("Transform AttributeResolver requires source attribute.");
+
+    e = XMLHelper::getFirstChildElement(e, Regex);
+    while (e) {
+        if (e->hasChildNodes() && e->hasAttributeNS(nullptr, match)) {
+            const XMLCh* repl = e->getTextContent();
+            if (repl && *repl) {
+                try {
+                    boost::shared_ptr<RegularExpression> re(new RegularExpression(e->getAttributeNS(nullptr, match)));
+                    m_regex.push_back(pair<boost::shared_ptr<RegularExpression>,const XMLCh*>(re, repl));
+                }
+                catch (XMLException& ex) {
+                    auto_ptr_char msg(ex.getMessage());
+                    auto_ptr_char m(e->getAttributeNS(nullptr, match));
+                    m_log.error("exception parsing regular expression (%s): %s", m.get(), msg.get());
+                }
+            }
+        }
+        e = XMLHelper::getNextSiblingElement(e, Regex);
+    }
+
+    if (m_regex.empty())
+        throw ConfigurationException("Transform AttributeResolver requires at least one Regex element.");
+}
+
+
+void TransformAttributeResolver::resolveAttributes(ResolutionContext& ctx) const
+{
+    TransformContext& tctx = dynamic_cast<TransformContext&>(ctx);
+    if (!tctx.getInputAttributes())
+        return;
+
+    SimpleAttribute* dest = nullptr;
+    auto_ptr<SimpleAttribute> destwrapper;
+
+    for (vector<Attribute*>::const_iterator a = tctx.getInputAttributes()->begin(); a != tctx.getInputAttributes()->end(); ++a) {
+        if (m_source != (*a)->getId() || (*a)->valueCount() == 0) {
+            continue;
+        }
+        else if (m_dest.empty() || m_dest.front().empty()) {
+            // Can we transform in-place?
+            dest = dynamic_cast<SimpleAttribute*>(*a);
+            if (!dest) {
+                m_log.warn("can't transform non-simple attribute (%s) in place, skipping it", m_source.c_str());
+                continue;
+            }
+        }
+        else if (!destwrapper.get()) {
+            destwrapper.reset(new SimpleAttribute(m_dest));
+        }
+
+        m_log.debug("applying transform to source attribute (%s) with %lu value(s)", m_source.c_str(), (*a)->valueCount());
+
+        // Apply transforms to each value.
+        for (size_t i = 0; i < (*a)->valueCount(); ++i) {
+            // Run the transform set in sequence against the initial value, substituting the result into the next step.
+            XMLCh* destval = nullptr;
+            auto_arrayptr<XMLCh> srcval(fromUTF8((*a)->getSerializedValues()[i].c_str()));
+            for (vector< pair<boost::shared_ptr<RegularExpression>,const XMLCh*> >::const_iterator r = m_regex.begin(); r != m_regex.end(); ++r) {
+                try {
+                    XMLCh* temp = r->first->replace(destval ? destval : srcval.get(), r->second);
+                    if (temp) {
+                        XMLString::release(&destval);
+                        destval = temp;
+                    }
+                }
+                catch (XMLException& ex) {
+                    auto_ptr_char msg(ex.getMessage());
+                    m_log.error("caught error applying regular expression: %s", msg.get());
+                }
+            }
+
+            // Save the result.
+            if (destval) {
+                auto_arrayptr<char> narrow(toUTF8(destval));
+                XMLString::release(&destval);
+                if (dest) {
+                    // Modify in place.
+                    dest->getValues()[i] = narrow.get();
+                }
+                else {
+                    // Add to new object.
+                    destwrapper->getValues().push_back(narrow.get());
+                }
+            }
+        }
+    }
+
+    // Save off new object.
+    if (destwrapper.get()) {
+        ctx.getResolvedAttributes().push_back(destwrapper.get());
+        destwrapper.release();
+    }
+}
index 19a437d..44d6dc4 100644 (file)
@@ -44,6 +44,7 @@ namespace shibsp {
 #ifdef HAVE_GSSAPI_NAMINGEXTS
     PluginManager<AttributeExtractor,string,const DOMElement*>::Factory GSSAPIExtractorFactory;
 #endif
+    PluginManager<AttributeResolver,string,const DOMElement*>::Factory TransformAttributeResolverFactory;
 };
 
 extern "C" int PLUGINS_EXPORTS xmltooling_extension_init(void*)
@@ -55,6 +56,7 @@ extern "C" int PLUGINS_EXPORTS xmltooling_extension_init(void*)
     XMLObjectBuilder::registerBuilder(xmltooling::QName(shibspconstants::SHIB2ATTRIBUTEMAP_NS, _GSSAPIName), new AnyElementBuilder());
     XMLObjectBuilder::registerBuilder(xmltooling::QName(shibspconstants::SHIB2ATTRIBUTEMAP_NS, _GSSAPIContext), new AnyElementBuilder());
 #endif
+    SPConfig::getConfig().AttributeResolverManager.registerFactory("Transform", TransformAttributeResolverFactory);
     return 0;   // signal success
 }
 
index 7aa9991..b3e4a11 100644 (file)
   <ItemGroup>
     <ClCompile Include="GSSAPIAttributeExtractor.cpp" />
     <ClCompile Include="plugins.cpp" />
+    <ClCompile Include="TransformAttributeResolver.cpp" />
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="internal.h" />
index 8e1bc9b..ab750f4 100644 (file)
@@ -3,6 +3,7 @@
   <ItemGroup>
     <ClCompile Include="GSSAPIAttributeExtractor.cpp" />
     <ClCompile Include="plugins.cpp" />
+    <ClCompile Include="TransformAttributeResolver.cpp" />
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="internal.h" />