summaryrefslogtreecommitdiff
path: root/battleNetApi/src/token.cpp
diff options
context:
space:
mode:
authorAotrix <[email protected]>2026-04-23 02:48:00 +0200
committerAotrix <[email protected]>2026-04-23 02:48:00 +0200
commit5582e994d39b16a6d61f05b13e78c30f4bac5d84 (patch)
treef1bb074a61fad89d072d007b8a19d3ca9f446d4f /battleNetApi/src/token.cpp
parentcc436cb44c60ef2485321e1e22b3ea0a59ee22af (diff)
Create TokenBuilder class
Diffstat (limited to 'battleNetApi/src/token.cpp')
-rw-r--r--battleNetApi/src/token.cpp48
1 files changed, 31 insertions, 17 deletions
diff --git a/battleNetApi/src/token.cpp b/battleNetApi/src/token.cpp
index 21717cb..1f952ff 100644
--- a/battleNetApi/src/token.cpp
+++ b/battleNetApi/src/token.cpp
@@ -10,23 +10,28 @@
using json = nlohmann::json;
-Token::Token(const std::string &iTokenFilePath) : mFilePath{iTokenFilePath}, mToken{generateToken()} {}
+Token::Token(const std::string &iTokenFilePath)
+ : mFilePath{iTokenFilePath}, mToken{generateToken()} {}
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
+ 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) {
+ if (aCredentialsFile["Token"]["Content"].dump().length() <
+ kTokenContentMinLength) {
return true;
}
- const std::uint64_t kExpirationTime{aCredentialsFile["Token"]["ExpirationTime"]};
+ const std::uint64_t kExpirationTime{
+ aCredentialsFile["Token"]["ExpirationTime"]};
const std::uint64_t kCurrentTime{static_cast<std::uint64_t>(
- std::chrono::duration_cast<std::chrono::seconds>(std::chrono::high_resolution_clock::now().time_since_epoch())
+ std::chrono::duration_cast<std::chrono::seconds>(
+ std::chrono::high_resolution_clock::now().time_since_epoch())
.count())};
if (kCurrentTime >= kExpirationTime) {
return true;
@@ -34,8 +39,6 @@ const bool Token::hasExpired() const {
return false;
}
-void Token::updateToken() { mToken = generateToken(); }
-
std::string Token::getTokenFromLocalFile() const {
std::ifstream aTokenFile{mFilePath};
return json::parse(aTokenFile)["Token"]["Content"];
@@ -43,18 +46,21 @@ std::string Token::getTokenFromLocalFile() const {
std::string Token::generateToken() {
if (hasExpired()) {
- const std::pair<std::string, std::uint64_t> kTokenContent{getTokenWithApi()};
+ const std::pair<std::string, std::uint64_t> kTokenContent{
+ getTokenWithApi()};
writeTokenToLocalFile(kTokenContent);
}
return getTokenFromLocalFile();
}
-void Token::writeTokenToLocalFile(const std::pair<std::string, std::uint64_t> &iToken) const {
+void Token::writeTokenToLocalFile(
+ const std::pair<std::string, std::uint64_t> &iToken) const {
std::ifstream aTokenFileInput{mFilePath};
json aCredentialsFile = json::parse(aTokenFileInput);
aCredentialsFile["Token"]["Content"] = iToken.first;
aCredentialsFile["Token"]["ExpirationTime"] =
- std::chrono::duration_cast<std::chrono::seconds>(getCurrentTime() + std::chrono::seconds(iToken.second))
+ std::chrono::duration_cast<std::chrono::seconds>(
+ getCurrentTime() + std::chrono::seconds(iToken.second))
.count();
std::ofstream aTokenFileOutput{mFilePath};
@@ -64,9 +70,11 @@ void Token::writeTokenToLocalFile(const std::pair<std::string, std::uint64_t> &i
std::pair<std::string, std::uint64_t> Token::getTokenWithApi() const {
std::ifstream aCredentialsFile{mFilePath};
json aCredentials = json::parse(aCredentialsFile);
- if (not aCredentials.contains("Agent") or not aCredentials["Agent"].contains("Username") or
+ 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::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"]};
@@ -77,10 +85,14 @@ std::pair<std::string, std::uint64_t> Token::getTokenWithApi() const {
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_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");
+ 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) {
@@ -93,10 +105,12 @@ std::pair<std::string, std::uint64_t> Token::getTokenWithApi() const {
}
std::chrono::seconds Token::getCurrentTime() const {
- return std::chrono::duration_cast<std::chrono::seconds>(std::chrono::utc_clock::now().time_since_epoch());
+ return std::chrono::duration_cast<std::chrono::seconds>(
+ 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 Token::curlCallbackStringOutput(void *iPtr, size_t iSize, size_t iNmemb,
+ std::string *iUserData) {
size_t newLength{iSize * iNmemb};
try {
iUserData->append(static_cast<char *>(iPtr), newLength);