Skip to content

Commit da93061

Browse files
committed
Restore session if missing
1 parent b4fb7dd commit da93061

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

webserver/cWebem.cpp

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,7 +1326,7 @@ bool cWebemRequestHandler::CheckAuthentication(WebEmSession & session, const req
13261326
session.id = sSID;
13271327
}
13281328
session.auth_token = sAuthToken;
1329-
// Check authen_token
1329+
// Check authen_token and restore session
13301330
if (checkAuthToken(session)) {
13311331
// user is authenticated
13321332
return true;
@@ -1386,7 +1386,10 @@ bool cWebemRequestHandler::CheckAuthentication(WebEmSession & session, const req
13861386
return false;
13871387
}
13881388

1389-
bool cWebemRequestHandler::checkAuthToken(const WebEmSession & session) {
1389+
/**
1390+
* Check authentication token if exists and restore the user session if necessary
1391+
*/
1392+
bool cWebemRequestHandler::checkAuthToken(WebEmSession & session) {
13901393
session_store* sstore = myWebem->GetSessionStore();
13911394
if (sstore == NULL) {
13921395
_log.Log(LOG_ERROR, "CheckAuthToken([%s_%s]) : no store defined", session.id.c_str(), session.auth_token.c_str());
@@ -1408,10 +1411,32 @@ bool cWebemRequestHandler::checkAuthToken(const WebEmSession & session) {
14081411
return false;
14091412
}
14101413

1411-
// TODO : Restore session ?
1412-
//session.username = base64_decode(storedSession.username);
1413-
14141414
_log.Log(LOG_STATUS, "CheckAuthToken(%s_%s_%s) : user authenticated", session.id.c_str(), session.auth_token.c_str(), session.username.c_str());
1415+
1416+
if (session.username.empty()) {
1417+
// Restore session if user exists and session does not already exist
1418+
bool userExists = false;
1419+
session.username = storedSession.username;
1420+
std::vector<_tWebUserPassword>::iterator ittu;
1421+
for (ittu=myWebem->m_userpasswords.begin(); ittu!=myWebem->m_userpasswords.end(); ++ittu) {
1422+
if (ittu->Username == session.username) { // the user still exists
1423+
userExists = true;
1424+
session.rights = ittu->userrights;
1425+
break;
1426+
}
1427+
}
1428+
if (!userExists) {
1429+
_log.Log(LOG_ERROR, "CheckAuthToken(%s_%s) : cannot restore session user not found", session.id.c_str(), session.auth_token.c_str());
1430+
removeAuthToken(session.id);
1431+
return false;
1432+
}
1433+
std::map<std::string, WebEmSession>::iterator itts = myWebem->m_sessions.find(session.id);
1434+
if (itts == myWebem->m_sessions.end()) {
1435+
_log.Log(LOG_STATUS, "CheckAuthToken(%s_%s_%s) : restore session", session.id.c_str(), session.auth_token.c_str(), session.username.c_str());
1436+
myWebem->m_sessions[session.id] = session;
1437+
}
1438+
}
1439+
14151440
return true;
14161441
}
14171442

webserver/cWebem.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ namespace http {
132132
void Logout();
133133
int parse_auth_header(const request& req, struct ah *ah);
134134
std::string generateAuthToken(const WebEmSession & session, const request & req);
135-
bool checkAuthToken(const WebEmSession & session);
135+
bool checkAuthToken(WebEmSession & session);
136136
void removeAuthToken(const std::string & sessionId);
137137
std::string m_doc_root;
138138
// Webem link to application code

0 commit comments

Comments
 (0)