a93d40dde4c1096799c383ad01354fcb74f9d6c2
[shibboleth/cpp-opensaml.git] / samltest / binding.h
1 /*
2  *  Copyright 2001-2009 Internet2
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "internal.h"
18
19 #include <saml/SAMLConfig.h>
20 #include <saml/binding/MessageDecoder.h>
21 #include <saml/binding/MessageEncoder.h>
22 #include <saml/binding/SecurityPolicy.h>
23 #include <saml/binding/SecurityPolicyRule.h>
24 #include <saml/saml2/metadata/Metadata.h>
25 #include <saml/saml2/metadata/MetadataProvider.h>
26 #include <xmltooling/io/HTTPRequest.h>
27 #include <xmltooling/io/HTTPResponse.h>
28 #include <xmltooling/security/Credential.h>
29 #include <xmltooling/security/CredentialCriteria.h>
30 #include <xmltooling/security/TrustEngine.h>
31 #include <xmltooling/util/URLEncoder.h>
32
33 using namespace opensaml::saml2md;
34 using namespace opensaml;
35 using namespace xmlsignature;
36
37 class SAMLBindingBaseTestCase : public HTTPRequest, public HTTPResponse
38 {
39 protected:
40     CredentialResolver* m_creds; 
41     MetadataProvider* m_metadata;
42     TrustEngine* m_trust;
43     map<string,string> m_fields;
44     map<string,string> m_headers;
45     string m_method,m_url,m_query;
46     vector<XSECCryptoX509*> m_clientCerts;
47     vector<const SecurityPolicyRule*> m_rules;
48
49 public:
50     void setUp() {
51         m_creds=NULL;
52         m_metadata=NULL;
53         m_trust=NULL;
54         m_fields.clear();
55         m_headers.clear();
56         m_method.erase();
57         m_url.erase();
58         m_query.erase();
59
60         try {
61             string config = data_path + "binding/ExampleMetadataProvider.xml";
62             ifstream in(config.c_str());
63             DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);
64             XercesJanitor<DOMDocument> janitor(doc);
65     
66             auto_ptr_XMLCh path("path");
67             string s = data_path + "binding/example-metadata.xml";
68             auto_ptr_XMLCh file(s.c_str());
69             doc->getDocumentElement()->setAttributeNS(NULL,path.get(),file.get());
70     
71             m_metadata = SAMLConfig::getConfig().MetadataProviderManager.newPlugin(
72                 XML_METADATA_PROVIDER,doc->getDocumentElement()
73                 );
74             m_metadata->init();
75
76             config = data_path + "FilesystemCredentialResolver.xml";
77             ifstream in2(config.c_str());
78             DOMDocument* doc2=XMLToolingConfig::getConfig().getParser().parse(in2);
79             XercesJanitor<DOMDocument> janitor2(doc2);
80             m_creds = XMLToolingConfig::getConfig().CredentialResolverManager.newPlugin(
81                 FILESYSTEM_CREDENTIAL_RESOLVER,doc2->getDocumentElement()
82                 );
83                 
84             m_trust = XMLToolingConfig::getConfig().TrustEngineManager.newPlugin(EXPLICIT_KEY_TRUSTENGINE, NULL);
85
86             m_rules.push_back(SAMLConfig::getConfig().SecurityPolicyRuleManager.newPlugin(MESSAGEFLOW_POLICY_RULE,NULL));
87             m_rules.push_back(SAMLConfig::getConfig().SecurityPolicyRuleManager.newPlugin(SIMPLESIGNING_POLICY_RULE,NULL));
88             m_rules.push_back(SAMLConfig::getConfig().SecurityPolicyRuleManager.newPlugin(XMLSIGNING_POLICY_RULE,NULL));
89         }
90         catch (XMLToolingException& ex) {
91             TS_TRACE(ex.what());
92             tearDown();
93             throw;
94         }
95
96     }
97     
98     void tearDown() {
99         for_each(m_rules.begin(), m_rules.end(), xmltooling::cleanup<SecurityPolicyRule>());
100         m_rules.clear();
101         delete m_creds;
102         delete m_metadata;
103         delete m_trust;
104         m_creds=NULL;
105         m_metadata=NULL;
106         m_trust=NULL;
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 NULL;
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() ? NULL : 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 };