2 * Copyright 2001-2007 Internet2
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
17 #include "internal.h"
\r
19 #include <saml/SAMLConfig.h>
\r
20 #include <saml/binding/MessageDecoder.h>
\r
21 #include <saml/binding/MessageEncoder.h>
\r
22 #include <saml/binding/SecurityPolicyRule.h>
\r
23 #include <saml/saml2/metadata/Metadata.h>
\r
24 #include <saml/saml2/metadata/MetadataProvider.h>
\r
25 #include <xmltooling/io/HTTPRequest.h>
\r
26 #include <xmltooling/io/HTTPResponse.h>
\r
27 #include <xmltooling/security/TrustEngine.h>
\r
28 #include <xmltooling/util/URLEncoder.h>
\r
30 using namespace opensaml::saml2md;
\r
31 using namespace opensaml;
\r
32 using namespace xmlsignature;
\r
34 class SAMLBindingBaseTestCase : public HTTPRequest, public HTTPResponse
\r
37 CredentialResolver* m_creds;
\r
38 MetadataProvider* m_metadata;
\r
39 TrustEngine* m_trust;
\r
40 map<string,string> m_fields;
\r
41 map<string,string> m_headers;
\r
42 string m_method,m_url,m_query;
\r
43 vector<XSECCryptoX509*> m_clientCerts;
\r
44 vector<const SecurityPolicyRule*> m_rules;
\r
58 string config = data_path + "binding/ExampleMetadataProvider.xml";
\r
59 ifstream in(config.c_str());
\r
60 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);
\r
61 XercesJanitor<DOMDocument> janitor(doc);
\r
63 auto_ptr_XMLCh path("path");
\r
64 string s = data_path + "binding/example-metadata.xml";
\r
65 auto_ptr_XMLCh file(s.c_str());
\r
66 doc->getDocumentElement()->setAttributeNS(NULL,path.get(),file.get());
\r
68 m_metadata = SAMLConfig::getConfig().MetadataProviderManager.newPlugin(
\r
69 XML_METADATA_PROVIDER,doc->getDocumentElement()
\r
73 config = data_path + "FilesystemCredentialResolver.xml";
\r
74 ifstream in2(config.c_str());
\r
75 DOMDocument* doc2=XMLToolingConfig::getConfig().getParser().parse(in2);
\r
76 XercesJanitor<DOMDocument> janitor2(doc2);
\r
77 m_creds = XMLToolingConfig::getConfig().CredentialResolverManager.newPlugin(
\r
78 FILESYSTEM_CREDENTIAL_RESOLVER,doc2->getDocumentElement()
\r
81 m_trust = XMLToolingConfig::getConfig().TrustEngineManager.newPlugin(EXPLICIT_KEY_TRUSTENGINE, NULL);
\r
83 m_rules.push_back(SAMLConfig::getConfig().SecurityPolicyRuleManager.newPlugin(MESSAGEFLOW_POLICY_RULE,NULL));
\r
84 m_rules.push_back(SAMLConfig::getConfig().SecurityPolicyRuleManager.newPlugin(SIMPLESIGNING_POLICY_RULE,NULL));
\r
85 m_rules.push_back(SAMLConfig::getConfig().SecurityPolicyRuleManager.newPlugin(XMLSIGNING_POLICY_RULE,NULL));
\r
87 catch (XMLToolingException& ex) {
\r
88 TS_TRACE(ex.what());
\r
96 for_each(m_rules.begin(), m_rules.end(), xmltooling::cleanup<SecurityPolicyRule>());
\r
111 // HTTPRequest methods
\r
113 const char* getMethod() const {
\r
114 return m_method.c_str();
\r
117 const char* getScheme() const {
\r
121 const char* getHostname() const {
\r
122 return "localhost";
\r
125 int getPort() const {
\r
129 string getContentType() const {
\r
130 return "application/x-www-form-urlencoded";
\r
133 long getContentLength() const {
\r
137 const char* getRequestURI() const {
\r
141 const char* getRequestURL() const {
\r
142 return m_url.c_str();
\r
145 const char* getRequestBody() const {
\r
149 const char* getQueryString() const {
\r
150 return m_query.c_str();
\r
153 string getRemoteUser() const {
\r
157 string getRemoteAddr() const {
\r
158 return "127.0.0.1";
\r
161 const std::vector<XSECCryptoX509*>& getClientCertificates() const {
\r
162 return m_clientCerts;
\r
165 string getHeader(const char* name) const {
\r
166 map<string,string>::const_iterator i=m_headers.find(name);
\r
167 return i==m_headers.end() ? "" : i->second;
\r
170 const char* getParameter(const char* name) const {
\r
171 map<string,string>::const_iterator i=m_fields.find(name);
\r
172 return i==m_fields.end() ? NULL : i->second.c_str();
\r
175 vector<const char*>::size_type getParameters(const char* name, vector<const char*>& values) const {
\r
177 map<string,string>::const_iterator i=m_fields.find(name);
\r
178 if (i!=m_fields.end())
\r
179 values.push_back(i->second.c_str());
\r
180 return values.size();
\r
183 // HTTPResponse methods
\r
185 void setResponseHeader(const char* name, const char* value) {
\r
186 m_headers[name] = value ? value : "";
\r
189 // The amount of error checking missing from this is incredible, but as long
\r
190 // as the test data isn't unexpected or malformed, it should work.
\r
192 long sendRedirect(const char* url) {
\r
194 char* dup = strdup(url);
\r
195 char* pch = strchr(dup,'?');
\r
200 while (name && *name) {
\r
201 pch=strchr(pch,'=');
\r
204 pch=strchr(pch,'&');
\r
207 XMLToolingConfig::getConfig().getURLEncoder()->decode(value);
\r
208 m_fields[name] = value;
\r
214 return m_fields.size();
\r
217 string html_decode(const string& s) const {
\r
219 const char* ch=s.c_str();
\r
222 if (!strncmp(ch,"<",4)) {
\r
223 decoded+='<'; ch+=4;
\r
225 else if (!strncmp(ch,">",4)) {
\r
226 decoded+='>'; ch+=4;
\r
228 else if (!strncmp(ch,""",6)) {
\r
229 decoded+='"'; ch+=6;
\r
231 else if (*++ch=='#') {
\r
232 decoded+=(char)atoi(++ch);
\r
233 ch=strchr(ch,';')+1;
\r
243 using HTTPResponse::sendResponse;
\r
245 long sendResponse(std::istream& inputStream, long status) {
\r
248 while (getline(inputStream,line))
\r
249 page += line + '\n';
\r
251 const char* pch=strstr(page.c_str(),"action=\"");
\r
252 pch+=strlen("action=\"");
\r
253 m_url = html_decode(page.substr(pch-page.c_str(),strchr(pch,'"')-pch));
\r
255 while (pch=strstr(pch,"<input type=\"hidden\" name=\"")) {
\r
256 pch+=strlen("<input type=\"hidden\" name=\"");
\r
257 string name = page.substr(pch-page.c_str(),strchr(pch,'"')-pch);
\r
258 pch=strstr(pch,"value=\"");
\r
259 pch+=strlen("value=\"");
\r
260 m_fields[name] = html_decode(page.substr(pch-page.c_str(),strchr(pch,'"')-pch));
\r
262 return m_fields.size();
\r