From 52e973ac041e2fc3ae3131b1099d435dce6a730d Mon Sep 17 00:00:00 2001 From: scantor Date: Mon, 23 May 2011 00:52:53 +0000 Subject: [PATCH] https://bugs.internet2.edu/jira/browse/SSPCPP-365 git-svn-id: https://svn.shibboleth.net/cpp-sp/branches/REL_2@3432 cb58f699-b61c-0410-a6fe-9272a202ed29 --- plugins/GSSAPIAttributeExtractor.cpp | 24 ++----- shibsp/Makefile.am | 2 + shibsp/attribute/Attribute.cpp | 2 + shibsp/attribute/BinaryAttribute.cpp | 131 +++++++++++++++++++++++++++++++++++ shibsp/attribute/BinaryAttribute.h | 93 +++++++++++++++++++++++++ shibsp/shibsp-lite.vcxproj | 2 + shibsp/shibsp.vcxproj | 2 + 7 files changed, 239 insertions(+), 17 deletions(-) create mode 100644 shibsp/attribute/BinaryAttribute.cpp create mode 100644 shibsp/attribute/BinaryAttribute.h diff --git a/plugins/GSSAPIAttributeExtractor.cpp b/plugins/GSSAPIAttributeExtractor.cpp index b17aed7..ec90a89 100644 --- a/plugins/GSSAPIAttributeExtractor.cpp +++ b/plugins/GSSAPIAttributeExtractor.cpp @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -273,22 +274,7 @@ void GSSAPIExtractorImpl::extractAttributes( return; } if (buf.length) { - if (rule->second.binary) { - // base64 encode the value - xsecsize_t len=0; - XMLByte* out=Base64::encode(reinterpret_cast(buf.value), buf.length, &len); - if (out) { - values.push_back(string(reinterpret_cast(out), len)); -#ifdef SHIBSP_XERCESC_HAS_XMLBYTE_RELEASE - XMLString::release(&out); -#else - XMLString::release((char**)&out); -#endif - } - } - else { - values.push_back(string(reinterpret_cast(buf.value), buf.length)); - } + values.push_back(string(reinterpret_cast(buf.value), buf.length)); } gss_release_buffer(&minor, &buf); } @@ -319,8 +305,12 @@ void GSSAPIExtractorImpl::extractAttributes( if (!scoped->getValues().empty()) attributes.push_back(scoped.release()); } + else if (rule->second.binary) { + auto_ptr binary(new BinaryAttribute(rule->second.ids)); + binary->getValues() = values; + attributes.push_back(binary.release()); + } else { - // If unscoped, just copy over the values. auto_ptr simple(new SimpleAttribute(rule->second.ids)); simple->getValues() = values; attributes.push_back(simple.release()); diff --git a/shibsp/Makefile.am b/shibsp/Makefile.am index b70cd50..34ce43c 100644 --- a/shibsp/Makefile.am +++ b/shibsp/Makefile.am @@ -45,6 +45,7 @@ libshibspinclude_HEADERS = \ attrinclude_HEADERS = \ attribute/Attribute.h \ attribute/AttributeDecoder.h \ + attribute/BinaryAttribute.h \ attribute/ExtensibleAttribute.h \ attribute/NameIDAttribute.h \ attribute/ScopedAttribute.h \ @@ -112,6 +113,7 @@ common_sources = \ SPConfig.cpp \ version.cpp \ attribute/Attribute.cpp \ + attribute/BinaryAttribute.cpp \ attribute/ExtensibleAttribute.cpp \ attribute/NameIDAttribute.cpp \ attribute/SimpleAttribute.cpp \ diff --git a/shibsp/attribute/Attribute.cpp b/shibsp/attribute/Attribute.cpp index 89f69bf..eba41df 100644 --- a/shibsp/attribute/Attribute.cpp +++ b/shibsp/attribute/Attribute.cpp @@ -47,6 +47,7 @@ namespace shibsp { SHIBSP_DLLLOCAL Attribute* NameIDAttributeFactory(DDF& in); SHIBSP_DLLLOCAL Attribute* ExtensibleAttributeFactory(DDF& in); SHIBSP_DLLLOCAL Attribute* XMLAttributeFactory(DDF& in); + SHIBSP_DLLLOCAL Attribute* BinaryAttributeFactory(DDF& in); #ifndef SHIBSP_LITE SHIBSP_DLLLOCAL PluginManager::Factory StringAttributeDecoderFactory; @@ -142,6 +143,7 @@ void shibsp::registerAttributeFactories() { Attribute::registerFactory("", SimpleAttributeFactory); Attribute::registerFactory("Simple", SimpleAttributeFactory); + Attribute::registerFactory("Binary", BinaryAttributeFactory); Attribute::registerFactory("Scoped", ScopedAttributeFactory); Attribute::registerFactory("NameID", NameIDAttributeFactory); Attribute::registerFactory("Extensible", ExtensibleAttributeFactory); diff --git a/shibsp/attribute/BinaryAttribute.cpp b/shibsp/attribute/BinaryAttribute.cpp new file mode 100644 index 0000000..6851eac --- /dev/null +++ b/shibsp/attribute/BinaryAttribute.cpp @@ -0,0 +1,131 @@ +/* + * Copyright 2009 Internet2 + * + * 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 + * + * 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. + */ + +/** + * BinaryAttribute.cpp + * + * An Attribute whose values are binary data. + */ + +#include "internal.h" +#include "attribute/BinaryAttribute.h" + +#include + +using namespace shibsp; +using namespace std; + +namespace shibsp { + SHIBSP_DLLLOCAL Attribute* BinaryAttributeFactory(DDF& in) { + return new BinaryAttribute(in); + } +}; + +BinaryAttribute::BinaryAttribute(const vector& ids) : Attribute(ids) +{ +} + +BinaryAttribute::BinaryAttribute(DDF& in) : Attribute(in) +{ + xsecsize_t x; + DDF val = in.first().first(); + while (val.string()) { + m_serialized.push_back(val.string()); + XMLByte* decoded=Base64::decode(reinterpret_cast(val.string()), &x); + if (decoded) { + m_values.push_back(string(reinterpret_cast(decoded), x)); +#ifdef SHIBSP_XERCESC_HAS_XMLBYTE_RELEASE + XMLString::release(&decoded); +#else + XMLString::release((char**)&decoded); +#endif + } + val = in.first().next(); + } +} + +BinaryAttribute::~BinaryAttribute() +{ +} + +vector& BinaryAttribute::getValues() +{ + return m_values; +} + +const vector& BinaryAttribute::getValues() const +{ + return m_values; +} + +size_t BinaryAttribute::valueCount() const +{ + return m_values.size(); +} + +void BinaryAttribute::clearSerializedValues() +{ + m_serialized.clear(); +} + +const char* BinaryAttribute::getString(size_t index) const +{ + return m_values[index].c_str(); +} + +void BinaryAttribute::removeValue(size_t index) +{ + Attribute::removeValue(index); + if (index < m_values.size()) + m_values.erase(m_values.begin() + index); +} + +const vector& BinaryAttribute::getSerializedValues() const +{ + xsecsize_t len; + XMLByte *pos, *pos2; + if (m_serialized.empty()) { + for (vector::const_iterator i=m_values.begin(); i!=m_values.end(); ++i) { + XMLByte* enc = Base64::encode(reinterpret_cast(i->data()), i->size(), &len); + if (enc) { + for (pos=enc, pos2=enc; *pos2; pos2++) + if (isgraph(*pos2)) + *pos++=*pos2; + *pos=0; + m_serialized.push_back(reinterpret_cast(enc)); +#ifdef SHIBSP_XERCESC_HAS_XMLBYTE_RELEASE + XMLString::release(&enc); +#else + XMLString::release((char**)&enc); +#endif + } + } + } + return Attribute::getSerializedValues(); +} + +DDF BinaryAttribute::marshall() const +{ + DDF ddf = Attribute::marshall(); + ddf.name("Binary"); + DDF vlist = ddf.first(); + const vector& encoded = getSerializedValues(); + for (vector::const_iterator i = encoded.begin(); i != encoded.end(); ++i) { + DDF val = DDF(nullptr).string(i->c_str()); + vlist.add(val); + } + return ddf; +} diff --git a/shibsp/attribute/BinaryAttribute.h b/shibsp/attribute/BinaryAttribute.h new file mode 100644 index 0000000..d837620 --- /dev/null +++ b/shibsp/attribute/BinaryAttribute.h @@ -0,0 +1,93 @@ +/* + * Copyright 2001-2009 Internet2 + * + * 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 + * + * 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. + */ + +/** + * @file shibsp/attribute/BinaryAttribute.h + * + * An Attribute whose values are binary data. + */ + +#ifndef __shibsp_binattr_h__ +#define __shibsp_binattr_h__ + +#include + +namespace shibsp { + +#if defined (_MSC_VER) + #pragma warning( push ) + #pragma warning( disable : 4251 ) +#endif + + /** + * An Attribute whose values are binary data. + * + *

