diff options
| author | Aotrix <[email protected]> | 2025-11-01 19:04:16 +0100 |
|---|---|---|
| committer | Aotrix <[email protected]> | 2025-11-01 19:04:16 +0100 |
| commit | 3baa8fa322dbc8f2a2183e8ac027387c75eb9180 (patch) | |
| tree | e70c83e40d762d34451353efedd6413af8e3bc5c /subprojects/battleNetApi/src/token.cpp | |
| parent | db533729ce52c9c7a5d1de5393426c0f76de3693 (diff) | |
added Token class
Diffstat (limited to 'subprojects/battleNetApi/src/token.cpp')
| -rw-r--r-- | subprojects/battleNetApi/src/token.cpp | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/subprojects/battleNetApi/src/token.cpp b/subprojects/battleNetApi/src/token.cpp new file mode 100644 index 0000000..7d2e20e --- /dev/null +++ b/subprojects/battleNetApi/src/token.cpp @@ -0,0 +1,96 @@ +#include "token.hpp" +#include <chrono> +#include <curl/curl.h> +#include <format> +#include <fstream> +#include <iostream> +#include <regex> + +Token::Token(const std::string &iTokenFilePath) : mFilePath{iTokenFilePath} {} + +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; +} + +void Token::refreshToken() const { + std::string file_path = std::format("{}/resources/token_info.txt", SOURCE_DIR_PATH); + std::ifstream token_file(file_path); + std::string line; + std::getline(token_file, line); + if (line.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(); + } + } +} + +void Token::writeNewTokenToFile() const { + std::string aToken{getValidTokenWithApi()}; + writeTokenToFile(aToken); +} + +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::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, + "2b50f1123b424d5e9c21971a110fdd04:" + "MrMkhVMNdOeqPL4Q4XVQAsRPR14J7Yq6"); + curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "grant_type=client_credentials"); + CURLcode result_code; + result_code = curl_easy_perform(curl); + curl_easy_cleanup(curl); + if (result_code != CURLE_OK) { + std::cerr << "ERROR: token request failed" << std::endl; + std::exit(EXIT_FAILURE); + } + } + + return extractFirstMatchRegex(token_info, "\"access_token\":\"(\\w+)\""); +} + +std::chrono::seconds Token::getCurrentTime() const { + return std::chrono::duration_cast<std::chrono::seconds>(std::chrono::utc_clock::now().time_since_epoch()); +} + +std::string Token::extractFirstMatchRegex(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(); + } + 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; + try { + t_s->append(static_cast<char *>(t_contents), newLength); + } catch (std::bad_alloc &e) { + // handle memory problem + return 0; + } + return newLength; +} |
