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