From a0632d299b85dc4ef7c0e03898f38e6deca0edc2 Mon Sep 17 00:00:00 2001 From: Aotrix <9aotrix@protonmail.com> Date: Tue, 28 Apr 2026 03:49:41 +0200 Subject: add HearthstoneTokenBuilder class --- battleNetApi/src/token.cpp | 121 ++------------------------------------------- 1 file changed, 3 insertions(+), 118 deletions(-) (limited to 'battleNetApi/src/token.cpp') diff --git a/battleNetApi/src/token.cpp b/battleNetApi/src/token.cpp index 1f952ff..b312cc1 100644 --- a/battleNetApi/src/token.cpp +++ b/battleNetApi/src/token.cpp @@ -1,122 +1,7 @@ #include "token.hpp" -#include -#include -#include -#include -#include -#include -#include -#include -using json = nlohmann::json; +Token::Token(const std::string &iTokenText) : mTokenText{iTokenText} {} -Token::Token(const std::string &iTokenFilePath) - : mFilePath{iTokenFilePath}, mToken{generateToken()} {} +std::string Token::text() const { return mTokenText; } -std::string Token::text() const { return mToken; } - -const bool Token::hasExpired() 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")) { - 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::chrono::duration_cast( - std::chrono::high_resolution_clock::now().time_since_epoch()) - .count())}; - if (kCurrentTime >= kExpirationTime) { - return true; - } - return false; -} - -std::string Token::getTokenFromLocalFile() const { - std::ifstream aTokenFile{mFilePath}; - return json::parse(aTokenFile)["Token"]["Content"]; -} - -std::string Token::generateToken() { - if (hasExpired()) { - const std::pair kTokenContent{ - getTokenWithApi()}; - writeTokenToLocalFile(kTokenContent); - } - return getTokenFromLocalFile(); -} - -void Token::writeTokenToLocalFile( - const std::pair &iToken) const { - std::ifstream aTokenFileInput{mFilePath}; - json aCredentialsFile = json::parse(aTokenFileInput); - aCredentialsFile["Token"]["Content"] = iToken.first; - aCredentialsFile["Token"]["ExpirationTime"] = - std::chrono::duration_cast( - getCurrentTime() + std::chrono::seconds(iToken.second)) - .count(); - - std::ofstream aTokenFileOutput{mFilePath}; - aTokenFileOutput << std::setw(4) << aCredentialsFile; -} - -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 - not aCredentials["Agent"].contains("Password")) { - std::cerr << "Please define Agent/Username and Agent/Password in the " - "JSON credentials file\n"; - std::exit(EXIT_FAILURE); - } - const std::string kAgentUsername{aCredentials["Agent"]["Username"]}; - const std::string kAgentPassword{aCredentials["Agent"]["Password"]}; - CURL *aCurl{curl_easy_init()}; - std::string aTokenInfo; - if (aCurl and not kAgentUsername.empty() and not kAgentPassword.empty()) { - curl_easy_setopt(aCurl, CURLOPT_URL, "https://oauth.battle.net/token"); - curl_easy_setopt(aCurl, CURLOPT_SSL_VERIFYPEER, 0L); - curl_easy_setopt(aCurl, CURLOPT_SSL_VERIFYHOST, 0L); - curl_easy_setopt(aCurl, CURLOPT_WRITEFUNCTION, - curlCallbackStringOutput); - curl_easy_setopt(aCurl, CURLOPT_WRITEDATA, &aTokenInfo); - curl_easy_setopt( - aCurl, CURLOPT_USERPWD, - std::format("{}:{}", kAgentUsername, kAgentPassword).data()); - curl_easy_setopt(aCurl, CURLOPT_POSTFIELDS, - "grant_type=client_credentials"); - CURLcode result_code{curl_easy_perform(aCurl)}; - curl_easy_cleanup(aCurl); - if (result_code != CURLE_OK) { - std::cerr << "ERROR: token request failed" << std::endl; - std::exit(EXIT_FAILURE); - } - } - json aTokenJson = json::parse(aTokenInfo); - return std::make_pair(aTokenJson["access_token"], aTokenJson["expires_in"]); -} - -std::chrono::seconds Token::getCurrentTime() const { - return std::chrono::duration_cast( - std::chrono::utc_clock::now().time_since_epoch()); -} - -size_t Token::curlCallbackStringOutput(void *iPtr, size_t iSize, size_t iNmemb, - std::string *iUserData) { - size_t newLength{iSize * iNmemb}; - try { - iUserData->append(static_cast(iPtr), newLength); - } catch (std::bad_alloc &e) { - // handle memory problem - return 0; - } - return newLength; -} +void Token::setText(const std::string &iTokenText) { mTokenText = iTokenText; } -- cgit v1.2.3