Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix string view in game world auth #4360

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions src/iologindata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ bool IOLoginData::loginserverAuthentication(const std::string& name, const std::
return true;
}

std::pair<uint32_t, std::string_view> IOLoginData::gameworldAuthentication(std::string_view accountName,
std::pair<uint32_t, std::string> IOLoginData::gameworldAuthentication(std::string_view accountName,
std::string_view password,
std::string_view characterName,
std::string_view token, uint32_t tokenTime)
Expand All @@ -102,28 +102,28 @@ std::pair<uint32_t, std::string_view> IOLoginData::gameworldAuthentication(std::
fmt::format("SELECT `id`, `password`, `secret` FROM `accounts` WHERE `name` = {:s} OR `email` = {:s}",
db.escapeString(accountName), db.escapeString(accountName)));
if (!result) {
return std::make_pair(0, characterName);
return std::make_pair(0, "");
}

// two-factor auth
if (g_config.getBoolean(ConfigManager::TWO_FACTOR_AUTH)) {
std::string secret = decodeSecret(result->getString("secret"));
if (!secret.empty()) {
if (token.empty()) {
return std::make_pair(0, characterName);
return std::make_pair(0, "");
}

bool tokenValid = token == generateToken(secret, tokenTime) ||
token == generateToken(secret, tokenTime - 1) ||
token == generateToken(secret, tokenTime + 1);
if (!tokenValid) {
return std::make_pair(0, characterName);
return std::make_pair(0, "");
}
}
}

if (transformToSHA1(password) != result->getString("password")) {
return std::make_pair(0, characterName);
return std::make_pair(0, "");
}

uint32_t accountId = result->getNumber<uint32_t>("id");
Expand All @@ -132,10 +132,9 @@ std::pair<uint32_t, std::string_view> IOLoginData::gameworldAuthentication(std::
fmt::format("SELECT `name` FROM `players` WHERE `name` = {:s} AND `account_id` = {:d} AND `deletion` = 0",
db.escapeString(characterName), accountId));
if (!result) {
return std::make_pair(0, characterName);
return std::make_pair(0, "");
}

return std::make_pair(accountId, result->getString("name"));
return std::make_pair(accountId, std::string{result->getString("name")});
}

uint32_t IOLoginData::getAccountIdByPlayerName(const std::string& playerName)
Expand Down
2 changes: 1 addition & 1 deletion src/iologindata.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class IOLoginData
static Account loadAccount(uint32_t accno);

static bool loginserverAuthentication(const std::string& name, const std::string& password, Account& account);
static std::pair<uint32_t, std::string_view> gameworldAuthentication(std::string_view accountName,
static std::pair<uint32_t, std::string> gameworldAuthentication(std::string_view accountName,
std::string_view password,
std::string_view characterName,
std::string_view token, uint32_t tokenTime);
Expand Down
5 changes: 3 additions & 2 deletions src/protocolgame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -469,14 +469,15 @@ void ProtocolGame::onRecvFirstMessage(NetworkMessage& msg)
}

uint32_t accountId;
std::tie(accountId, characterName) =
std::string characterNameStr;
std::tie(accountId, characterNameStr) =
IOLoginData::gameworldAuthentication(accountName, password, characterName, token, tokenTime);
if (accountId == 0) {
disconnectClient("Account name or password is not correct.");
return;
}

g_dispatcher.addTask([=, thisPtr = getThis(), characterName = std::string{characterName}]() {
g_dispatcher.addTask([=, thisPtr = getThis(), characterName = characterNameStr]() {
thisPtr->login(characterName, accountId, operatingSystem);
});
}
Expand Down