1 /**
2 * Licensed to the University Corporation for Advanced Internet
3 * Development, Inc. (UCAID) under one or more contributor license
4 * agreements. See the NOTICE file distributed with this work for
5 * additional information regarding copyright ownership.
6 *
7 * UCAID licenses this file to you under the Apache License,
8 * Version 2.0 (the "License"); you may not use this file except
9 * in compliance with the License. You may obtain a copy of the
10 * License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17 * either express or implied. See the License for the specific
18 * language governing permissions and limitations under the License.
19 */
20
21 /**
22 * CommonDomainCookie.cpp
23 *
24 * Helper class for maintaining discovery cookie.
25 */
26
27 #include "internal.h"
28 #include "util/CommonDomainCookie.h"
29
30 #include <boost/algorithm/string.hpp>
31 #include <xercesc/util/Base64.hpp>
32 #include <xsec/framework/XSECDefs.hpp>
33 #include <xmltooling/XMLToolingConfig.h>
34 #include <xmltooling/util/URLEncoder.h>
35
36 using namespace opensaml;
37 using namespace xmltooling;
38 using namespace boost;
39 using namespace std;
40
41 const char CommonDomainCookie::CDCName[] = "_saml_idp";
42
CommonDomainCookie(const char * cookie)43 CommonDomainCookie::CommonDomainCookie(const char* cookie)
44 {
45 if (!cookie)
46 return;
47
48 // Copy it so we can URL-decode it.
49 char* b64=strdup(cookie);
50 XMLToolingConfig::getConfig().getURLEncoder()->decode(b64);
51
52 // Chop it up and save off elements.
53 split(m_list, b64, is_space(), algorithm::token_compress_on);
54 free(b64);
55
56 // Remove empty elements.
57 m_list.erase(remove(m_list.begin(), m_list.end(), ""), m_list.end());
58
59 // Now Base64 decode the list elements, overwriting them.
60 XMLSize_t len;
61 for (vector<string>::iterator i = m_list.begin(); i != m_list.end(); ++i) {
62 trim(*i);
63 XMLByte* decoded=Base64::decode(reinterpret_cast<const XMLByte*>(i->c_str()),&len);
64 if (decoded && *decoded) {
65 i->assign(reinterpret_cast<char*>(decoded));
66 XMLString::release((char**)&decoded);
67 }
68 }
69 }
70
~CommonDomainCookie()71 CommonDomainCookie::~CommonDomainCookie()
72 {
73 }
74
get() const75 const vector<string>& CommonDomainCookie::get() const
76 {
77 return m_list;
78 }
79
set(const char * entityID)80 const char* CommonDomainCookie::set(const char* entityID)
81 {
82 // First remove the IdP from the list.
83 m_list.erase(remove(m_list.begin(), m_list.end(), entityID), m_list.end());
84
85 // Append it to the end.
86 m_list.push_back(entityID);
87
88 // Now rebuild the delimited list.
89 XMLSize_t len;
90 string delimited;
91 for (vector<string>::const_iterator j = m_list.begin(); j != m_list.end(); ++j) {
92
93 XMLByte* b64 = Base64::encode(reinterpret_cast<const XMLByte*>(j->c_str()), j->length(), &len);
94 if (b64) {
95 XMLByte *pos, *pos2;
96 for (pos = b64, pos2 = b64; *pos2; ++pos2)
97 if (isgraph(*pos2))
98 *pos++ = *pos2;
99 *pos = 0;
100
101 if (!delimited.empty())
102 delimited += ' ';
103 delimited += reinterpret_cast<char*>(b64);
104 XMLString::release((char**)&b64);
105 }
106 }
107
108 m_encoded = XMLToolingConfig::getConfig().getURLEncoder()->encode(delimited.c_str());
109 return m_encoded.c_str();
110 }
111