diff options
| -rw-r--r-- | subprojects/battleNetApi/include/token.hpp | 4 | ||||
| -rw-r--r-- | subprojects/battleNetApi/src/token.cpp | 82 |
2 files changed, 46 insertions, 40 deletions
diff --git a/subprojects/battleNetApi/include/token.hpp b/subprojects/battleNetApi/include/token.hpp index def5dc2..e2e3088 100644 --- a/subprojects/battleNetApi/include/token.hpp +++ b/subprojects/battleNetApi/include/token.hpp @@ -18,8 +18,8 @@ class Token { void writeTokenToFile(const std::string &iToken) const; std::string getValidTokenWithApi() const; std::chrono::seconds getCurrentTime() const; - std::string extractTokenWithRegex(const std::string &t_str, const std::string &t_regex) const; - static size_t curlCallbackStringOutput(void *t_contents, size_t t_size, size_t t_nmemb, std::string *t_s); + 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); friend class TokenTests; }; diff --git a/subprojects/battleNetApi/src/token.cpp b/subprojects/battleNetApi/src/token.cpp index d3b3054..5a0d6fa 100644 --- a/subprojects/battleNetApi/src/token.cpp +++ b/subprojects/battleNetApi/src/token.cpp @@ -1,10 +1,12 @@ #include "token.hpp" +#include <charconv> #include <chrono> #include <curl/curl.h> #include <format> #include <fstream> #include <iostream> #include <regex> +#include <system_error> Token::Token(const std::string &iTokenFilePath, const std::string &iUsername, const std::string &iPassword) : mFilePath{iTokenFilePath}, mUsername{iUsername}, mPassword{iPassword}, mToken{getValidToken()} {} @@ -13,24 +15,29 @@ std::string Token::toString() const { return mToken; } std::string Token::getValidToken() const { refreshToken(); - std::ifstream token_file(mFilePath); - std::string line; - std::getline(token_file, line); - std::string token = line.substr(0, line.find(" ")); - return token; + std::ifstream aTokenFile(mFilePath); + std::string aLine; + std::getline(aTokenFile, aLine); + const std::string kToken = aLine.substr(0, aLine.find(" ")); + return kToken; } void Token::refreshToken() const { - std::ifstream token_file{mFilePath}; - std::string line; - std::getline(token_file, line); - if (line.empty()) { + std::ifstream aTokenFile{mFilePath}; + std::string aLine; + std::getline(aTokenFile, aLine); + if (aLine.empty()) { writeNewTokenToFile(); } else { - long expire_at = std::stoull(line.substr(line.find(" ") + 1)); - if (std::chrono::duration_cast<std::chrono::seconds>(std::chrono::utc_clock::now().time_since_epoch()) - .count() >= expire_at) { - writeNewTokenToFile(); + const std::string aTimeLimitText{aLine.substr(aLine.find(" ") + 1)}; + long int aTimeLimit; + const auto kConversionStatus{ + std::from_chars(aTimeLimitText.data(), aTimeLimitText.data() + aTimeLimitText.size(), aTimeLimit)}; + if (kConversionStatus.ec == std::errc{}) { + if (std::chrono::duration_cast<std::chrono::seconds>(std::chrono::utc_clock::now().time_since_epoch()) + .count() >= aTimeLimit) { + writeNewTokenToFile(); + } } } } @@ -43,32 +50,32 @@ void Token::writeNewTokenToFile() const { } void Token::writeTokenToFile(const std::string &iToken) const { - std::ofstream token_file{mFilePath}; - std::chrono::seconds expires_at = getCurrentTime() + std::chrono::hours(24); - token_file << iToken << " " << expires_at.count() << '\n'; + std::ofstream aTokenFile{mFilePath}; + std::chrono::seconds aExpiresAt = getCurrentTime() + std::chrono::hours(24); + aTokenFile << iToken << " " << aExpiresAt.count() << '\n'; } std::string Token::getValidTokenWithApi() const { - CURL *curl = curl_easy_init(); - std::string token_info; - if (curl) { - curl_easy_setopt(curl, CURLOPT_URL, "https://oauth.battle.net/token"); - curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); - curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curlCallbackStringOutput); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, &token_info); - curl_easy_setopt(curl, CURLOPT_USERPWD, std::format("{}:{}", mUsername, mPassword).data()); - curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "grant_type=client_credentials"); + CURL *aCurl = curl_easy_init(); + std::string aTokenInfo; + if (aCurl) { + 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("{}:{}", mUsername, mPassword).data()); + curl_easy_setopt(aCurl, CURLOPT_POSTFIELDS, "grant_type=client_credentials"); CURLcode result_code; - result_code = curl_easy_perform(curl); - curl_easy_cleanup(curl); + 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); } } - return extractTokenWithRegex(token_info, "\"access_token\":\"(\\w+)\""); + return extractTokenWithRegex(aTokenInfo, "\"access_token\":\"(\\w+)\""); } std::chrono::seconds Token::getCurrentTime() const { @@ -76,20 +83,19 @@ std::chrono::seconds Token::getCurrentTime() const { } std::string Token::extractTokenWithRegex(const std::string &iText, const std::string &iRegex) const { - std::regex regex(iRegex); - auto aMatchBegin = std::sregex_iterator(iText.begin(), iText.end(), regex); - auto aMatchEnd = std::sregex_iterator(); - if (std::distance(aMatchBegin, aMatchEnd) > 0) { - std::smatch match = *aMatchBegin; - return match[1].str(); + const std::regex kRegex{iRegex}; + std::smatch aMatches; + if (std::regex_search(iText, aMatches, kRegex)) { + return aMatches[1].str(); } + std::cerr << std::format("Unable to retrieve token with regex '{}'", iRegex); return ""; } -size_t Token::curlCallbackStringOutput(void *t_contents, size_t t_size, size_t t_nmemb, std::string *t_s) { - size_t newLength = t_size * t_nmemb; +size_t Token::curlCallbackStringOutput(void *iPtr, size_t iSize, size_t iNmemb, std::string *iUserData) { + size_t newLength = iSize * iNmemb; try { - t_s->append(static_cast<char *>(t_contents), newLength); + iUserData->append(static_cast<char *>(iPtr), newLength); } catch (std::bad_alloc &e) { // handle memory problem return 0; |
