Remove extra header
[shibboleth/cpp-sp.git] / shibsp / handler / impl / LocalLogoutInitiator.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  * LocalLogoutInitiator.cpp
23  * 
24  * Logs out a session locally.
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 "handler/AbstractHandler.h"
34 #include "handler/LogoutInitiator.h"
35
36 #ifndef SHIBSP_LITE
37 using namespace boost;
38 #endif
39
40 using namespace shibsp;
41 using namespace xmltooling;
42 using namespace std;
43
44 namespace shibsp {
45
46 #if defined (_MSC_VER)
47     #pragma warning( push )
48     #pragma warning( disable : 4250 )
49 #endif
50
51     class SHIBSP_DLLLOCAL LocalLogoutInitiator : public AbstractHandler, public LogoutInitiator
52     {
53     public:
54         LocalLogoutInitiator(const DOMElement* e, const char* appId);
55         virtual ~LocalLogoutInitiator() {}
56         
57         void setParent(const PropertySet* parent);
58         void receive(DDF& in, ostream& out);
59         pair<bool,long> run(SPRequest& request, bool isHandler=true) const;
60
61     private:
62         pair<bool,long> doRequest(
63             const Application& application, const HTTPRequest& request, HTTPResponse& httpResponse, Session* session
64             ) const;
65
66         string m_appId;
67     };
68
69 #if defined (_MSC_VER)
70     #pragma warning( pop )
71 #endif
72
73     Handler* SHIBSP_DLLLOCAL LocalLogoutInitiatorFactory(const pair<const DOMElement*,const char*>& p)
74     {
75         return new LocalLogoutInitiator(p.first, p.second);
76     }
77 };
78
79 LocalLogoutInitiator::LocalLogoutInitiator(const DOMElement* e, const char* appId)
80     : AbstractHandler(e, Category::getInstance(SHIBSP_LOGCAT".LogoutInitiator.Local")), m_appId(appId)
81 {
82     pair<bool,const char*> loc = getString("Location");
83     if (loc.first) {
84         string address = string(appId) + loc.second + "::run::LocalLI";
85         setAddress(address.c_str());
86     }
87 }
88
89 void LocalLogoutInitiator::setParent(const PropertySet* parent)
90 {
91     DOMPropertySet::setParent(parent);
92     pair<bool,const char*> loc = getString("Location");
93     if (loc.first) {
94         string address = m_appId + loc.second + "::run::LocalLI";
95         setAddress(address.c_str());
96     }
97     else {
98         m_log.warn("no Location property in Local LogoutInitiator (or parent), can't register as remoted handler");
99     }
100 }
101
102 pair<bool,long> LocalLogoutInitiator::run(SPRequest& request, bool isHandler) const
103 {
104     // Defer to base class first.
105     pair<bool,long> ret = LogoutHandler::run(request, isHandler);
106     if (ret.first)
107         return ret;
108
109     if (SPConfig::getConfig().isEnabled(SPConfig::OutOfProcess)) {
110         // When out of process, we run natively.
111         Session* session = nullptr;
112         try {
113             session = request.getSession(false, true, false);  // don't cache it and ignore all checks
114         }
115         catch (std::exception& ex) {
116             m_log.error("error accessing current session: %s", ex.what());
117         }
118         return doRequest(request.getApplication(), request, request, session);
119     }
120     else {
121         // When not out of process, we remote the request.
122         vector<string> headers(1,"Cookie");
123         DDF out,in = wrap(request,&headers);
124         DDFJanitor jin(in), jout(out);
125         out=request.getServiceProvider().getListenerService()->send(in);
126         return unwrap(request, out);
127     }
128 }
129
130 void LocalLogoutInitiator::receive(DDF& in, ostream& out)
131 {
132 #ifndef SHIBSP_LITE
133     // Defer to base class for back channel notifications
134     if (in["notify"].integer() == 1)
135         return LogoutHandler::receive(in, out);
136
137     // Find application.
138     const char* aid=in["application_id"].string();
139     const Application* app=aid ? SPConfig::getConfig().getServiceProvider()->getApplication(aid) : nullptr;
140     if (!app) {
141         // Something's horribly wrong.
142         m_log.error("couldn't find application (%s) for logout", aid ? aid : "(missing)");
143         throw ConfigurationException("Unable to locate application for logout, deleted?");
144     }
145
146     // Unpack the request.
147     scoped_ptr<HTTPRequest> req(getRequest(in));
148
149     // Set up a response shim.
150     DDF ret(nullptr);
151     DDFJanitor jout(ret);
152     scoped_ptr<HTTPResponse> resp(getResponse(ret));
153
154     Session* session = nullptr;
155     try {
156          session = app->getServiceProvider().getSessionCache()->find(*app, *req, nullptr, nullptr);
157     }
158     catch (std::exception& ex) {
159         m_log.error("error accessing current session: %s", ex.what());
160     }
161
162     // This is the "last chance" handler so even without a session, we "complete" the logout.
163     doRequest(*app, *req, *resp, session);
164
165     out << ret;
166 #else
167     throw ConfigurationException("Cannot perform logout using lite version of shibsp library.");
168 #endif
169 }
170
171 pair<bool,long> LocalLogoutInitiator::doRequest(
172     const Application& application, const HTTPRequest& httpRequest, HTTPResponse& httpResponse, Session* session
173     ) const
174 {
175     if (session) {
176         // Guard the session in case of exception.
177         Locker locker(session, false);
178
179         // Do back channel notification.
180         bool result;
181         vector<string> sessions(1, session->getID());
182         result = notifyBackChannel(application, httpRequest.getRequestURL(), sessions, true);
183 #ifndef SHIBSP_LITE
184         scoped_ptr<LogoutEvent> logout_event(newLogoutEvent(application, &httpRequest, session));
185         if (logout_event) {
186             logout_event->m_logoutType = result ? LogoutEvent::LOGOUT_EVENT_LOCAL : LogoutEvent::LOGOUT_EVENT_PARTIAL;
187             application.getServiceProvider().getTransactionLog()->write(*logout_event);
188         }
189 #endif
190         locker.assign();    // unlock the session
191         application.getServiceProvider().getSessionCache()->remove(application, httpRequest, &httpResponse);
192         if (!result)
193             return sendLogoutPage(application, httpRequest, httpResponse, "partial");
194     }
195
196     // Route back to return location specified, or use the local template.
197     const char* dest = httpRequest.getParameter("return");
198     if (dest) {
199         // Relative URLs get promoted, absolutes get validated.
200         if (*dest == '/') {
201             string d(dest);
202             httpRequest.absolutize(d);
203             return make_pair(true, httpResponse.sendRedirect(d.c_str()));
204         }
205         application.limitRedirect(httpRequest, dest);
206         return make_pair(true, httpResponse.sendRedirect(dest));
207     }
208     return sendLogoutPage(application, httpRequest, httpResponse, "local");
209 }