Skip to content

Commit

Permalink
Auth cookie refresh on PC_EXIT
Browse files Browse the repository at this point in the history
  • Loading branch information
yungcomputerchair committed Nov 23, 2024
1 parent 55cf3f7 commit ed9fe61
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/PlayerManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,14 @@ static void heartbeatPlayer(CNSocket* sock, CNPacketData* data) {

static void exitGame(CNSocket* sock, CNPacketData* data) {
auto exitData = (sP_CL2FE_REQ_PC_EXIT*)data->buf;

// Refresh any auth cookie, in case "change character" was used
Player* plr = getPlayer(sock);
if (plr != nullptr) {
// 5 seconds should be enough to log in again
Database::refreshCookie(plr->accountId, 5);
}

INITSTRUCT(sP_FE2CL_REP_PC_EXIT_SUCC, response);

response.iID = exitData->iID;
Expand Down
1 change: 1 addition & 0 deletions src/db/Database.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ namespace Database {
// return true if cookie is valid for the account.
// invalidates the stored cookie afterwards
bool checkCookie(int accountId, const char *cookie);
void refreshCookie(int accountId, int durationSec);

// interface for the /ban command
bool banPlayer(int playerId, std::string& reason);
Expand Down
21 changes: 21 additions & 0 deletions src/db/login.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,27 @@ bool Database::checkCookie(int accountId, const char *tryCookie) {
return match;
}

void Database::refreshCookie(int accountId, int durationSec) {
std::lock_guard<std::mutex> lock(dbCrit);

const char* sql = R"(
UPDATE Auth
SET Expires = ?
WHERE AccountID = ?;
)";

int expires = getTimestamp() + durationSec;

sqlite3_stmt* stmt;
sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
sqlite3_bind_int(stmt, 1, expires);
sqlite3_bind_int(stmt, 2, accountId);
int rc = sqlite3_step(stmt);
sqlite3_finalize(stmt);
if (rc != SQLITE_DONE)
std::cout << "[WARN] Database fail on refreshCookie(): " << sqlite3_errmsg(db) << std::endl;
}

void Database::updateSelected(int accountId, int slot) {
std::lock_guard<std::mutex> lock(dbCrit);

Expand Down

0 comments on commit ed9fe61

Please sign in to comment.