5768ac4d09d36762279d4874f25f2f1bfda3e817
[shibboleth/sp.git] / shibsp / handler / impl / LocalLogoutInitiator.cpp
1 /*
2  *  Copyright 2001-2009 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 "SPRequest.h"
28 #include "handler/AbstractHandler.h"
29 #include "handler/LogoutHandler.h"
30
31 using namespace shibsp;
32 using namespace xmltooling;
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         void setParent(const PropertySet* parent);
49         pair<bool,long> run(SPRequest& request, bool isHandler=true) const;
50
51 #ifndef SHIBSP_LITE
52         const char* getType() const {
53             return "LogoutInitiator";
54         }
55 #endif
56
57     private:
58         string m_appId;
59     };
60
61 #if defined (_MSC_VER)
62     #pragma warning( pop )
63 #endif
64
65     Handler* SHIBSP_DLLLOCAL LocalLogoutInitiatorFactory(const pair<const DOMElement*,const char*>& p)
66     {
67         return new LocalLogoutInitiator(p.first, p.second);
68     }
69 };
70
71 LocalLogoutInitiator::LocalLogoutInitiator(const DOMElement* e, const char* appId)
72     : AbstractHandler(e, Category::getInstance(SHIBSP_LOGCAT".LogoutInitiator.Local")), m_appId(appId)
73 {
74     pair<bool,const char*> loc = getString("Location");
75     if (loc.first) {
76         string address = string(appId) + loc.second + "::run::LocalLI";
77         setAddress(address.c_str());
78     }
79 }
80
81 void LocalLogoutInitiator::setParent(const PropertySet* parent)
82 {
83     DOMPropertySet::setParent(parent);
84     pair<bool,const char*> loc = getString("Location");
85     if (loc.first) {
86         string address = m_appId + loc.second + "::run::LocalLI";
87         setAddress(address.c_str());
88     }
89     else {
90         m_log.warn("no Location property in Local LogoutInitiator (or parent), can't register as remoted handler");
91     }
92 }
93
94 pair<bool,long> LocalLogoutInitiator::run(SPRequest& request, bool isHandler) const
95 {
96     // Defer to base class first.
97     pair<bool,long> ret = LogoutHandler::run(request, isHandler);
98     if (ret.first)
99         return ret;
100
101     const Application& app = request.getApplication();
102     string session_id = app.getServiceProvider().getSessionCache()->active(app, request);
103     if (!session_id.empty()) {
104         // Do back channel notification.
105         vector<string> sessions(1, session_id);
106         if (!notifyBackChannel(app, request.getRequestURL(), sessions, true)) {
107             app.getServiceProvider().getSessionCache()->remove(app, request, &request);
108             return sendLogoutPage(app, request, request, true, "Partial logout failure.");
109         }
110         request.getServiceProvider().getSessionCache()->remove(app, request, &request);
111     }
112
113     // Route back to return location specified, or use the local template.
114     const char* dest = request.getParameter("return");
115     if (dest)
116         return make_pair(true, request.sendRedirect(dest));
117     return sendLogoutPage(app, request, request, true, "Logout was successful.");
118 }