From 9465c64a517181b650c3b476752536ed9c49befb Mon Sep 17 00:00:00 2001 From: Aotrix <9aotrix@protonmail.com> Date: Sun, 25 Jan 2026 02:54:06 +0100 Subject: fixed token generation when content length is inferior to min --- battleNetApi/src/token.cpp | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) (limited to 'battleNetApi/src') diff --git a/battleNetApi/src/token.cpp b/battleNetApi/src/token.cpp index de0276a..e32a946 100644 --- a/battleNetApi/src/token.cpp +++ b/battleNetApi/src/token.cpp @@ -1,10 +1,12 @@ #include "token.hpp" #include +#include #include #include #include #include #include +#include using json = nlohmann::json; @@ -16,8 +18,11 @@ const bool Token::hasTokenExpired() const { std::ifstream aTokenFile{mFilePath}; json aCredentialsFile = json::parse(aTokenFile); if (not aCredentialsFile.contains("Token") or not aCredentialsFile["Token"].contains("ExpirationTime") or - not aCredentialsFile["Token"].contains("Content") or aCredentialsFile["Token"]["Content"].empty()) { - return false; + not aCredentialsFile["Token"].contains("Content")) { + return true; + } + if (aCredentialsFile["Token"]["Content"].dump().length() < kTokenContentMinLength) { + return true; } const std::uint64_t kExpirationTime{aCredentialsFile["Token"]["ExpirationTime"]}; const std::uint64_t kCurrentTime{static_cast( @@ -29,6 +34,8 @@ const bool Token::hasTokenExpired() const { return false; } +void Token::updateToken() { mToken = generateToken(); } + std::string Token::getTokenFromLocalFile() const { std::ifstream aTokenFile{mFilePath}; return json::parse(aTokenFile)["Token"]["Content"]; @@ -36,24 +43,25 @@ std::string Token::getTokenFromLocalFile() const { std::string Token::generateToken() { if (hasTokenExpired()) { - const std::string kTokenContent{getTokenWithApi()}; + const std::pair kTokenContent{getTokenWithApi()}; writeNewTokenToLocalFile(kTokenContent); } return getTokenFromLocalFile(); } -void Token::writeNewTokenToLocalFile(const std::string &iTokenContent) const { +void Token::writeNewTokenToLocalFile(const std::pair &iToken) const { std::ifstream aTokenFileInput{mFilePath}; json aCredentialsFile = json::parse(aTokenFileInput); - aCredentialsFile["Token"]["Content"] = iTokenContent; + aCredentialsFile["Token"]["Content"] = iToken.first; aCredentialsFile["Token"]["ExpirationTime"] = - std::chrono::duration_cast(getCurrentTime() + std::chrono::hours(24)).count(); + std::chrono::duration_cast(getCurrentTime() + std::chrono::seconds(iToken.second)) + .count(); std::ofstream aTokenFileOutput{mFilePath}; aTokenFileOutput << std::setw(4) << aCredentialsFile; } -std::string Token::getTokenWithApi() const { +std::pair Token::getTokenWithApi() const { std::ifstream aCredentialsFile{mFilePath}; json aCredentials = json::parse(aCredentialsFile); if (not aCredentials.contains("Agent") or not aCredentials["Agent"].contains("Username") or @@ -80,7 +88,8 @@ std::string Token::getTokenWithApi() const { std::exit(EXIT_FAILURE); } } - return json::parse(aTokenInfo)["access_token"]; + json aTokenJson = json::parse(aTokenInfo); + return std::make_pair(aTokenJson["access_token"], aTokenJson["expires_in"]); } std::chrono::seconds Token::getCurrentTime() const { -- cgit v1.2.3