From 3a0c599bbed7473a4e0f3a085e18d88cce2c2a6a Mon Sep 17 00:00:00 2001 From: Scott Cantor Date: Thu, 30 Nov 2006 21:48:19 +0000 Subject: [PATCH] Copied in CDC class from Shib --- saml/Makefile.am | 2 + saml/saml.vcproj | 8 ++++ saml/util/CommonDomainCookie.cpp | 98 ++++++++++++++++++++++++++++++++++++++++ saml/util/CommonDomainCookie.h | 82 +++++++++++++++++++++++++++++++++ 4 files changed, 190 insertions(+) create mode 100644 saml/util/CommonDomainCookie.cpp create mode 100644 saml/util/CommonDomainCookie.h diff --git a/saml/Makefile.am b/saml/Makefile.am index 3839885..ccb114f 100644 --- a/saml/Makefile.am +++ b/saml/Makefile.am @@ -58,6 +58,7 @@ siginclude_HEADERS = \ signature/SignatureProfileValidator.h utilinclude_HEADERS = \ + util/CommonDomainCookie.h \ util/SAMLConstants.h saml1coreinclude_HEADERS = \ @@ -160,6 +161,7 @@ libsaml_la_SOURCES = \ encryption/EncryptedKeyResolver.cpp \ signature/ContentReference.cpp \ signature/SignatureProfileValidator.cpp \ + util/CommonDomainCookie.cpp \ util/SAMLConstants.cpp # this is different from the project version diff --git a/saml/saml.vcproj b/saml/saml.vcproj index 3b83e31..691a2dd 100644 --- a/saml/saml.vcproj +++ b/saml/saml.vcproj @@ -191,6 +191,10 @@ Name="util" > + + @@ -581,6 +585,10 @@ Name="util" > + + diff --git a/saml/util/CommonDomainCookie.cpp b/saml/util/CommonDomainCookie.cpp new file mode 100644 index 0000000..3c82cf8 --- /dev/null +++ b/saml/util/CommonDomainCookie.cpp @@ -0,0 +1,98 @@ +/* + * Copyright 2001-2005 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. + */ + +/** + * CommonDomainCookie.cpp + * + * Helper class for maintaining discovery cookie. + */ + +#include "internal.h" +#include "binding/URLEncoder.h" +#include "util/CommonDomainCookie.h" + +#include + +using namespace opensaml; +using namespace std; + +const char CommonDomainCookie::CDCName[] = "_saml_idp"; + +CommonDomainCookie::CommonDomainCookie(const char* cookie) +{ + if (!cookie) + return; + + // Copy it so we can URL-decode it. + char* b64=strdup(cookie); + SAMLConfig::getConfig().getURLEncoder()->decode(b64); + + // Chop it up and save off elements. + vector templist; + char* ptr=b64; + while (*ptr) { + while (*ptr && isspace(*ptr)) ptr++; + char* end=ptr; + while (*end && !isspace(*end)) end++; + templist.push_back(string(ptr,end-ptr)); + ptr=end; + } + free(b64); + + // Now Base64 decode the list. + unsigned int len; + for (vector::iterator i=templist.begin(); i!=templist.end(); ++i) { + XMLByte* decoded=Base64::decode(reinterpret_cast(i->c_str()),&len); + if (decoded && *decoded) { + m_list.push_back(reinterpret_cast(decoded)); + XMLString::release(&decoded); + } + } +} + +const char* CommonDomainCookie::set(const char* entityID) +{ + // First scan the list for this IdP. + for (vector::iterator i=m_list.begin(); i!=m_list.end(); i++) { + if (*i == entityID) { + m_list.erase(i); + break; + } + } + + // Append it to the end. + m_list.push_back(entityID); + + // Now rebuild the delimited list. + unsigned int len; + string delimited; + for (vector::const_iterator j=m_list.begin(); j!=m_list.end(); j++) { + if (!delimited.empty()) delimited += ' '; + + XMLByte* b64=Base64::encode(reinterpret_cast(j->c_str()),j->length(),&len); + XMLByte *pos, *pos2; + for (pos=b64, pos2=b64; *pos2; pos2++) + if (isgraph(*pos2)) + *pos++=*pos2; + *pos=0; + + delimited += reinterpret_cast(b64); + XMLString::release(&b64); + } + + m_encoded=SAMLConfig::getConfig().getURLEncoder()->encode(delimited.c_str()); + return m_encoded.c_str(); +} diff --git a/saml/util/CommonDomainCookie.h b/saml/util/CommonDomainCookie.h new file mode 100644 index 0000000..ec94486 --- /dev/null +++ b/saml/util/CommonDomainCookie.h @@ -0,0 +1,82 @@ +/* + * Copyright 2001-2006 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 saml/util/CommonDomainCookie.h + * + * Helper class for maintaining discovery cookie. + */ + +#ifndef __saml_cdc_h__ +#define __saml_cdc_h__ + +#include + +#include +#include + +#if defined (_MSC_VER) + #pragma warning( push ) + #pragma warning( disable : 4250 4251 ) +#endif + +namespace opensaml { + /** + * Helper class for maintaining discovery cookie. + */ + class SAML_API CommonDomainCookie { + MAKE_NONCOPYABLE(CommonDomainCookie); + public: + /** + * Parses a cookie for reading or writing. + * + * @param cookie the raw cookie value + */ + CommonDomainCookie(const char* cookie); + + ~CommonDomainCookie() {} + + /** + * Returns list of IdPs stored in cookie. + * + * @return reference to vector of entityIDs + */ + const std::vector& get() const { + return m_list; + } + + /** + * Adds/moves an IdP to the front of the list. + * + * @param entityID name of IdP to add + * @return new value of cookie + */ + const char* set(const char* entityID); + + /** Name of cookie ("_saml_idp") */ + static const char CDCName[]; + + private: + std::string m_encoded; + std::vector m_list; + }; +}; + +#if defined (_MSC_VER) + #pragma warning( pop ) +#endif + +#endif /* __saml_cdc_h__ */ -- 2.1.4