From e4eb9a6e38991c513ae4d5712abce8a06f936795 Mon Sep 17 00:00:00 2001 From: Lion Kortlepel Date: Sun, 3 Mar 2024 12:31:50 +0100 Subject: [PATCH] fixup identity --- CMakeLists.txt | 3 +- src/Identity.cpp | 114 ++--------------------------------------------- src/Identity.h | 5 +-- 3 files changed, 7 insertions(+), 115 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f8c6d02..d7971d9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/src/Identity.cpp b/src/Identity.cpp index 88d7fb5..a576c8a 100644 --- a/src/Identity.cpp +++ b/src/Identity.cpp @@ -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(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()) { - LoginAuth = true; - spdlog::info("{}", d["message"].get()); - update_key(d["private_key"].get().c_str()); - PublicKey = d["public_key"].get(); - Username = d["username"].get(); - Role = d["role"].get(); - } 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()) { - LoginAuth = true; - if (d.contains("private_key")) { - update_key(d["private_key"].get().c_str()); - } - if (d.contains("public_key")) { - PublicKey = d["public_key"].get(); - } - spdlog::info("Authentication successful!"); - } else { - spdlog::info("Authentication failed!"); - } - d.erase("private_key"); - d.erase("public_key"); - return d.dump(); -} - Result ident::login_cached() noexcept { std::string private_key; try { @@ -139,14 +32,15 @@ Result 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::login(const std::string& username_or_email, const std::string& password) { +Result 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::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 { diff --git a/src/Identity.h b/src/Identity.h index aed6e27..e20e4c4 100644 --- a/src/Identity.h +++ b/src/Identity.h @@ -1,7 +1,7 @@ #pragma once +#include "Result.h" #include #include -#include "Result.h" namespace ident { @@ -20,7 +20,7 @@ bool is_login_cached() noexcept; Result login_cached() noexcept; -Result login(const std::string& username_or_email, const std::string& password); +Result login(const std::string& username_or_email, const std::string& password, bool remember); namespace detail { @@ -31,4 +31,3 @@ namespace detail { } } -