summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--battleNetApi/include/token.hpp16
-rw-r--r--battleNetApi/resources/token_info2
-rw-r--r--battleNetApi/src/token.cpp68
3 files changed, 49 insertions, 37 deletions
diff --git a/battleNetApi/include/token.hpp b/battleNetApi/include/token.hpp
index e2e3088..f529a08 100644
--- a/battleNetApi/include/token.hpp
+++ b/battleNetApi/include/token.hpp
@@ -10,13 +10,17 @@ class Token {
const std::string mFilePath; //!< Path to the token file
const std::string mUsername; //!< Username for Battle.net API
const std::string mPassword; //!< Password for Battle.net API
- const std::string mToken; //!< Token value
+ std::string mToken; //!< Token value
- std::string getValidToken() const;
- void refreshToken() const;
- void writeNewTokenToFile() const;
- void writeTokenToFile(const std::string &iToken) const;
- std::string getValidTokenWithApi() const;
+ /**
+ * @return A token that a user can use to perform API requests.
+ * A new token might be computed if the current token lifetime expired.
+ */
+ std::string getValidToken();
+ const bool hasTokenExpired() const;
+ std::string writeNewTokenToLocalFile() const;
+ std::string getTokenFromLocalFile() const;
+ std::string getTokenWithApi() const;
std::chrono::seconds getCurrentTime() const;
std::string extractTokenWithRegex(const std::string &iText, const std::string &iRegex) const;
static size_t curlCallbackStringOutput(void *iPtr, size_t iSize, size_t iNmemb, std::string *iUserData);
diff --git a/battleNetApi/resources/token_info b/battleNetApi/resources/token_info
index a3da9f6..d29dfe5 100644
--- a/battleNetApi/resources/token_info
+++ b/battleNetApi/resources/token_info
@@ -1 +1 @@
-EUmsmWh2JRYNfdfZQ2YkIzF0BEtRD3jBL3 1767267228
+EUjGlU0RAL4P2D7Qd1j10cf4TgFY1w6xC1 1767629613
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) {