--- /dev/null
+/*\r
+ * Copyright 2001-2005 Internet2\r
+ * \r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+\r
+/**\r
+ * CommonDomainCookie.cpp\r
+ * \r
+ * Helper class for maintaining discovery cookie. \r
+ */\r
+\r
+#include "internal.h"\r
+#include "binding/URLEncoder.h"\r
+#include "util/CommonDomainCookie.h"\r
+\r
+#include <xercesc/util/Base64.hpp>\r
+\r
+using namespace opensaml;\r
+using namespace std;\r
+\r
+const char CommonDomainCookie::CDCName[] = "_saml_idp";\r
+\r
+CommonDomainCookie::CommonDomainCookie(const char* cookie)\r
+{\r
+ if (!cookie)\r
+ return;\r
+\r
+ // Copy it so we can URL-decode it.\r
+ char* b64=strdup(cookie);\r
+ SAMLConfig::getConfig().getURLEncoder()->decode(b64);\r
+\r
+ // Chop it up and save off elements.\r
+ vector<string> templist;\r
+ char* ptr=b64;\r
+ while (*ptr) {\r
+ while (*ptr && isspace(*ptr)) ptr++;\r
+ char* end=ptr;\r
+ while (*end && !isspace(*end)) end++;\r
+ templist.push_back(string(ptr,end-ptr));\r
+ ptr=end;\r
+ }\r
+ free(b64);\r
+\r
+ // Now Base64 decode the list.\r
+ unsigned int len;\r
+ for (vector<string>::iterator i=templist.begin(); i!=templist.end(); ++i) {\r
+ XMLByte* decoded=Base64::decode(reinterpret_cast<const XMLByte*>(i->c_str()),&len);\r
+ if (decoded && *decoded) {\r
+ m_list.push_back(reinterpret_cast<char*>(decoded));\r
+ XMLString::release(&decoded);\r
+ }\r
+ }\r
+}\r
+\r
+const char* CommonDomainCookie::set(const char* entityID)\r
+{\r
+ // First scan the list for this IdP.\r
+ for (vector<string>::iterator i=m_list.begin(); i!=m_list.end(); i++) {\r
+ if (*i == entityID) {\r
+ m_list.erase(i);\r
+ break;\r
+ }\r
+ }\r
+ \r
+ // Append it to the end.\r
+ m_list.push_back(entityID);\r
+ \r
+ // Now rebuild the delimited list.\r
+ unsigned int len;\r
+ string delimited;\r
+ for (vector<string>::const_iterator j=m_list.begin(); j!=m_list.end(); j++) {\r
+ if (!delimited.empty()) delimited += ' ';\r
+ \r
+ XMLByte* b64=Base64::encode(reinterpret_cast<const XMLByte*>(j->c_str()),j->length(),&len);\r
+ XMLByte *pos, *pos2;\r
+ for (pos=b64, pos2=b64; *pos2; pos2++)\r
+ if (isgraph(*pos2))\r
+ *pos++=*pos2;\r
+ *pos=0;\r
+ \r
+ delimited += reinterpret_cast<char*>(b64);\r
+ XMLString::release(&b64);\r
+ }\r
+ \r
+ m_encoded=SAMLConfig::getConfig().getURLEncoder()->encode(delimited.c_str());\r
+ return m_encoded.c_str();\r
+}\r
--- /dev/null
+/*
+ * 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 <saml/base.h>
+
+#include <string>
+#include <vector>
+
+#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<std::string>& 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<std::string> m_list;
+ };
+};
+
+#if defined (_MSC_VER)
+ #pragma warning( pop )
+#endif
+
+#endif /* __saml_cdc_h__ */