2 * Copyright 2001-2010 Internet2
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
33 using namespace opensaml::saml2md;
34 using namespace opensaml;
35 using namespace xmlsignature;
37 class SAMLBindingBaseTestCase : public HTTPRequest, public HTTPResponse
40 CredentialResolver* m_creds;
41 MetadataProvider* m_metadata;
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;
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);
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(nullptr,path.get(),file.get());
71 m_metadata = SAMLConfig::getConfig().MetadataProviderManager.newPlugin(
72 XML_METADATA_PROVIDER,doc->getDocumentElement()
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()
84 m_trust = XMLToolingConfig::getConfig().TrustEngineManager.newPlugin(EXPLICIT_KEY_TRUSTENGINE, nullptr);
86 m_rules.push_back(SAMLConfig::getConfig().SecurityPolicyRuleManager.newPlugin(MESSAGEFLOW_POLICY_RULE,nullptr));
87 m_rules.push_back(SAMLConfig::getConfig().SecurityPolicyRuleManager.newPlugin(SIMPLESIGNING_POLICY_RULE,nullptr));
88 m_rules.push_back(SAMLConfig::getConfig().SecurityPolicyRuleManager.newPlugin(XMLSIGNING_POLICY_RULE,nullptr));
90 catch (XMLToolingException& ex) {
99 for_each(m_rules.begin(), m_rules.end(), xmltooling::cleanup<SecurityPolicyRule>());
114 // HTTPRequest methods
116 const char* getMethod() const {
117 return m_method.c_str();
120 const char* getScheme() const {
124 const char* getHostname() const {
128 int getPort() const {
132 string getContentType() const {
133 return "application/x-www-form-urlencoded";
136 long getContentLength() const {
140 const char* getRequestURI() const {
144 const char* getRequestURL() const {
145 return m_url.c_str();
148 const char* getRequestBody() const {
152 const char* getQueryString() const {
153 return m_query.c_str();
156 string getRemoteUser() const {
160 string getRemoteAddr() const {
164 const std::vector<XSECCryptoX509*>& getClientCertificates() const {
165 return m_clientCerts;
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;
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();
178 vector<const char*>::size_type getParameters(const char* name, vector<const char*>& values) const {
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();
186 // HTTPResponse methods
188 void setResponseHeader(const char* name, const char* value) {
189 m_headers[name] = value ? value : "";
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.
195 long sendRedirect(const char* url) {
197 char* dup = strdup(url);
198 char* pch = strchr(dup,'?');
203 while (name && *name) {
210 XMLToolingConfig::getConfig().getURLEncoder()->decode(value);
211 m_fields[name] = value;
217 return m_fields.size();
220 string html_decode(const string& s) const {
222 const char* ch=s.c_str();
225 if (!strncmp(ch,"<",4)) {
228 else if (!strncmp(ch,">",4)) {
231 else if (!strncmp(ch,""",6)) {
234 else if (*++ch=='#') {
235 decoded+=(char)atoi(++ch);
246 using HTTPResponse::sendResponse;
248 long sendResponse(std::istream& inputStream, long status) {
251 while (getline(inputStream,line))
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));
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));
265 return m_fields.size();