diff options
| author | Aotrix <[email protected]> | 2026-01-25 02:54:06 +0100 |
|---|---|---|
| committer | Aotrix <[email protected]> | 2026-01-25 02:54:06 +0100 |
| commit | 9465c64a517181b650c3b476752536ed9c49befb (patch) | |
| tree | 5e2f1cd80ce4663cfce0b5ed865f86dfaf9bfe3b /battleNetApi/src/token.cpp | |
| parent | 15de61ad9fead748287537cdcc11fc82683a09ef (diff) | |
fixed token generation when content length is inferior to min
Diffstat (limited to 'battleNetApi/src/token.cpp')
| -rw-r--r-- | battleNetApi/src/token.cpp | 25 |
1 files changed, 17 insertions, 8 deletions
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 <chrono> +#include <cstdint> #include <curl/curl.h> #include <format> #include <fstream> #include <iostream> #include <nlohmann/json.hpp> +#include <utility> 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<std::uint64_t>( @@ -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<std::string, std::uint64_t> kTokenContent{getTokenWithApi()}; writeNewTokenToLocalFile(kTokenContent); } return getTokenFromLocalFile(); } -void Token::writeNewTokenToLocalFile(const std::string &iTokenContent) const { +void Token::writeNewTokenToLocalFile(const std::pair<std::string, std::uint64_t> &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<std::chrono::seconds>(getCurrentTime() + std::chrono::hours(24)).count(); + std::chrono::duration_cast<std::chrono::seconds>(getCurrentTime() + std::chrono::seconds(iToken.second)) + .count(); std::ofstream aTokenFileOutput{mFilePath}; aTokenFileOutput << std::setw(4) << aCredentialsFile; } -std::string Token::getTokenWithApi() const { +std::pair<std::string, std::uint64_t> 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 { |
