/
SessionApi.cpp
254 lines (205 loc) · 9.07 KB
/
SessionApi.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
* Copyright (C) 2011-2019 AirDC++ Project
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "stdinc.h"
#include <api/SessionApi.h>
#include <api/SystemApi.h>
#include <api/WebUserUtils.h>
#include <api/common/Serializer.h>
#include <web-server/JsonUtil.h>
#include <web-server/WebSocket.h>
#include <web-server/WebServerManager.h>
#include <web-server/WebServerSettings.h>
#include <web-server/WebUserManager.h>
#include <airdcpp/ActivityManager.h>
#include <airdcpp/ClientManager.h>
#include <airdcpp/SettingsManager.h>
#include <airdcpp/version.h>
namespace webserver {
SessionApi::SessionApi(Session* aSession) :
SubscribableApiModule(aSession, Access::ADMIN, { "session_created", "session_removed" })
{
// Just fail these since we have a session already...
METHOD_HANDLER(Access::ANY, METHOD_POST, (EXACT_PARAM("authorize")), SessionApi::failAuthenticatedRequest);
METHOD_HANDLER(Access::ANY, METHOD_POST, (EXACT_PARAM("socket")), SessionApi::failAuthenticatedRequest);
// Methods for the current session
METHOD_HANDLER(Access::ANY, METHOD_POST, (EXACT_PARAM("activity")), SessionApi::handleActivity);
METHOD_HANDLER(Access::ANY, METHOD_GET, (EXACT_PARAM("self")), SessionApi::handleGetCurrentSession);
METHOD_HANDLER(Access::ANY, METHOD_DELETE, (EXACT_PARAM("self")), SessionApi::handleRemoveCurrentSession);
// Admin methods
METHOD_HANDLER(Access::ADMIN, METHOD_GET, (), SessionApi::handleGetSessions);
METHOD_HANDLER(Access::ADMIN, METHOD_GET, (TOKEN_PARAM), SessionApi::handleGetSession);
METHOD_HANDLER(Access::ADMIN, METHOD_DELETE, (TOKEN_PARAM), SessionApi::handleRemoveSession);
aSession->getServer()->getUserManager().addListener(this);
}
SessionApi::~SessionApi() {
session->getServer()->getUserManager().removeListener(this);
}
api_return SessionApi::failAuthenticatedRequest(ApiRequest& aRequest) {
aRequest.setResponseErrorStr("This method can't be used after authentication");
return websocketpp::http::status_code::precondition_failed;
}
api_return SessionApi::handleActivity(ApiRequest& aRequest) {
// This may also be used to prevent the session from expiring
if (JsonUtil::getOptionalFieldDefault<bool>("user_active", aRequest.getRequestBody(), false)) {
ActivityManager::getInstance()->updateActivity();
}
return websocketpp::http::status_code::no_content;
}
api_return SessionApi::handleRemoveCurrentSession(ApiRequest& aRequest) {
return logout(aRequest, aRequest.getSession());
}
api_return SessionApi::logout(ApiRequest& aRequest, const SessionPtr& aSession) {
if (aSession->getSessionType() == Session::TYPE_BASIC_AUTH) {
aRequest.setResponseErrorStr("Sessions using basic authentication can't be deleted");
return websocketpp::http::status_code::bad_request;
}
session->getServer()->getUserManager().logout(aSession);
return websocketpp::http::status_code::no_content;
}
websocketpp::http::status_code::value SessionApi::handleLogin(ApiRequest& aRequest, bool aIsSecure, const WebSocketPtr& aSocket, const string& aIP) {
auto& um = WebServerManager::getInstance()->getUserManager();
const auto& reqJson = aRequest.getRequestBody();
auto grantType = JsonUtil::getOptionalFieldDefault<string>("grant_type", reqJson, "password");
auto inactivityMinutes = JsonUtil::getOptionalFieldDefault<uint64_t>("max_inactivity", reqJson, WEBCFG(DEFAULT_SESSION_IDLE_TIMEOUT).uint64());
SessionPtr session = nullptr;
auto sessionType = aIsSecure ? Session::TYPE_SECURE : Session::TYPE_PLAIN;
if (grantType == "password") {
auto username = JsonUtil::getField<string>("username", reqJson, false);
auto password = JsonUtil::getField<string>("password", reqJson, false);
try {
session = um.authenticateSession(username, password,
sessionType, inactivityMinutes, aIP);
} catch (const std::exception& e) {
aRequest.setResponseErrorStr(e.what());
return websocketpp::http::status_code::unauthorized;
}
} else if (grantType == "refresh_token") {
auto refreshToken = JsonUtil::getField<string>("refresh_token", reqJson, false);
try {
session = um.authenticateSession(refreshToken,
sessionType, inactivityMinutes, aIP);
} catch (const std::exception& e) {
aRequest.setResponseErrorStr(e.what());
return websocketpp::http::status_code::bad_request;
}
} else {
JsonUtil::throwError("grant_type", JsonUtil::ERROR_INVALID, "Invalid grant_type");
return websocketpp::http::status_code::bad_request;
}
dcassert(session);
if (aSocket) {
session->onSocketConnected(aSocket);
aSocket->setSession(session);
}
aRequest.setResponseBody(serializeLoginInfo(session, um.createRefreshToken(session->getUser())));
return websocketpp::http::status_code::ok;
}
json SessionApi::serializeLoginInfo(const SessionPtr& aSession, const string& aRefreshToken) {
json ret = {
{ "auth_token", aSession->getAuthToken() },
{ "token_type", "Bearer" },
{ "user", Serializer::serializeItem(aSession->getUser(), WebUserUtils::propertyHandler) },
{ "system_info", SystemApi::getSystemInfo() },
{ "wizard_pending", SETTING(WIZARD_PENDING) },
};
if (!aRefreshToken.empty()) {
ret.emplace("refresh_token", aRefreshToken);
}
return ret;
}
api_return SessionApi::handleSocketConnect(ApiRequest& aRequest, bool aIsSecure, const WebSocketPtr& aSocket) {
if (!aSocket) {
aRequest.setResponseErrorStr("This method may be called only via a websocket");
return websocketpp::http::status_code::bad_request;
}
auto sessionToken = JsonUtil::getField<string>("auth_token", aRequest.getRequestBody(), false);
auto session = WebServerManager::getInstance()->getUserManager().getSession(sessionToken);
if (!session) {
aRequest.setResponseErrorStr("Invalid session token");
return websocketpp::http::status_code::bad_request;
}
if ((session->getSessionType() == Session::TYPE_SECURE) != aIsSecure) {
aRequest.setResponseErrorStr("Invalid protocol");
return websocketpp::http::status_code::bad_request;
}
session->onSocketConnected(aSocket);
aSocket->setSession(session);
aRequest.setResponseBody(serializeLoginInfo(session, Util::emptyString));
return websocketpp::http::status_code::no_content;
}
api_return SessionApi::handleGetSessions(ApiRequest& aRequest) {
auto sessions = session->getServer()->getUserManager().getSessions();
aRequest.setResponseBody(Serializer::serializeList(sessions, serializeSession));
return websocketpp::http::status_code::ok;
}
api_return SessionApi::handleGetSession(ApiRequest& aRequest) {
auto s = session->getServer()->getUserManager().getSession(aRequest.getTokenParam());
if (!s) {
aRequest.setResponseErrorStr("Session not found");
return websocketpp::http::status_code::not_found;
}
aRequest.setResponseBody(serializeSession(s));
return websocketpp::http::status_code::ok;
}
api_return SessionApi::handleRemoveSession(ApiRequest& aRequest) {
auto removeSession = session->getServer()->getUserManager().getSession(aRequest.getTokenParam());
if (!removeSession) {
aRequest.setResponseErrorStr("Session not found");
return websocketpp::http::status_code::not_found;
}
return logout(aRequest, removeSession);
}
api_return SessionApi::handleGetCurrentSession(ApiRequest& aRequest) {
aRequest.setResponseBody(serializeSession(aRequest.getSession()));
return websocketpp::http::status_code::ok;
}
string SessionApi::getSessionType(const SessionPtr& aSession) noexcept {
switch (aSession->getSessionType()) {
case Session::TYPE_BASIC_AUTH: return "basic_auth";
case Session::TYPE_PLAIN: return "plain";
case Session::TYPE_SECURE: return "secure";
case Session::TYPE_EXTENSION: return "extension";
}
dcassert(0);
return "";
}
json SessionApi::serializeSession(const SessionPtr& aSession) noexcept {
return {
{ "id", aSession->getId() },
{ "type", getSessionType(aSession) },
{ "last_activity", GET_TICK() - aSession->getLastActivity() },
{ "ip", aSession->getIp() },
{ "user", Serializer::serializeItem(aSession->getUser(), WebUserUtils::propertyHandler) }
};
}
void SessionApi::on(WebUserManagerListener::SessionCreated, const SessionPtr& aSession) noexcept {
maybeSend("session_created", [&] {
return json({
{ "session", serializeSession(aSession) },
});
});
}
void SessionApi::on(WebUserManagerListener::SessionRemoved, const SessionPtr& aSession, bool aTimedOut) noexcept {
maybeSend("session_removed", [&] {
return json({
{ "session", serializeSession(aSession) },
{ "timed_out", aTimedOut },
});
});
}
}