fixup identity

This commit is contained in:
Lion Kortlepel
2024-03-03 12:31:50 +01:00
parent 874f381d43
commit e4eb9a6e38
3 changed files with 7 additions and 115 deletions

View File

@@ -73,7 +73,6 @@ set(PRJ_LIBRARIES
Boost::iostreams
Boost::thread
Boost::filesystem
Boost::outcome
cryptopp::cryptopp
ZLIB::ZLIB
OpenSSL::SSL
@@ -87,7 +86,7 @@ find_package(fmt CONFIG REQUIRED)
find_package(doctest CONFIG REQUIRED)
find_package(spdlog CONFIG REQUIRED)
find_package(httplib CONFIG REQUIRED)
find_package(Boost REQUIRED COMPONENTS system iostreams thread filesystem outcome)
find_package(Boost REQUIRED COMPONENTS system iostreams thread filesystem)
find_package(cryptopp CONFIG REQUIRED)
find_package(ZLIB REQUIRED)
find_package(OpenSSL REQUIRED)

View File

@@ -8,113 +8,6 @@
namespace fs = std::filesystem;
Identity::Identity() {
check_local_key();
}
void Identity::check_local_key() {
if (fs::exists("key") && fs::file_size("key") < 100) {
std::ifstream Key("key");
if (Key.is_open()) {
auto Size = fs::file_size("key");
std::string Buffer(Size, 0);
Key.read(&Buffer[0], static_cast<long>(Size));
Key.close();
for (char& c : Buffer) {
if (!std::isalnum(c) && c != '-') {
update_key(nullptr);
return;
}
}
Buffer = HTTP::Post("https://auth.beammp.com/userlogin", R"({"pk":")" + Buffer + "\"}");
nlohmann::json d = nlohmann::json::parse(Buffer, nullptr, false);
if (Buffer == "-1" || Buffer.at(0) != '{' || d.is_discarded()) {
spdlog::error(Buffer);
spdlog::info("Invalid answer from authentication servers. Check your internet connection and see if you can reach https://beammp.com.");
update_key(nullptr);
}
if (d["success"].get<bool>()) {
LoginAuth = true;
spdlog::info("{}", d["message"].get<std::string>());
update_key(d["private_key"].get<std::string>().c_str());
PublicKey = d["public_key"].get<std::string>();
Username = d["username"].get<std::string>();
Role = d["role"].get<std::string>();
} else {
spdlog::info("Auto-Authentication unsuccessful please re-login!");
update_key(nullptr);
}
} else {
spdlog::warn("Could not open saved key!");
update_key(nullptr);
}
} else
update_key(nullptr);
}
void Identity::update_key(const char* newKey) {
if (newKey && std::isalnum(newKey[0])) {
PrivateKey = newKey;
std::ofstream Key("key");
if (Key.is_open()) {
Key << newKey;
Key.close();
} else {
spdlog::error("Cannot write key to disk!");
}
} else if (fs::exists("key")) {
fs::remove("key");
}
}
static std::string GetFail(const std::string& R) {
std::string DRet = R"({"success":false,"message":)";
DRet += "\"" + R + "\"}";
spdlog::error(R);
return DRet;
}
std::string Identity::login(const std::string& fields) {
spdlog::debug("Logging in with {}", fields);
if (fields == "LO") {
LoginAuth = false;
update_key(nullptr);
return "";
}
spdlog::info("Attempting to authenticate...");
std::string Buffer = HTTP::Post("https://auth.beammp.com/userlogin", fields);
if (Buffer == "-1") {
return GetFail("Failed to communicate with the auth system!");
}
nlohmann::json d = nlohmann::json::parse(Buffer, nullptr, false);
if (Buffer.at(0) != '{' || d.is_discarded()) {
spdlog::error(Buffer);
return GetFail("Invalid answer from authentication servers, please try again later!");
}
if (d.contains("success") && d["success"].get<bool>()) {
LoginAuth = true;
if (d.contains("private_key")) {
update_key(d["private_key"].get<std::string>().c_str());
}
if (d.contains("public_key")) {
PublicKey = d["public_key"].get<std::string>();
}
spdlog::info("Authentication successful!");
} else {
spdlog::info("Authentication failed!");
}
d.erase("private_key");
d.erase("public_key");
return d.dump();
}
Result<ident::Identity, std::string> ident::login_cached() noexcept {
std::string private_key;
try {
@@ -139,14 +32,15 @@ Result<ident::Identity, std::string> ident::login_cached() noexcept {
return std::string("Invalid login saved, please log in again.");
}
return detail::login(pk_json);
// login and remember (again)
return detail::login(pk_json, true);
}
bool ident::is_login_cached() noexcept {
return std::filesystem::exists(KEYFILE) && std::filesystem::is_regular_file(KEYFILE);
}
Result<ident::Identity, std::string> ident::login(const std::string& username_or_email, const std::string& password) {
Result<ident::Identity, std::string> ident::login(const std::string& username_or_email, const std::string& password, bool remember) {
std::string login_json {};
try {
@@ -160,7 +54,7 @@ Result<ident::Identity, std::string> ident::login(const std::string& username_or
return std::string("Username or password contain illegal characters, please try again.");
}
return detail::login(login_json);
return detail::login(login_json, remember);
}
void ident::detail::cache_key(const std::string& private_key) noexcept {

View File

@@ -1,7 +1,7 @@
#pragma once
#include "Result.h"
#include <filesystem>
#include <string>
#include "Result.h"
namespace ident {
@@ -20,7 +20,7 @@ bool is_login_cached() noexcept;
Result<Identity, std::string> login_cached() noexcept;
Result<Identity, std::string> login(const std::string& username_or_email, const std::string& password);
Result<Identity, std::string> login(const std::string& username_or_email, const std::string& password, bool remember);
namespace detail {
@@ -31,4 +31,3 @@ namespace detail {
}
}