Provide client request into template generation, particularly for logout.
[shibboleth/cpp-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 std;
33
34 namespace shibsp {
35
36 #if defined (_MSC_VER)
37     #pragma warning( push )
38     #pragma warning( disable : 4250 )
39 #endif
40
41     class SHIBSP_DLLLOCAL LocalLogoutInitiator : public AbstractHandler, public LogoutHandler
42     {
43     public:
44         LocalLogoutInitiator(const DOMElement* e, const char* appId);
45         virtual ~LocalLogoutInitiator() {}
46         
47         void setParent(const PropertySet* parent);
48         pair<bool,long> run(SPRequest& request, bool isHandler=true) const;
49
50 #ifndef SHIBSP_LITE
51         const char* getType() const {
52             return "LogoutInitiator";
53         }
54 #endif
55
56     private:
57         string m_appId;
58     };
59
60 #if defined (_MSC_VER)
61     #pragma warning( pop )
62 #endif
63
64     Handler* SHIBSP_DLLLOCAL LocalLogoutInitiatorFactory(const pair<const DOMElement*,const char*>& p)
65     {
66         return new LocalLogoutInitiator(p.first, p.second);
67     }
68 };
69
70 LocalLogoutInitiator::LocalLogoutInitiator(const DOMElement* e, const char* appId)
71     : AbstractHandler(e, Category::getInstance(SHIBSP_LOGCAT".LogoutInitiator.Local")), m_appId(appId)
72 {
73     pair<bool,const char*> loc = getString("Location");
74     if (loc.first) {
75         string address = string(appId) + loc.second + "::run::LocalLI";
76         setAddress(address.c_str());
77     }
78 }
79
80 void LocalLogoutInitiator::setParent(const PropertySet* parent)
81 {
82     DOMPropertySet::setParent(parent);
83     pair<bool,const char*> loc = getString("Location");
84     if (loc.first) {
85         string address = m_appId + loc.second + "::run::LocalLI";
86         setAddress(address.c_str());
87     }
88     else {
89         m_log.warn("no Location property in Local LogoutInitiator (or parent), can't register as remoted handler");
90     }
91 }
92
93 pair<bool,long> LocalLogoutInitiator::run(SPRequest& request, bool isHandler) const
94 {
95     // Defer to base class first.
96     pair<bool,long> ret = LogoutHandler::run(request, isHandler);
97     if (ret.first)
98         return ret;
99
100     const Application& app = request.getApplication();
101     string session_id = app.getServiceProvider().getSessionCache()->active(app, request);
102     if (!session_id.empty()) {
103         // Do back channel notification.
104         vector<string> sessions(1, session_id);
105         if (!notifyBackChannel(app, request.getRequestURL(), sessions, true)) {
106             app.getServiceProvider().getSessionCache()->remove(app, request, &request);
107             return sendLogoutPage(app, request, request, true, "Partial logout failure.");
108         }
109         request.getServiceProvider().getSessionCache()->remove(app, request, &request);
110     }
111
112     return sendLogoutPage(app, request, request, true, "Logout was successful.");
113 }