Update gitignore to account for subdirs / missing files.
[shibboleth/cpp-opensaml.git] / samltest / binding.h
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 #include "internal.h"
22
23 #include <saml/SAMLConfig.h>
24 #include <saml/binding/MessageDecoder.h>
25 #include <saml/binding/MessageEncoder.h>
26 #include <saml/binding/SecurityPolicy.h>
27 #include <saml/binding/SecurityPolicyRule.h>
28 #include <saml/saml2/metadata/Metadata.h>
29 #include <saml/saml2/metadata/MetadataProvider.h>
30
31 #include <boost/scoped_ptr.hpp>
32 #include <xmltooling/io/HTTPRequest.h>
33 #include <xmltooling/io/HTTPResponse.h>
34 #include <xmltooling/security/Credential.h>
35 #include <xmltooling/security/CredentialCriteria.h>
36 #include <xmltooling/security/TrustEngine.h>
37 #include <xmltooling/util/URLEncoder.h>
38
39 using namespace opensaml::saml2md;
40 using namespace opensaml;
41 using namespace xmlsignature;
42
43 class SAMLBindingBaseTestCase : public HTTPRequest, public HTTPResponse
44 {
45 protected:
46     boost::scoped_ptr<CredentialResolver> m_creds;
47     boost::scoped_ptr<MetadataProvider> m_metadata;
48     boost::scoped_ptr<TrustEngine> m_trust;
49     map<string,string> m_fields;
50     map<string,string> m_headers;
51     string m_method,m_url,m_query;
52     vector<XSECCryptoX509*> m_clientCerts;
53     vector<const SecurityPolicyRule*> m_rules;
54
55 public:
56     void setUp() {
57         m_fields.clear();
58         m_headers.clear();
59         m_method.erase();
60         m_url.erase();
61         m_query.erase();
62
63         try {
64             string config = data_path + "binding/ExampleMetadataProvider.xml";
65             ifstream in(config.c_str());
66             DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);
67             XercesJanitor<DOMDocument> janitor(doc);
68     
69             auto_ptr_XMLCh path("path");
70             string s = data_path + "binding/example-metadata.xml";
71             auto_ptr_XMLCh file(s.c_str());
72             doc->getDocumentElement()->setAttributeNS(nullptr,path.get(),file.get());
73     
74             m_metadata.reset(
75                 SAMLConfig::getConfig().MetadataProviderManager.newPlugin(XML_METADATA_PROVIDER, doc->getDocumentElement())
76                 );
77             m_metadata->init();
78
79             config = data_path + "FilesystemCredentialResolver.xml";
80             ifstream in2(config.c_str());
81             DOMDocument* doc2=XMLToolingConfig::getConfig().getParser().parse(in2);
82             XercesJanitor<DOMDocument> janitor2(doc2);
83             m_creds.reset(
84                 XMLToolingConfig::getConfig().CredentialResolverManager.newPlugin(FILESYSTEM_CREDENTIAL_RESOLVER, doc2->getDocumentElement())
85                 );
86                 
87             m_trust.reset(XMLToolingConfig::getConfig().TrustEngineManager.newPlugin(EXPLICIT_KEY_TRUSTENGINE, nullptr));
88
89             m_rules.push_back(SAMLConfig::getConfig().SecurityPolicyRuleManager.newPlugin(MESSAGEFLOW_POLICY_RULE,nullptr));
90             m_rules.push_back(SAMLConfig::getConfig().SecurityPolicyRuleManager.newPlugin(SIMPLESIGNING_POLICY_RULE,nullptr));
91             m_rules.push_back(SAMLConfig::getConfig().SecurityPolicyRuleManager.newPlugin(XMLSIGNING_POLICY_RULE,nullptr));
92         }
93         catch (XMLToolingException& ex) {
94             TS_TRACE(ex.what());
95             tearDown();
96             throw;
97         }
98
99     }
100     
101     void tearDown() {
102         for_each(m_rules.begin(), m_rules.end(), xmltooling::cleanup<SecurityPolicyRule>());
103         m_trust.reset();
104         m_metadata.reset();
105         m_creds.reset();
106         m_rules.clear();
107         m_fields.clear();
108         m_headers.clear();
109         m_method.erase();
110         m_url.erase();
111         m_query.erase();
112     }
113
114     // HTTPRequest methods
115
116     const char* getMethod() const {
117         return m_method.c_str();
118     }
119
120     const char* getScheme() const {
121         return "https";
122     }
123
124     const char* getHostname() const {
125         return "localhost";
126     }
127
128     int getPort() const {
129         return 443;
130     }
131
132     string getContentType() const {
133         return "application/x-www-form-urlencoded";
134     }
135
136     long getContentLength() const {
137         return -1;
138     }
139
140     const char* getRequestURI() const {
141         return "/";
142     }
143
144     const char* getRequestURL() const {
145         return m_url.c_str();
146     }
147     
148     const char* getRequestBody() const {
149         return nullptr;
150     }
151     
152     const char* getQueryString() const {
153         return m_query.c_str();
154     }
155     
156     string getRemoteUser() const {
157         return "";
158     }
159
160     string getRemoteAddr() const {
161         return "127.0.0.1";
162     }
163
164     const std::vector<XSECCryptoX509*>& getClientCertificates() const {
165         return m_clientCerts;
166     }
167
168     string getHeader(const char* name) const {
169         map<string,string>::const_iterator i=m_headers.find(name);
170         return i==m_headers.end() ? "" : i->second;
171     }
172     
173     const char* getParameter(const char* name) const {
174         map<string,string>::const_iterator i=m_fields.find(name);
175         return i==m_fields.end() ? nullptr : i->second.c_str();
176     }
177
178     vector<const char*>::size_type getParameters(const char* name, vector<const char*>& values) const {
179         values.clear();
180         map<string,string>::const_iterator i=m_fields.find(name);
181         if (i!=m_fields.end())
182             values.push_back(i->second.c_str());
183         return values.size();
184     }
185     
186     // HTTPResponse methods
187     
188     void setResponseHeader(const char* name, const char* value) {
189         m_headers[name] = value ? value : "";
190     }
191
192     // The amount of error checking missing from this is incredible, but as long
193     // as the test data isn't unexpected or malformed, it should work.
194     
195     long sendRedirect(const char* url) {
196         m_method = "GET";
197         char* dup = strdup(url);
198         char* pch = strchr(dup,'?');
199         if (pch) {
200             *pch++=0;
201             m_query = pch;
202             char* name=pch;
203             while (name && *name) {
204                 pch=strchr(pch,'=');
205                 *pch++=0;
206                 char* value=pch;
207                 pch=strchr(pch,'&');
208                 if (pch)
209                     *pch++=0;
210                 XMLToolingConfig::getConfig().getURLEncoder()->decode(value);
211                 m_fields[name] = value;
212                 name = pch; 
213             }
214         }
215         m_url = dup;
216         free(dup);
217         return m_fields.size();
218     }
219     
220     string html_decode(const string& s) const {
221         string decoded;
222         const char* ch=s.c_str();
223         while (*ch) {
224             if (*ch=='&') {
225                 if (!strncmp(ch,"&lt;",4)) {
226                     decoded+='<'; ch+=4;
227                 }
228                 else if (!strncmp(ch,"&gt;",4)) {
229                     decoded+='>'; ch+=4;
230                 }
231                 else if (!strncmp(ch,"&quot;",6)) {
232                     decoded+='"'; ch+=6;
233                 }
234                 else if (*++ch=='#') {
235                     decoded+=(char)atoi(++ch);
236                     ch=strchr(ch,';')+1;
237                 }
238             }
239             else {
240                 decoded+=*ch++;
241             }
242         }
243         return decoded;
244     }
245     
246     using HTTPResponse::sendResponse;
247
248     long sendResponse(std::istream& inputStream, long status) {
249         m_method="POST";
250         string page,line;
251         while (getline(inputStream,line))
252             page += line + '\n';
253             
254         const char* pch=strstr(page.c_str(),"action=\"");
255         pch+=strlen("action=\"");
256         m_url = html_decode(page.substr(pch-page.c_str(),strchr(pch,'"')-pch));
257
258         while ((pch = strstr(pch,"<input type=\"hidden\" name=\""))) {
259             pch+=strlen("<input type=\"hidden\" name=\"");
260             string name = page.substr(pch-page.c_str(),strchr(pch,'"')-pch);
261             pch=strstr(pch,"value=\"");
262             pch+=strlen("value=\"");
263             m_fields[name] = html_decode(page.substr(pch-page.c_str(),strchr(pch,'"')-pch));
264         }
265         return m_fields.size();
266     }
267 };