Centralize local account creation and create API endpoint for creation

This commit is contained in:
Akkadius 2019-07-07 04:32:59 -05:00
parent 4bc6493718
commit 392b328a95
8 changed files with 171 additions and 13 deletions

View File

@ -1,6 +1,7 @@
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
SET(eqlogin_sources
account_management.cpp
client.cpp
client_manager.cpp
database.cpp
@ -13,6 +14,7 @@ SET(eqlogin_sources
)
SET(eqlogin_headers
account_management.h
client.h
client_manager.h
database.h

View File

@ -0,0 +1,72 @@
/**
* EQEmulator: Everquest Server Emulator
* Copyright (C) 2001-2019 EQEmulator Development Team (https://github.com/EQEmu/Server)
*
* 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; version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY except by those people which sell it, which
* are required to give you total support for your newly bought product;
* 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 "account_management.h"
#include "login_server.h"
extern LoginServer server;
/**
* @param username
* @param password
* @return
*/
bool AccountManagement::CreateLocalLoginServerAccount(
std::string username,
std::string password
)
{
auto mode = server.options.GetEncryptionMode();
auto hash = eqcrypt_hash(username, password, mode);
LogInfo(
"Attempting to create local login account for user [{0}] encryption algorithm [{1}] ({2})",
username,
GetEncryptionByModeId(mode),
mode
);
unsigned int db_id = 0;
std::string db_loginserver = server.options.GetDefaultLoginServerName();
if (server.db->DoesLoginServerAccountExist(username, hash, db_loginserver, 1)) {
LogInfo(
"Attempting to create local login account for user [{0}] login [{1}] db_id [{2}] but already exists!",
username,
db_loginserver,
db_id
);
return false;
}
if (server.db->CreateLoginData(username, hash, db_loginserver, db_id)) {
LogInfo(
"Account creation success for user [{0}] encryption algorithm [{1}] ({2})",
username,
GetEncryptionByModeId(mode),
mode
);
return true;
}
LogError("Failed to create local login account for user [{0}]!", username);
return false;
}

View File

@ -0,0 +1,31 @@
/**
* EQEmulator: Everquest Server Emulator
* Copyright (C) 2001-2019 EQEmulator Development Team (https://github.com/EQEmu/Server)
*
* 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; version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY except by those people which sell it, which
* are required to give you total support for your newly bought product;
* 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
*
*/
#ifndef EQEMU_ACCOUNT_MANAGEMENT_H
#define EQEMU_ACCOUNT_MANAGEMENT_H
#include "iostream"
class AccountManagement {
public:
static bool CreateLocalLoginServerAccount(std::string username, std::string password);
};
#endif //EQEMU_ACCOUNT_MANAGEMENT_H

View File

@ -87,7 +87,7 @@ bool Client::Process()
}
if (server.options.IsTraceOn()) {
LogInfo("Server list request received from client.");
LogDebug("Server list request received from client.");
}
SendServerListPacket(*(uint32_t *) app->pBuffer);

View File

@ -230,4 +230,39 @@ bool eqcrypt_verify_hash(const std::string &username, const std::string &passwor
}
return false;
}
std::string GetEncryptionByModeId(uint32 mode) {
switch (mode) {
case EncryptionModeMD5:
return "MD5";
case EncryptionModeMD5PassUser:
return "MD5PassUser";
case EncryptionModeMD5UserPass:
return "MD5UserPass";
case EncryptionModeMD5Triple:
return "MD5Triple";
case EncryptionModeSHA:
return "SHA";
case EncryptionModeSHAPassUser:
return "SHAPassUser";
case EncryptionModeSHAUserPass:
return "SHAUserPass";
case EncryptionModeSHATriple:
return "SHATriple";
case EncryptionModeSHA512:
return "SHA512";
case EncryptionModeSHA512PassUser:
return "SHA512PassUser";
case EncryptionModeSHA512UserPass:
return "SHA512UserPass";
case EncryptionModeSHA512Triple:
return "SHA512Triple";
case EncryptionModeArgon2:
return "Argon2";
case EncryptionModeSCrypt:
return "SCrypt";
default:
return "";
}
}

View File

@ -21,6 +21,7 @@
#pragma once
#include <string>
#include "../common/types.h"
enum EncryptionMode
{
@ -40,6 +41,11 @@ enum EncryptionMode
EncryptionModeSCrypt = 14
};
/**
* @param mode
* @return
*/
std::string GetEncryptionByModeId(uint32 mode);
const char* eqcrypt_block(const char *buffer_in, size_t buffer_in_sz, char* buffer_out, bool enc);
std::string eqcrypt_hash(const std::string &username, const std::string &password, int mode);
bool eqcrypt_verify_hash(const std::string &username, const std::string &password, const std::string &pwhash, int mode);

View File

@ -24,6 +24,7 @@
#include "../common/util/uuid.h"
#include "login_server.h"
#include "loginserver_webserver.h"
#include "account_management.h"
extern LoginServer server;
@ -163,17 +164,7 @@ namespace LoginserverCommandHandler {
exit(1);
}
std::string user = cmd("--username").str();
std::string password = cmd("--password").str();
auto mode = server.options.GetEncryptionMode();
auto hash = eqcrypt_hash(user, password, mode);
unsigned int db_id = 0;
std::string db_login = server.options.GetDefaultLoginServerName();
if (!server.db->CreateLoginData(user, hash, db_login, db_id)) {
}
AccountManagement::CreateLocalLoginServerAccount(cmd("--username").str(), cmd("--password").str());
}
}

View File

@ -23,6 +23,7 @@
#include "login_server.h"
#include "../common/json/json.h"
#include "../common/string_util.h"
#include "account_management.h"
extern LoginServer server;
@ -38,7 +39,6 @@ namespace LoginserverWebserver {
api.Get(
"/servers/list", [](const httplib::Request &request, httplib::Response &res) {
LoginserverWebserver::TokenManager::AuthCanRead(request, res);
Json::Value response;
@ -60,6 +60,27 @@ namespace LoginserverWebserver {
LoginserverWebserver::SendResponse(response, res);
}
);
api.Post(
"/account/create", [](const httplib::Request &request, httplib::Response &res) {
LoginserverWebserver::TokenManager::AuthCanWrite(request, res);
Json::Value response;
bool account_created = AccountManagement::CreateLocalLoginServerAccount(
request.get_param_value("username"),
request.get_param_value("password")
);
if (account_created) {
response["message"] = "Account created successfully!";
}
else {
response["message"] = "Account failed to create!";
}
LoginserverWebserver::SendResponse(response, res);
}
);
}
/**