diff options
| author | Aotrix <[email protected]> | 2026-01-04 17:38:51 +0100 |
|---|---|---|
| committer | Aotrix <[email protected]> | 2026-01-04 17:38:51 +0100 |
| commit | 06109ce40a8b8a9eecf359a006e06227200e768b (patch) | |
| tree | d3ba493e887f61413a012d1e030e5ccb7b3a92e2 /battleNetApi/src/token.cpp | |
| parent | 89f360d5b14d183ae4f131852ad15fdbd080cc47 (diff) | |
Refactoring of Token class
Diffstat (limited to 'battleNetApi/src/token.cpp')
| -rw-r--r-- | battleNetApi/src/token.cpp | 68 |
1 files changed, 38 insertions, 30 deletions
diff --git a/battleNetApi/src/token.cpp b/battleNetApi/src/token.cpp index 89f0d2a..fdfb182 100644 --- a/battleNetApi/src/token.cpp +++ b/battleNetApi/src/token.cpp @@ -13,51 +13,59 @@ Token::Token(const std::string &iTokenFilePath, const std::string &iUsername, co std::string Token::toString() const { return mToken; } -std::string Token::getValidToken() const { - refreshToken(); +const bool Token::hasTokenExpired() const { std::ifstream aTokenFile{mFilePath}; std::string aLine; std::getline(aTokenFile, aLine); - const std::string kToken{aLine.substr(0, aLine.find(" "))}; - return kToken; + if (aLine.empty()) { + return true; + } + const std::string kTimeLimitText{aLine.substr(aLine.find(" ") + 1)}; + long int aTimeLimit; + const auto kConversionStatus{ + std::from_chars(kTimeLimitText.data(), kTimeLimitText.data() + kTimeLimitText.size(), aTimeLimit)}; + if (kConversionStatus.ec != std::errc{}) { + std::cerr << "Conversion of token timestamp failed.\n"; + return true; + } + const bool kTokenExpired{ + std::chrono::duration_cast<std::chrono::seconds>(std::chrono::utc_clock::now().time_since_epoch()).count() >= + aTimeLimit}; + if (kTokenExpired) { + return true; + } + return false; } -void Token::refreshToken() const { +std::string Token::getTokenFromLocalFile() const { std::ifstream aTokenFile{mFilePath}; std::string aLine; std::getline(aTokenFile, aLine); - if (aLine.empty()) { - writeNewTokenToFile(); - } else { - const std::string kTimeLimitText{aLine.substr(aLine.find(" ") + 1)}; - long int aTimeLimit; - const auto kConversionStatus{ - std::from_chars(kTimeLimitText.data(), kTimeLimitText.data() + kTimeLimitText.size(), aTimeLimit)}; - if (kConversionStatus.ec == std::errc{}) { - const bool kTokenExpired{ - std::chrono::duration_cast<std::chrono::seconds>(std::chrono::utc_clock::now().time_since_epoch()) - .count() >= aTimeLimit}; - if (kTokenExpired) { - writeNewTokenToFile(); - } - } - } + return aLine.substr(0, aLine.find(" ")); } -void Token::writeNewTokenToFile() const { - const std::string kToken{getValidTokenWithApi()}; - if (not kToken.empty()) { - writeTokenToFile(kToken); +std::string Token::getValidToken() { + if (hasTokenExpired()) { + return writeNewTokenToLocalFile(); } + return getTokenFromLocalFile(); } -void Token::writeTokenToFile(const std::string &iToken) const { - std::ofstream aTokenFile{mFilePath}; - std::chrono::seconds aExpiresAt{getCurrentTime() + std::chrono::hours(24)}; - aTokenFile << iToken << " " << aExpiresAt.count() << '\n'; +std::string Token::writeNewTokenToLocalFile() const { + std::fstream aTokenFile{mFilePath}; + std::string aLine; + std::getline(aTokenFile, aLine); + const std::string kToken{getTokenWithApi()}; + if (not kToken.empty()) { + std::chrono::seconds aExpiresAt{getCurrentTime() + std::chrono::hours(24)}; + aTokenFile << kToken << " " << aExpiresAt.count() << '\n'; + return kToken; + } + std::cerr << "Error: no valid token can be generated.\n"; + return ""; } -std::string Token::getValidTokenWithApi() const { +std::string Token::getTokenWithApi() const { CURL *aCurl{curl_easy_init()}; std::string aTokenInfo; if (aCurl) { |
