Form generator for discovery along with sample form.
[shibboleth/sp.git] / shibsp / handler / impl / FormSessionInitiator.cpp
1 /*
2  *  Copyright 2001-2007 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 /**
18  * FormSessionInitiator.cpp
19  * 
20  * HTML form-based IdP discovery.
21  */
22
23 #include "internal.h"
24 #include "Application.h"
25 #include "exceptions.h"
26 #include "SPRequest.h"
27 #include "handler/AbstractHandler.h"
28 #include "handler/SessionInitiator.h"
29 #include "util/TemplateParameters.h"
30
31 #include <fstream>
32 #include <xmltooling/XMLToolingConfig.h>
33 #include <xmltooling/util/PathResolver.h>
34 #include <xmltooling/util/URLEncoder.h>
35
36 using namespace shibsp;
37 using namespace opensaml;
38 using namespace xmltooling;
39 using namespace std;
40
41 namespace shibsp {
42
43 #if defined (_MSC_VER)
44     #pragma warning( push )
45     #pragma warning( disable : 4250 )
46 #endif
47
48     class SHIBSP_DLLLOCAL FormSessionInitiator : public SessionInitiator, public AbstractHandler
49     {
50     public:
51         FormSessionInitiator(const DOMElement* e, const char* appId)
52             : AbstractHandler(e, Category::getInstance(SHIBSP_LOGCAT".SessionInitiator.Form")), m_template(getString("template").second) {
53             if (!m_template)
54                 throw ConfigurationException("Form SessionInitiator requires a template property.");
55         }
56         virtual ~FormSessionInitiator() {}
57         
58         pair<bool,long> run(SPRequest& request, string& entityID, bool isHandler=true) const;
59
60     private:
61         const char* m_template;
62     };
63
64 #if defined (_MSC_VER)
65     #pragma warning( pop )
66 #endif
67
68     SessionInitiator* SHIBSP_DLLLOCAL FormSessionInitiatorFactory(const pair<const DOMElement*,const char*>& p)
69     {
70         return new FormSessionInitiator(p.first, p.second);
71     }
72
73 };
74
75 pair<bool,long> FormSessionInitiator::run(SPRequest& request, string& entityID, bool isHandler) const
76 {
77     string target;
78     const char* option;
79     bool isPassive=false;
80     const Application& app=request.getApplication();
81
82     if (isHandler) {
83         option = request.getParameter("target");
84         if (option)
85             target = option;
86         recoverRelayState(request.getApplication(), request, request, target, false);
87     }
88     else {
89         // We're running as a "virtual handler" from within the filter.
90         // The target resource is the current one.
91         target=request.getRequestURL();
92     }
93
94     // Compute the return URL. We start with a self-referential link.
95     string returnURL=request.getHandlerURL(target.c_str());
96     pair<bool,const char*> thisloc = getString("Location");
97     if (thisloc.first) returnURL += thisloc.second;
98
99     if (isHandler) {
100         // We may already have RelayState set if we looped back here,
101         // but just in case target is a resource, we reset it back.
102         option = request.getParameter("target");
103         if (option)
104             target = option;
105     }
106     preserveRelayState(request.getApplication(), request, target);
107
108     request.setContentType("text/html");
109     request.setResponseHeader("Expires","01-Jan-1997 12:00:00 GMT");
110     request.setResponseHeader("Cache-Control","private,no-store,no-cache");
111     string fname(m_template);
112     ifstream infile(XMLToolingConfig::getConfig().getPathResolver()->resolve(fname, PathResolver::XMLTOOLING_CFG_FILE).c_str());
113     if (!infile)
114         throw ConfigurationException("Unable to access HTML template ($1).", params(1, m_template));
115     TemplateParameters tp;
116     tp.m_request = &request;
117     tp.setPropertySet(request.getApplication().getPropertySet("Errors"));
118     tp.m_map["action"] = returnURL;
119     if (!target.empty())
120         tp.m_map["target"] = target;
121     stringstream str;
122     XMLToolingConfig::getConfig().getTemplateEngine()->run(infile, str, tp);
123     return make_pair(true,request.sendResponse(str));
124 }