40f1aae6bd90f38cef0815fe6cb3ef7aada1c823
[shibboleth/cpp-sp.git] / shibsp / handler / impl / LogoutHandler.cpp
1 /**
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.
6  *
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
10  * License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  */
20
21 /**
22  * LogoutHandler.cpp
23  *
24  * Base class for logout-related handlers.
25  */
26
27 #include "internal.h"
28 #include "exceptions.h"
29 #include "Application.h"
30 #include "ServiceProvider.h"
31 #include "SessionCache.h"
32 #include "SPRequest.h"
33 #include "TransactionLog.h"
34 #include "handler/LogoutHandler.h"
35 #include "util/TemplateParameters.h"
36
37 #include <fstream>
38 #include <boost/lexical_cast.hpp>
39 #include <xmltooling/XMLToolingConfig.h>
40 #include <xmltooling/util/PathResolver.h>
41 #include <xmltooling/util/URLEncoder.h>
42
43 using namespace shibsp;
44 using namespace xmltooling;
45 using namespace boost;
46 using namespace std;
47
48 LogoutHandler::LogoutHandler() : m_initiator(true)
49 {
50 }
51
52 LogoutHandler::~LogoutHandler()
53 {
54 }
55
56 pair<bool,long> LogoutHandler::sendLogoutPage(
57     const Application& application, const HTTPRequest& request, HTTPResponse& response, bool local, const char* status
58     ) const
59 {
60     return sendLogoutPage(application, request, response, local ? "local" : "global");
61 }
62
63 pair<bool,long> LogoutHandler::sendLogoutPage(
64     const Application& application, const HTTPRequest& request, HTTPResponse& response, const char* type
65     ) const
66 {
67     string tname = string(type) + "Logout";
68     const PropertySet* props = application.getPropertySet("Errors");
69     pair<bool,const char*> prop = props ? props->getString(tname.c_str()) : pair<bool,const char*>(false,nullptr);
70     if (!prop.first) {
71         tname += ".html";
72         prop.second = tname.c_str();
73     }
74     response.setContentType("text/html");
75     response.setResponseHeader("Expires","Wed, 01 Jan 1997 12:00:00 GMT");
76     response.setResponseHeader("Cache-Control","private,no-store,no-cache,max-age=0");
77     string fname(prop.second);
78     ifstream infile(XMLToolingConfig::getConfig().getPathResolver()->resolve(fname, PathResolver::XMLTOOLING_CFG_FILE).c_str());
79     if (!infile)
80         throw ConfigurationException("Unable to access $1 HTML template.", params(1,prop.second));
81     TemplateParameters tp;
82     tp.m_request = &request;
83     tp.setPropertySet(props);
84     tp.m_map["logoutStatus"] = "Logout completed successfully.";  // Backward compatibility.
85     stringstream str;
86     XMLToolingConfig::getConfig().getTemplateEngine()->run(infile, str, tp);
87     return make_pair(true,response.sendResponse(str));
88 }
89
90 pair<bool,long> LogoutHandler::run(SPRequest& request, bool isHandler) const
91 {
92     // If we're inside a chain, do nothing.
93     if (getParent())
94         return make_pair(false,0L);
95
96     // If this isn't a LogoutInitiator, we only "continue" a notification loop, rather than starting one.
97     if (!m_initiator && !request.getParameter("notifying"))
98         return make_pair(false,0L);
99
100     // Try another front-channel notification. No extra parameters and the session is implicit.
101     return notifyFrontChannel(request.getApplication(), request, request);
102 }
103
104 void LogoutHandler::receive(DDF& in, ostream& out)
105 {
106     DDF ret(nullptr);
107     DDFJanitor jout(ret);
108     if (in["notify"].integer() != 1)
109         throw ListenerException("Unsupported operation.");
110
111     // Find application.
112     const char* aid=in["application_id"].string();
113     const Application* app=aid ? SPConfig::getConfig().getServiceProvider()->getApplication(aid) : nullptr;
114     if (!app) {
115         // Something's horribly wrong.
116         Category::getInstance(SHIBSP_LOGCAT".Logout").error("couldn't find application (%s) for logout", aid ? aid : "(missing)");
117         throw ConfigurationException("Unable to locate application for logout, deleted?");
118     }
119
120     vector<string> sessions;
121     DDF s = in["sessions"];
122     DDF temp = s.first();
123     while (temp.isstring()) {
124         sessions.push_back(temp.string());
125         temp = s.next();
126         if (notifyBackChannel(*app, in["url"].string(), sessions, in["local"].integer()==1))
127             ret.integer(1);
128     }
129
130     out << ret;
131 }
132
133 pair<bool,long> LogoutHandler::notifyFrontChannel(
134     const Application& application,
135     const HTTPRequest& request,
136     HTTPResponse& response,
137     const map<string,string>* params
138     ) const
139 {
140     // Index of notification point starts at 0.
141     unsigned int index = 0;
142     const char* param = request.getParameter("index");
143     if (param)
144         index = atoi(param);
145
146     // "return" is a backwards-compatible "eventual destination" to go back to after logout completes.
147     param = request.getParameter("return");
148
149     // Fetch the next front notification URL and bump the index for the next round trip.
150     string loc = application.getNotificationURL(request.getRequestURL(), true, index++);
151     if (loc.empty())
152         return make_pair(false,0L);
153
154     const URLEncoder* encoder = XMLToolingConfig::getConfig().getURLEncoder();
155
156     // Start with an "action" telling the application what this is about.
157     loc = loc + (strchr(loc.c_str(),'?') ? '&' : '?') + "action=logout";
158
159     // Now we create a second URL representing the return location back to us.
160     const char* start = request.getRequestURL();
161     const char* end = strchr(start, '?');
162     string locstr(start, end ? end - start : strlen(start));
163
164     // Add a signal that we're coming back from notification and the next index.
165     locstr = locstr + "?notifying=1&index=" + lexical_cast<string>(index);
166
167     // Add return if set.
168     if (param)
169         locstr = locstr + "&return=" + encoder->encode(param);
170
171     // We preserve anything we're instructed to directly.
172     if (params) {
173         for (map<string,string>::const_iterator p = params->begin(); p!=params->end(); ++p)
174             locstr = locstr + '&' + p->first + '=' + encoder->encode(p->second.c_str());
175     }
176     else {
177         for (vector<string>::const_iterator q = m_preserve.begin(); q!=m_preserve.end(); ++q) {
178             param = request.getParameter(q->c_str());
179             if (param)
180                 locstr = locstr + '&' + *q + '=' + encoder->encode(param);
181         }
182     }
183
184     // Add the notifier's return parameter to the destination location and redirect.
185     // This is NOT the same as the return parameter that might be embedded inside it ;-)
186     loc = loc + "&return=" + encoder->encode(locstr.c_str());
187     return make_pair(true, response.sendRedirect(loc.c_str()));
188 }
189
190 #ifndef SHIBSP_LITE
191 #include "util/SPConstants.h"
192 #include <xmltooling/impl/AnyElement.h>
193 #include <xmltooling/soap/SOAP.h>
194 #include <xmltooling/soap/SOAPClient.h>
195 #include <xmltooling/soap/HTTPSOAPTransport.h>
196 using namespace soap11;
197 namespace {
198     static const XMLCh LogoutNotification[] =   UNICODE_LITERAL_18(L,o,g,o,u,t,N,o,t,i,f,i,c,a,t,i,o,n);
199     static const XMLCh SessionID[] =            UNICODE_LITERAL_9(S,e,s,s,i,o,n,I,D);
200     static const XMLCh _type[] =                UNICODE_LITERAL_4(t,y,p,e);
201     static const XMLCh _local[] =               UNICODE_LITERAL_5(l,o,c,a,l);
202     static const XMLCh _global[] =              UNICODE_LITERAL_6(g,l,o,b,a,l);
203
204     class SHIBSP_DLLLOCAL SOAPNotifier : public soap11::SOAPClient
205     {
206     public:
207         SOAPNotifier() {}
208         virtual ~SOAPNotifier() {}
209     private:
210         void prepareTransport(SOAPTransport& transport) {
211             transport.setVerifyHost(false);
212             HTTPSOAPTransport* http = dynamic_cast<HTTPSOAPTransport*>(&transport);
213             if (http) {
214                 http->useChunkedEncoding(false);
215                 http->setRequestHeader(PACKAGE_NAME, PACKAGE_VERSION);
216             }
217         }
218     };
219 };
220 #endif
221
222 bool LogoutHandler::notifyBackChannel(
223     const Application& application, const char* requestURL, const vector<string>& sessions, bool local
224     ) const
225 {
226     if (sessions.empty()) {
227         Category::getInstance(SHIBSP_LOGCAT".Logout").error("no sessions supplied to back channel notification method");
228         return false;
229     }
230
231     unsigned int index = 0;
232     string endpoint = application.getNotificationURL(requestURL, false, index++);
233     if (endpoint.empty())
234         return true;
235
236     if (SPConfig::getConfig().isEnabled(SPConfig::OutOfProcess)) {
237 #ifndef SHIBSP_LITE
238         scoped_ptr<Envelope> env(EnvelopeBuilder::buildEnvelope());
239         Body* body = BodyBuilder::buildBody();
240         env->setBody(body);
241         ElementProxy* msg = new AnyElementImpl(shibspconstants::SHIB2SPNOTIFY_NS, LogoutNotification);
242         body->getUnknownXMLObjects().push_back(msg);
243         msg->setAttribute(xmltooling::QName(nullptr, _type), local ? _local : _global);
244         for (vector<string>::const_iterator s = sessions.begin(); s != sessions.end(); ++s) {
245             auto_ptr_XMLCh temp(s->c_str());
246             ElementProxy* child = new AnyElementImpl(shibspconstants::SHIB2SPNOTIFY_NS, SessionID);
247             child->setTextContent(temp.get());
248             msg->getUnknownXMLObjects().push_back(child);
249         }
250
251         bool result = true;
252         SOAPNotifier soaper;
253         while (!endpoint.empty()) {
254             try {
255                 soaper.send(*env, SOAPTransport::Address(application.getId(), application.getId(), endpoint.c_str()));
256                 delete soaper.receive();
257             }
258             catch (std::exception& ex) {
259                 Category::getInstance(SHIBSP_LOGCAT".Logout").error("error notifying application of logout event: %s", ex.what());
260                 result = false;
261             }
262             soaper.reset();
263             endpoint = application.getNotificationURL(requestURL, false, index++);
264         }
265         return result;
266 #else
267         return false;
268 #endif
269     }
270
271     // When not out of process, we remote the back channel work.
272     DDF out,in(m_address.c_str());
273     DDFJanitor jin(in), jout(out);
274     in.addmember("notify").integer(1);
275     in.addmember("application_id").string(application.getId());
276     in.addmember("url").string(requestURL);
277     if (local)
278         in.addmember("local").integer(1);
279     DDF s = in.addmember("sessions").list();
280     for (vector<string>::const_iterator i = sessions.begin(); i!=sessions.end(); ++i) {
281         DDF temp = DDF(nullptr).string(i->c_str());
282         s.add(temp);
283     }
284     out = application.getServiceProvider().getListenerService()->send(in);
285     return (out.integer() == 1);
286 }
287
288 #ifndef SHIBSP_LITE
289
290 LogoutEvent* LogoutHandler::newLogoutEvent(
291     const Application& application, const xmltooling::HTTPRequest* request, const Session* session
292     ) const
293 {
294     if (!SPConfig::getConfig().isEnabled(SPConfig::Logging))
295         return nullptr;
296     try {
297         auto_ptr<TransactionLog::Event> event(SPConfig::getConfig().EventManager.newPlugin(LOGOUT_EVENT, nullptr));
298         LogoutEvent* logout_event = dynamic_cast<LogoutEvent*>(event.get());
299         if (logout_event) {
300             logout_event->m_request = request;
301             logout_event->m_app = &application;
302             logout_event->m_binding = getString("Binding").second;
303             logout_event->m_session = session;
304             if (session) {
305                 logout_event->m_nameID = session->getNameID();
306                 logout_event->m_sessions.push_back(session->getID());
307             }
308             event.release();
309             return logout_event;
310         }
311         else {
312             Category::getInstance(SHIBSP_LOGCAT".Logout").warn("unable to audit event, log event object was of an incorrect type");
313         }
314     }
315     catch (std::exception& ex) {
316         Category::getInstance(SHIBSP_LOGCAT".Logout").warn("exception auditing event: %s", ex.what());
317     }
318     return nullptr;
319 }
320
321 #endif