2 * Licensed to the University Corporation for Advanced Internet
3 * Development, Inc. (UCAID) under one or more contributor license
4 * agreements. See the NOTICE file distributed with this work for
5 * additional information regarding copyright ownership.
7 * UCAID licenses this file to you under the Apache License,
8 * Version 2.0 (the "License"); you may not use this file except
9 * in compliance with the License. You may obtain a copy of the
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17 * either express or implied. See the License for the specific
18 * language governing permissions and limitations under the License.
22 * FormSessionInitiator.cpp
24 * HTML form-based IdP discovery.
28 #include "Application.h"
29 #include "exceptions.h"
30 #include "handler/AbstractHandler.h"
31 #include "handler/SessionInitiator.h"
32 #include "util/TemplateParameters.h"
35 #include <xmltooling/XMLToolingConfig.h>
36 #include <xmltooling/util/PathResolver.h>
37 #include <xmltooling/util/URLEncoder.h>
39 using namespace shibsp;
40 using namespace opensaml;
41 using namespace xmltooling;
46 #if defined (_MSC_VER)
47 #pragma warning( push )
48 #pragma warning( disable : 4250 )
51 class SHIBSP_DLLLOCAL FormSessionInitiator : public SessionInitiator, public AbstractHandler
54 FormSessionInitiator(const DOMElement* e, const char* appId)
55 : AbstractHandler(e, Category::getInstance(SHIBSP_LOGCAT ".SessionInitiator.Form")), m_template(getString("template").second) {
57 throw ConfigurationException("Form SessionInitiator requires a template property.");
59 virtual ~FormSessionInitiator() {}
61 pair<bool,long> run(SPRequest& request, string& entityID, bool isHandler=true) const;
64 const char* m_template;
67 #if defined (_MSC_VER)
68 #pragma warning( pop )
71 SessionInitiator* SHIBSP_DLLLOCAL FormSessionInitiatorFactory(const pair<const DOMElement*,const char*>& p)
73 return new FormSessionInitiator(p.first, p.second);
78 pair<bool,long> FormSessionInitiator::run(SPRequest& request, string& entityID, bool isHandler) const
80 if (!checkCompatibility(request, isHandler))
81 return make_pair(false,0L);
84 pair<bool,const char*> prop;
85 const Application& app=request.getApplication();
88 prop = getString("target", request);
91 recoverRelayState(app, request, request, target, false);
94 // Check for a hardwired target value in the map or handler.
95 prop = getString("target", request, HANDLER_PROPERTY_MAP|HANDLER_PROPERTY_FIXED);
99 target = request.getRequestURL();
102 // Compute the return URL. We start with a self-referential link.
103 string returnURL=request.getHandlerURL(target.c_str());
104 pair<bool,const char*> thisloc = getString("Location");
106 returnURL += thisloc.second;
109 // We may already have RelayState set if we looped back here,
110 // but we've turned it back into a resource by this point, so if there's
111 // a target on the URL, reset to that value.
112 prop.second = request.getParameter("target");
113 if (prop.second && *prop.second)
114 target = prop.second;
117 preserveRelayState(app, request, target);
119 request.setContentType("text/html");
120 request.setResponseHeader("Expires","Wed, 01 Jan 1997 12:00:00 GMT");
121 request.setResponseHeader("Cache-Control","private,no-store,no-cache,max-age=0");
122 string fname(m_template);
123 ifstream infile(XMLToolingConfig::getConfig().getPathResolver()->resolve(fname, PathResolver::XMLTOOLING_CFG_FILE).c_str());
125 throw ConfigurationException("Unable to access HTML template ($1).", params(1, m_template));
126 TemplateParameters tp;
127 tp.m_request = &request;
128 tp.setPropertySet(app.getPropertySet("Errors"));
129 tp.m_map["action"] = returnURL;
131 tp.m_map["target"] = target;
133 XMLToolingConfig::getConfig().getTemplateEngine()->run(infile, str, tp);
134 return make_pair(true,request.sendResponse(str));