summaryrefslogtreecommitdiff
path: root/battleNetApi/include/token.hpp
blob: 23d1f496b147d09abc5378d0d51b71d45a9d745b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#pragma once

#include <chrono>
#include <string>

class Token {
  public:
    Token(const std::string &iTokenFilePath);
    std::string text() const;
    const bool hasExpired() const;
    void updateToken();

  private:
    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
    std::string mToken;          //!< Token value

    /**
     * @return An access token that a user can use to perform API requests.
     * First, the cache file is checked.
     * A new token is computed if the current token lifetime expired,
     * or if there is no token at all.
     */
    std::string generateToken();
    void writeTokenToLocalFile(const std::pair<std::string, std::uint64_t> &iToken) const;
    std::string getTokenFromLocalFile() const;
    std::pair<std::string, std::uint64_t> getTokenWithApi() const;
    std::chrono::seconds getCurrentTime() const;
    static size_t curlCallbackStringOutput(void *iPtr, size_t iSize, size_t iNmemb, std::string *iUserData);

    static const std::uint16_t kTokenContentMinLength{10};
};