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: login into another accounts #2853

Merged
merged 9 commits into from
Sep 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions src/account/account_repository.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class AccountRepository {
virtual bool loadBySession(const std::string &email, AccountInfo &acc) = 0;
virtual bool save(const AccountInfo &accInfo) = 0;

virtual bool getCharacterByNameAndAccountId(const uint32_t &id, const std::string &name) = 0;
phacUFPE marked this conversation as resolved.
Show resolved Hide resolved

virtual bool getPassword(const uint32_t &id, std::string &password) = 0;

virtual bool getCoins(const uint32_t &id, const uint8_t &type, uint32_t &coins) = 0;
Expand Down
10 changes: 10 additions & 0 deletions src/account/account_repository_db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@
return successful;
};

bool AccountRepositoryDB::getCharacterByNameAndAccountId(const uint32_t &id, const std::string &name) {
auto result = g_database().storeQuery(fmt::format("SELECT * FROM `players` WHERE `account_id` = {} AND `name` = {}", id, g_database().escapeString(name)));
if (!result) {
g_logger().error("Failed to get character: [{}] from account: [{}]!", name, id);
return false;
}

return result->countResults() > 0;
phacUFPE marked this conversation as resolved.
Show resolved Hide resolved
}

bool AccountRepositoryDB::getPassword(const uint32_t &id, std::string &password) {
auto result = g_database().storeQuery(fmt::format("SELECT * FROM `accounts` WHERE `id` = {}", id));
if (!result) {
Expand Down Expand Up @@ -153,7 +163,7 @@
fmt::format("SELECT `name`, `deletion` FROM `players` WHERE `account_id` = {} ORDER BY `name` ASC", acc.id)
);

if (!result) {

Check warning on line 166 in src/account/account_repository_db.cpp

View workflow job for this annotation

GitHub Actions / Qodana for C/C++

misra-cpp2008-5-3-1

MISRA 5-3-1: Each operand of the ! operator, the logical && or the logical || operators shall have type bool
g_logger().error("Failed to load account[{}] players!", acc.id);
return false;
}
Expand Down
2 changes: 2 additions & 0 deletions src/account/account_repository_db.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class AccountRepositoryDB final : public AccountRepository {
bool loadBySession(const std::string &esseionKey, AccountInfo &acc) override;
bool save(const AccountInfo &accInfo) override;

bool getCharacterByNameAndAccountId(const uint32_t &id, const std::string &name) override;

bool getPassword(const uint32_t &id, std::string &password) override;

bool getCoins(const uint32_t &id, const uint8_t &type, uint32_t &coins) override;
Expand Down
7 changes: 6 additions & 1 deletion src/io/iologindata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include "enums/account_type.hpp"
#include "enums/account_errors.hpp"

bool IOLoginData::gameWorldAuthentication(const std::string &accountDescriptor, const std::string &password, std::string &characterName, uint32_t &accountId, bool oldProtocol) {
bool IOLoginData::gameWorldAuthentication(const std::string &accountDescriptor, const std::string &password, std::string &characterName, uint32_t &accountId, bool oldProtocol, const uint32_t ip) {
Account account(accountDescriptor);
account.setProtocolCompat(oldProtocol);

Expand All @@ -38,6 +38,11 @@ bool IOLoginData::gameWorldAuthentication(const std::string &accountDescriptor,
}
}

if (!g_accountRepository().getCharacterByNameAndAccountId(account.getID(), characterName)) {
g_logger().warn("IP [{}] trying to connect into another account character", convertIPToString(ip));
return false;
}

if (AccountErrors_t::Ok != enumFromValue<AccountErrors_t>(account.load())) {
g_logger().error("Failed to load account [{}]", accountDescriptor);
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/io/iologindata.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ using ItemBlockList = std::list<std::pair<int32_t, std::shared_ptr<Item>>>;

class IOLoginData {
public:
static bool gameWorldAuthentication(const std::string &accountDescriptor, const std::string &sessionOrPassword, std::string &characterName, uint32_t &accountId, bool oldProcotol);
static bool gameWorldAuthentication(const std::string &accountDescriptor, const std::string &sessionOrPassword, std::string &characterName, uint32_t &accountId, bool oldProcotol, const uint32_t ip);
static uint8_t getAccountType(uint32_t accountId);
static void updateOnlineStatus(uint32_t guid, bool login);
static bool loadPlayerById(std::shared_ptr<Player> player, uint32_t id, bool disableIrrelevantInfo = true);
Expand Down
2 changes: 1 addition & 1 deletion src/server/network/protocol/protocolgame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -841,7 +841,7 @@ void ProtocolGame::onRecvFirstMessage(NetworkMessage &msg) {
}

uint32_t accountId;
if (!IOLoginData::gameWorldAuthentication(accountDescriptor, password, characterName, accountId, oldProtocol)) {
if (!IOLoginData::gameWorldAuthentication(accountDescriptor, password, characterName, accountId, oldProtocol, getIP())) {
ss.str(std::string());
if (authType == "session") {
ss << "Your session has expired. Please log in again.";
Expand Down
Loading