Binary attributes use base64 encoding to serialize their values. + * The original binary values are accessible in the underlying value + * collection. + */ + class SHIBSP_API BinaryAttribute : public Attribute + { + public: + /** + * Constructor. + * + * @param ids array with primary identifier in first position, followed by any aliases + */ + BinaryAttribute(const std::vector& ids); + + /** + * Constructs based on a remoted BinaryAttribute. + * + * @param in input object containing marshalled BinaryAttribute + */ + BinaryAttribute(DDF& in); + + virtual ~BinaryAttribute(); + + /** + * Returns the set of raw binary values. + * + * @return a mutable vector of the values + */ + std::vector& getValues(); + + /** + * Returns the set of raw binary values. + * + * @return an immutable vector of the values + */ + const std::vector& getValues() const; + + // Virtual function overrides. + size_t valueCount() const; + void clearSerializedValues(); + const char* getString(size_t index) const; + void removeValue(size_t index); + const std::vector& getSerializedValues() const; + DDF marshall() const; + + private: + std::vector m_values; + }; + +#if defined (_MSC_VER) + #pragma warning( pop ) +#endif + +}; + +#endif /* __shibsp_scopedattr_h__ */ diff --git a/shibsp/shibsp-lite.vcxproj b/shibsp/shibsp-lite.vcxproj index 2bbd998..9296ba0 100644 --- a/shibsp/shibsp-lite.vcxproj +++ b/shibsp/shibsp-lite.vcxproj @@ -205,6 +205,7 @@ + @@ -267,6 +268,7 @@ + diff --git a/shibsp/shibsp.vcxproj b/shibsp/shibsp.vcxproj index 6f7e164..17dcd08 100644 --- a/shibsp/shibsp.vcxproj +++ b/shibsp/shibsp.vcxproj @@ -209,6 +209,7 @@ + @@ -312,6 +313,7 @@ + -- 2.1.4