Move back channel notify loop out of base class for better control.
[shibboleth/sp.git] / shibsp / handler / impl / LocalLogoutInitiator.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  * LocalLogoutInitiator.cpp
19  * 
20  * Logs out a session locally.
21  */
22
23 #include "internal.h"
24 #include "Application.h"
25 #include "ServiceProvider.h"
26 #include "SessionCache.h"
27 #include "handler/AbstractHandler.h"
28 #include "handler/LogoutHandler.h"
29
30 using namespace shibsp;
31 using namespace xmltooling;
32 using namespace log4cpp;
33 using namespace std;
34
35 namespace shibsp {
36
37 #if defined (_MSC_VER)
38     #pragma warning( push )
39     #pragma warning( disable : 4250 )
40 #endif
41
42     class SHIBSP_DLLLOCAL LocalLogoutInitiator : public AbstractHandler, public LogoutHandler
43     {
44     public:
45         LocalLogoutInitiator(const DOMElement* e, const char* appId);
46         virtual ~LocalLogoutInitiator() {}
47         
48         pair<bool,long> run(SPRequest& request, bool isHandler=true) const;
49
50     private:
51         vector<Handler*> m_handlers;
52     };
53
54 #if defined (_MSC_VER)
55     #pragma warning( pop )
56 #endif
57
58     Handler* SHIBSP_DLLLOCAL LocalLogoutInitiatorFactory(const pair<const DOMElement*,const char*>& p)
59     {
60         return new LocalLogoutInitiator(p.first, p.second);
61     }
62 };
63
64 LocalLogoutInitiator::LocalLogoutInitiator(const DOMElement* e, const char* appId)
65     : AbstractHandler(e, Category::getInstance(SHIBSP_LOGCAT".LogoutInitiator"))
66 {
67     pair<bool,const char*> loc = getString("Location");
68     if (loc.first) {
69         string address = string(appId) + loc.second + "::run::LocalLI";
70         setAddress(address.c_str());
71     }
72 }
73
74 pair<bool,long> LocalLogoutInitiator::run(SPRequest& request, bool isHandler) const
75 {
76     // Defer to base class first.
77     pair<bool,long> ret = LogoutHandler::run(request, isHandler);
78     if (ret.first)
79         return ret;
80
81     // Get session ID from cookie.
82     pair<string,const char*> shib_cookie = request.getApplication().getCookieNameProps("_shibsession_");
83     const char* session_id = request.getCookie(shib_cookie.first.c_str());
84     if (session_id) {
85         // Do back channel notification.
86         vector<string> sessions(1, session_id);
87         if (!notifyBackChannel(request.getApplication(), sessions)) {
88             request.getApplication().getServiceProvider().getSessionCache()->remove(session_id, request.getApplication());
89             return sendLogoutPage(request.getApplication(), request, true, "Partial logout failure.");
90         }
91         request.getServiceProvider().getSessionCache()->remove(session_id, request.getApplication());
92     }
93
94     return sendLogoutPage(request.getApplication(), request, true, "Logout was successful.");
95 }