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.
18 * FormSessionInitiator.cpp
20 * HTML form-based IdP discovery.
24 #include "Application.h"
25 #include "exceptions.h"
26 #include "handler/AbstractHandler.h"
27 #include "handler/SessionInitiator.h"
28 #include "util/TemplateParameters.h"
31 #include <xmltooling/XMLToolingConfig.h>
32 #include <xmltooling/util/PathResolver.h>
33 #include <xmltooling/util/URLEncoder.h>
35 using namespace shibsp;
36 using namespace opensaml;
37 using namespace xmltooling;
42 #if defined (_MSC_VER)
43 #pragma warning( push )
44 #pragma warning( disable : 4250 )
47 class SHIBSP_DLLLOCAL FormSessionInitiator : public SessionInitiator, public AbstractHandler
50 FormSessionInitiator(const DOMElement* e, const char* appId)
51 : AbstractHandler(e, Category::getInstance(SHIBSP_LOGCAT".SessionInitiator.Form")), m_template(getString("template").second) {
53 throw ConfigurationException("Form SessionInitiator requires a template property.");
55 virtual ~FormSessionInitiator() {}
57 pair<bool,long> run(SPRequest& request, string& entityID, bool isHandler=true) const;
60 const char* m_template;
63 #if defined (_MSC_VER)
64 #pragma warning( pop )
67 SessionInitiator* SHIBSP_DLLLOCAL FormSessionInitiatorFactory(const pair<const DOMElement*,const char*>& p)
69 return new FormSessionInitiator(p.first, p.second);
74 pair<bool,long> FormSessionInitiator::run(SPRequest& request, string& entityID, bool isHandler) const
76 if (!checkCompatibility(request, isHandler))
77 return make_pair(false,0L);
81 const Application& app=request.getApplication();
84 option = request.getParameter("target");
87 recoverRelayState(app, request, request, target, false);
90 // We're running as a "virtual handler" from within the filter.
91 // The target resource is the current one.
92 target=request.getRequestURL();
95 // Compute the return URL. We start with a self-referential link.
96 string returnURL=request.getHandlerURL(target.c_str());
97 pair<bool,const char*> thisloc = getString("Location");
98 if (thisloc.first) returnURL += thisloc.second;
101 // We may already have RelayState set if we looped back here,
102 // but just in case target is a resource, we reset it back.
103 option = request.getParameter("target");
107 preserveRelayState(app, request, target);
109 request.setContentType("text/html");
110 request.setResponseHeader("Expires","01-Jan-1997 12:00:00 GMT");
111 request.setResponseHeader("Cache-Control","private,no-store,no-cache");
112 string fname(m_template);
113 ifstream infile(XMLToolingConfig::getConfig().getPathResolver()->resolve(fname, PathResolver::XMLTOOLING_CFG_FILE).c_str());
115 throw ConfigurationException("Unable to access HTML template ($1).", params(1, m_template));
116 TemplateParameters tp;
117 tp.m_request = &request;
118 tp.setPropertySet(app.getPropertySet("Errors"));
119 tp.m_map["action"] = returnURL;
121 tp.m_map["target"] = target;
123 XMLToolingConfig::getConfig().getTemplateEngine()->run(infile, str, tp);
124 return make_pair(true,request.sendResponse(str));