Change license header, remove stale pkg files.
[shibboleth/cpp-opensaml.git] / saml / util / CommonDomainCookie.cpp
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 <xercesc/util/Base64.hpp>
31 #include <xsec/framework/XSECDefs.hpp>
32 #include <xmltooling/XMLToolingConfig.h>
33 #include <xmltooling/util/URLEncoder.h>
34
35 using namespace opensaml;
36 using namespace xmltooling;
37 using namespace std;
38
39 const char CommonDomainCookie::CDCName[] = "_saml_idp";
40
41 CommonDomainCookie::CommonDomainCookie(const char* cookie)
42 {
43     if (!cookie)
44         return;
45
46     // Copy it so we can URL-decode it.
47     char* b64=strdup(cookie);
48     XMLToolingConfig::getConfig().getURLEncoder()->decode(b64);
49
50     // Chop it up and save off elements.
51     vector<string> templist;
52     char* ptr=b64;
53     while (*ptr) {
54         while (*ptr && isspace(*ptr)) ptr++;
55         char* end=ptr;
56         while (*end && !isspace(*end)) end++;
57         templist.push_back(string(ptr,end-ptr));
58         ptr=end;
59     }
60     free(b64);
61
62     // Now Base64 decode the list.
63     xsecsize_t len;
64     for (vector<string>::iterator i=templist.begin(); i!=templist.end(); ++i) {
65         XMLByte* decoded=Base64::decode(reinterpret_cast<const XMLByte*>(i->c_str()),&len);
66         if (decoded && *decoded) {
67             m_list.push_back(reinterpret_cast<char*>(decoded));
68 #ifdef OPENSAML_XERCESC_HAS_XMLBYTE_RELEASE
69             XMLString::release(&decoded);
70 #else
71             XMLString::release((char**)&decoded);
72 #endif
73         }
74     }
75 }
76
77 CommonDomainCookie::~CommonDomainCookie()
78 {
79 }
80
81 const vector<string>& CommonDomainCookie::get() const
82 {
83     return m_list;
84 }
85
86 const char* CommonDomainCookie::set(const char* entityID)
87 {
88     // First scan the list for this IdP.
89     for (vector<string>::iterator i=m_list.begin(); i!=m_list.end(); i++) {
90         if (*i == entityID) {
91             m_list.erase(i);
92             break;
93         }
94     }
95     
96     // Append it to the end.
97     m_list.push_back(entityID);
98     
99     // Now rebuild the delimited list.
100     xsecsize_t len;
101     string delimited;
102     for (vector<string>::const_iterator j=m_list.begin(); j!=m_list.end(); j++) {
103         if (!delimited.empty()) delimited += ' ';
104         
105         XMLByte* b64=Base64::encode(reinterpret_cast<const XMLByte*>(j->c_str()),j->length(),&len);
106         XMLByte *pos, *pos2;
107         for (pos=b64, pos2=b64; *pos2; pos2++)
108             if (isgraph(*pos2))
109                 *pos++=*pos2;
110         *pos=0;
111         
112         delimited += reinterpret_cast<char*>(b64);
113 #ifdef OPENSAML_XERCESC_HAS_XMLBYTE_RELEASE
114         XMLString::release(&b64);
115 #else
116         XMLString::release((char**)&b64);
117 #endif
118     }
119     
120     m_encoded=XMLToolingConfig::getConfig().getURLEncoder()->encode(delimited.c_str());
121     return m_encoded.c_str();
122 }