2 * Copyright 2001-2009 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/SecurityPolicy.h>
\r
23 #include <saml/binding/SecurityPolicyRule.h>
\r
24 #include <saml/saml2/metadata/Metadata.h>
\r
25 #include <saml/saml2/metadata/MetadataProvider.h>
\r
26 #include <xmltooling/io/HTTPRequest.h>
\r
27 #include <xmltooling/io/HTTPResponse.h>
\r
28 #include <xmltooling/security/Credential.h>
\r
29 #include <xmltooling/security/CredentialCriteria.h>
\r
30 #include <xmltooling/security/TrustEngine.h>
\r
31 #include <xmltooling/util/URLEncoder.h>
\r
33 using namespace opensaml::saml2md;
\r
34 using namespace opensaml;
\r
35 using namespace xmlsignature;
\r
37 class SAMLBindingBaseTestCase : public HTTPRequest, public HTTPResponse
\r
40 CredentialResolver* m_creds;
\r
41 MetadataProvider* m_metadata;
\r
42 TrustEngine* m_trust;
\r
43 map<string,string> m_fields;
\r
44 map<string,string> m_headers;
\r
45 string m_method,m_url,m_query;
\r
46 vector<XSECCryptoX509*> m_clientCerts;
\r
47 vector<const SecurityPolicyRule*> m_rules;
\r
61 string config = data_path + "binding/ExampleMetadataProvider.xml";
\r
62 ifstream in(config.c_str());
\r
63 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);
\r
64 XercesJanitor<DOMDocument> janitor(doc);
\r
66 auto_ptr_XMLCh path("path");
\r
67 string s = data_path + "binding/example-metadata.xml";
\r
68 auto_ptr_XMLCh file(s.c_str());
\r
69 doc->getDocumentElement()->setAttributeNS(NULL,path.get(),file.get());
\r
71 m_metadata = SAMLConfig::getConfig().MetadataProviderManager.newPlugin(
\r
72 XML_METADATA_PROVIDER,doc->getDocumentElement()
\r
76 config = data_path + "FilesystemCredentialResolver.xml";
\r
77 ifstream in2(config.c_str());
\r
78 DOMDocument* doc2=XMLToolingConfig::getConfig().getParser().parse(in2);
\r
79 XercesJanitor<DOMDocument> janitor2(doc2);
\r
80 m_creds = XMLToolingConfig::getConfig().CredentialResolverManager.newPlugin(
\r
81 FILESYSTEM_CREDENTIAL_RESOLVER,doc2->getDocumentElement()
\r
84 m_trust = XMLToolingConfig::getConfig().TrustEngineManager.newPlugin(EXPLICIT_KEY_TRUSTENGINE, NULL);
\r
86 m_rules.push_back(SAMLConfig::getConfig().SecurityPolicyRuleManager.newPlugin(MESSAGEFLOW_POLICY_RULE,NULL));
\r
87 m_rules.push_back(SAMLConfig::getConfig().SecurityPolicyRuleManager.newPlugin(SIMPLESIGNING_POLICY_RULE,NULL));
\r
88 m_rules.push_back(SAMLConfig::getConfig().SecurityPolicyRuleManager.newPlugin(XMLSIGNING_POLICY_RULE,NULL));
\r
90 catch (XMLToolingException& ex) {
\r
91 TS_TRACE(ex.what());
\r
99 for_each(m_rules.begin(), m_rules.end(), xmltooling::cleanup<SecurityPolicyRule>());
\r
114 // HTTPRequest methods
\r
116 const char* getMethod() const {
\r
117 return m_method.c_str();
\r
120 const char* getScheme() const {
\r
124 const char* getHostname() const {
\r
125 return "localhost";
\r
128 int getPort() const {
\r
132 string getContentType() const {
\r
133 return "application/x-www-form-urlencoded";
\r
136 long getContentLength() const {
\r
140 const char* getRequestURI() const {
\r
144 const char* getRequestURL() const {
\r
145 return m_url.c_str();
\r
148 const char* getRequestBody() const {
\r
152 const char* getQueryString() const {
\r
153 return m_query.c_str();
\r
156 string getRemoteUser() const {
\r
160 string getRemoteAddr() const {
\r
161 return "127.0.0.1";
\r
164 const std::vector<XSECCryptoX509*>& getClientCertificates() const {
\r
165 return m_clientCerts;
\r
168 string getHeader(const char* name) const {
\r
169 map<string,string>::const_iterator i=m_headers.find(name);
\r
170 return i==m_headers.end() ? "" : i->second;
\r
173 const char* getParameter(const char* name) const {
\r
174 map<string,string>::const_iterator i=m_fields.find(name);
\r
175 return i==m_fields.end() ? NULL : i->second.c_str();
\r
178 vector<const char*>::size_type getParameters(const char* name, vector<const char*>& values) const {
\r
180 map<string,string>::const_iterator i=m_fields.find(name);
\r
181 if (i!=m_fields.end())
\r
182 values.push_back(i->second.c_str());
\r
183 return values.size();
\r
186 // HTTPResponse methods
\r
188 void setResponseHeader(const char* name, const char* value) {
\r
189 m_headers[name] = value ? value : "";
\r
192 // The amount of error checking missing from this is incredible, but as long
\r
193 // as the test data isn't unexpected or malformed, it should work.
\r
195 long sendRedirect(const char* url) {
\r
197 char* dup = strdup(url);
\r
198 char* pch = strchr(dup,'?');
\r
203 while (name && *name) {
\r
204 pch=strchr(pch,'=');
\r
207 pch=strchr(pch,'&');
\r
210 XMLToolingConfig::getConfig().getURLEncoder()->decode(value);
\r
211 m_fields[name] = value;
\r
217 return m_fields.size();
\r
220 string html_decode(const string& s) const {
\r
222 const char* ch=s.c_str();
\r
225 if (!strncmp(ch,"<",4)) {
\r
226 decoded+='<'; ch+=4;
\r
228 else if (!strncmp(ch,">",4)) {
\r
229 decoded+='>'; ch+=4;
\r
231 else if (!strncmp(ch,""",6)) {
\r
232 decoded+='"'; ch+=6;
\r
234 else if (*++ch=='#') {
\r
235 decoded+=(char)atoi(++ch);
\r
236 ch=strchr(ch,';')+1;
\r
246 using HTTPResponse::sendResponse;
\r
248 long sendResponse(std::istream& inputStream, long status) {
\r
251 while (getline(inputStream,line))
\r
252 page += line + '\n';
\r
254 const char* pch=strstr(page.c_str(),"action=\"");
\r
255 pch+=strlen("action=\"");
\r
256 m_url = html_decode(page.substr(pch-page.c_str(),strchr(pch,'"')-pch));
\r
258 while (pch=strstr(pch,"<input type=\"hidden\" name=\"")) {
\r
259 pch+=strlen("<input type=\"hidden\" name=\"");
\r
260 string name = page.substr(pch-page.c_str(),strchr(pch,'"')-pch);
\r
261 pch=strstr(pch,"value=\"");
\r
262 pch+=strlen("value=\"");
\r
263 m_fields[name] = html_decode(page.substr(pch-page.c_str(),strchr(pch,'"')-pch));
\r
265 return m_fields.size();
\r