|
1 | | -#pragma once |
2 | | - |
3 | | -#include <cstdint> |
4 | | -#include <exception> |
5 | | -#include <functional> |
6 | | -#include <mutex> |
7 | | -#include <optional> |
8 | | -#include <unordered_set> |
9 | | -#include <string> |
10 | | -#include <utility> |
11 | | -#include <vector> |
12 | | - |
13 | | -namespace authforge { |
14 | | - |
15 | | -class AuthForgeClient { |
16 | | -public: |
17 | | - static constexpr const char *kDefaultApiBaseUrl = "https://auth.authforge.cc"; |
18 | | - |
19 | | - AuthForgeClient( |
20 | | - std::string appId, |
21 | | - std::string appSecret, |
22 | | - std::string publicKey, |
23 | | - std::string heartbeatMode, |
24 | | - int heartbeatInterval = 900, |
25 | | - std::string apiBaseUrl = kDefaultApiBaseUrl, |
26 | | - std::function<void(const std::string &, const std::exception *)> onFailure = nullptr, |
27 | | - int requestTimeout = 15); |
28 | | - |
29 | | - bool Login(const std::string &licenseKey); |
30 | | - void Logout(); |
31 | | - bool IsAuthenticated() const; |
32 | | - std::optional<std::string> GetSessionDataJson() const; |
33 | | - std::optional<std::string> GetAppVariablesJson() const; |
34 | | - std::optional<std::string> GetLicenseVariablesJson() const; |
35 | | - |
36 | | -private: |
37 | | - struct JsonValue { |
38 | | - bool exists = false; |
39 | | - bool isString = false; |
40 | | - std::string value; |
41 | | - }; |
42 | | - |
43 | | - enum class SigningContext { Validate, Heartbeat }; |
44 | | - |
45 | | - void StartHeartbeatOnce(); |
46 | | - void HeartbeatLoop() noexcept; |
47 | | - void ServerHeartbeat(); |
48 | | - void LocalHeartbeat(); |
49 | | - void ValidateAndStore(const std::string &licenseKey); |
50 | | - void ApplySignedResponse(const std::string &responseJson, const std::string &expectedNonce, const std::optional<std::string> &licenseKey, SigningContext context); |
51 | | - |
52 | | - std::string PostJson(const std::string &path, const std::string &bodyJson, std::string *usedNonce = nullptr) const; |
53 | | - std::string ExtractServerError(const std::string &responseJson) const; |
54 | | - void Fail(const std::string &reason, const std::exception *exc = nullptr) const noexcept; |
55 | | - |
56 | | - std::string GetHwid() const; |
57 | | - std::string SafeMacAddress() const; |
58 | | - std::string SafeCpuInfo() const; |
59 | | - std::string SafeDiskSerial() const; |
60 | | - std::string RunCommand(const std::string &command) const; |
61 | | - |
62 | | - static bool ExtractJsonValue(const std::string &json, const std::string &key, JsonValue &outValue); |
63 | | - static std::optional<std::string> ExtractJsonString(const std::string &json, const std::string &key); |
64 | | - static std::optional<long long> ExtractJsonInt(const std::string &json, const std::string &key); |
65 | | - static std::string BuildJsonBody(const std::vector<std::pair<std::string, std::string>> &pairs); |
66 | | - static std::string EscapeJsonString(const std::string &value); |
67 | | - static std::string UnescapeJsonString(const std::string &value, bool &ok); |
68 | | - static std::string Trim(const std::string &value); |
69 | | - static std::string ToLower(std::string value); |
70 | | - |
71 | | - static std::string GenerateNonceHex32(); |
72 | | - static std::vector<unsigned char> Sha256Bytes(const std::string &input); |
73 | | - static std::string Sha256Hex(const std::string &input); |
74 | | - static std::string BytesToHexLower(const std::vector<unsigned char> &bytes); |
75 | | - static std::vector<unsigned char> DecodeBase64Any(const std::string &value); |
76 | | - static std::vector<unsigned char> DecodeBase64WithAlphabet(const std::string &value, bool urlSafe); |
77 | | - static std::string AddBase64Padding(const std::string &value); |
78 | | - static bool IsSuccessStatus(const JsonValue &status); |
79 | | - static std::optional<long long> ExtractExpiresInFromSessionToken(const std::string &sessionToken); |
80 | | - static std::optional<std::string> DecodeSessionTokenBody(const std::string &sessionToken); |
81 | | - void VerifySignature(const std::string &rawPayloadB64, const std::string &signature) const; |
82 | | - |
83 | | - std::string appId_; |
84 | | - std::string appSecret_; |
85 | | - std::string publicKey_; |
86 | | - std::string heartbeatMode_; |
87 | | - int heartbeatInterval_; |
88 | | - std::string apiBaseUrl_; |
89 | | - std::function<void(const std::string &, const std::exception *)> onFailure_; |
90 | | - int requestTimeout_; |
91 | | - |
92 | | - mutable std::mutex lock_; |
93 | | - bool heartbeatStarted_; |
94 | | - |
95 | | - std::string licenseKey_; |
96 | | - std::string sessionToken_; |
97 | | - std::optional<long long> sessionExpiresIn_; |
98 | | - std::string lastNonce_; |
99 | | - std::string rawPayloadB64_; |
100 | | - std::string signature_; |
101 | | - std::string keyId_; |
102 | | - std::vector<unsigned char> verifyPublicKeyBytes_; |
103 | | - std::string sessionDataJson_; |
104 | | - std::string appVariablesJson_; |
105 | | - std::string licenseVariablesJson_; |
106 | | - bool authenticated_ = false; |
107 | | - bool heartbeatStop_ = false; |
108 | | - std::string hwid_; |
109 | | - std::unordered_set<std::string> knownServerErrors_ = { |
110 | | - "invalid_app", |
111 | | - "invalid_key", |
112 | | - "expired", |
113 | | - "revoked", |
114 | | - "hwid_mismatch", |
115 | | - "no_credits", |
116 | | - "app_burn_cap_reached", |
117 | | - "blocked", |
118 | | - "rate_limited", |
119 | | - "replay_detected", |
120 | | - "app_disabled", |
121 | | - "session_expired", |
122 | | - "bad_request", |
123 | | - "system_error", |
124 | | - }; |
125 | | -}; |
126 | | - |
127 | | -} // namespace authforge |
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <cstdint> |
| 4 | +#include <exception> |
| 5 | +#include <functional> |
| 6 | +#include <mutex> |
| 7 | +#include <optional> |
| 8 | +#include <unordered_set> |
| 9 | +#include <string> |
| 10 | +#include <utility> |
| 11 | +#include <vector> |
| 12 | + |
| 13 | +namespace authforge { |
| 14 | + |
| 15 | +class AuthForgeClient { |
| 16 | +public: |
| 17 | + static constexpr const char *kDefaultApiBaseUrl = "https://auth.authforge.cc"; |
| 18 | + |
| 19 | + AuthForgeClient( |
| 20 | + std::string appId, |
| 21 | + std::string appSecret, |
| 22 | + std::string publicKey, |
| 23 | + std::string heartbeatMode, |
| 24 | + int heartbeatInterval = 900, |
| 25 | + std::string apiBaseUrl = kDefaultApiBaseUrl, |
| 26 | + std::function<void(const std::string &, const std::exception *)> onFailure = nullptr, |
| 27 | + int requestTimeout = 15, |
| 28 | + int ttlSeconds = 0); |
| 29 | + |
| 30 | + bool Login(const std::string &licenseKey); |
| 31 | + void Logout(); |
| 32 | + bool IsAuthenticated() const; |
| 33 | + std::optional<std::string> GetSessionDataJson() const; |
| 34 | + std::optional<std::string> GetAppVariablesJson() const; |
| 35 | + std::optional<std::string> GetLicenseVariablesJson() const; |
| 36 | + |
| 37 | +private: |
| 38 | + struct JsonValue { |
| 39 | + bool exists = false; |
| 40 | + bool isString = false; |
| 41 | + std::string value; |
| 42 | + }; |
| 43 | + |
| 44 | + enum class SigningContext { Validate, Heartbeat }; |
| 45 | + |
| 46 | + void StartHeartbeatOnce(); |
| 47 | + void HeartbeatLoop() noexcept; |
| 48 | + void ServerHeartbeat(); |
| 49 | + void LocalHeartbeat(); |
| 50 | + void ValidateAndStore(const std::string &licenseKey); |
| 51 | + void ApplySignedResponse(const std::string &responseJson, const std::string &expectedNonce, const std::optional<std::string> &licenseKey, SigningContext context); |
| 52 | + |
| 53 | + std::string PostJson(const std::string &path, const std::string &bodyJson, std::string *usedNonce = nullptr) const; |
| 54 | + std::string ExtractServerError(const std::string &responseJson) const; |
| 55 | + void Fail(const std::string &reason, const std::exception *exc = nullptr) const noexcept; |
| 56 | + |
| 57 | + std::string GetHwid() const; |
| 58 | + std::string SafeMacAddress() const; |
| 59 | + std::string SafeCpuInfo() const; |
| 60 | + std::string SafeDiskSerial() const; |
| 61 | + std::string RunCommand(const std::string &command) const; |
| 62 | + |
| 63 | + static bool ExtractJsonValue(const std::string &json, const std::string &key, JsonValue &outValue); |
| 64 | + static std::optional<std::string> ExtractJsonString(const std::string &json, const std::string &key); |
| 65 | + static std::optional<long long> ExtractJsonInt(const std::string &json, const std::string &key); |
| 66 | + static std::string BuildJsonBody(const std::vector<std::pair<std::string, std::string>> &pairs); |
| 67 | + static std::string EscapeJsonString(const std::string &value); |
| 68 | + static std::string UnescapeJsonString(const std::string &value, bool &ok); |
| 69 | + static std::string Trim(const std::string &value); |
| 70 | + static std::string ToLower(std::string value); |
| 71 | + |
| 72 | + static std::string GenerateNonceHex32(); |
| 73 | + static std::vector<unsigned char> Sha256Bytes(const std::string &input); |
| 74 | + static std::string Sha256Hex(const std::string &input); |
| 75 | + static std::string BytesToHexLower(const std::vector<unsigned char> &bytes); |
| 76 | + static std::vector<unsigned char> DecodeBase64Any(const std::string &value); |
| 77 | + static std::vector<unsigned char> DecodeBase64WithAlphabet(const std::string &value, bool urlSafe); |
| 78 | + static std::string AddBase64Padding(const std::string &value); |
| 79 | + static bool IsSuccessStatus(const JsonValue &status); |
| 80 | + static std::optional<long long> ExtractExpiresInFromSessionToken(const std::string &sessionToken); |
| 81 | + static std::optional<std::string> DecodeSessionTokenBody(const std::string &sessionToken); |
| 82 | + void VerifySignature(const std::string &rawPayloadB64, const std::string &signature) const; |
| 83 | + |
| 84 | + std::string appId_; |
| 85 | + std::string appSecret_; |
| 86 | + std::string publicKey_; |
| 87 | + std::string heartbeatMode_; |
| 88 | + int heartbeatInterval_; |
| 89 | + std::string apiBaseUrl_; |
| 90 | + std::function<void(const std::string &, const std::exception *)> onFailure_; |
| 91 | + int requestTimeout_; |
| 92 | + // Requested session token lifetime in seconds for /auth/validate. 0 means |
| 93 | + // "let the server pick its default" (24h today). Server clamps to |
| 94 | + // [3600, 604800]; preserved across heartbeat refreshes. |
| 95 | + int ttlSeconds_; |
| 96 | + |
| 97 | + mutable std::mutex lock_; |
| 98 | + bool heartbeatStarted_; |
| 99 | + |
| 100 | + std::string licenseKey_; |
| 101 | + std::string sessionToken_; |
| 102 | + std::optional<long long> sessionExpiresIn_; |
| 103 | + std::string lastNonce_; |
| 104 | + std::string rawPayloadB64_; |
| 105 | + std::string signature_; |
| 106 | + std::string keyId_; |
| 107 | + std::vector<unsigned char> verifyPublicKeyBytes_; |
| 108 | + std::string sessionDataJson_; |
| 109 | + std::string appVariablesJson_; |
| 110 | + std::string licenseVariablesJson_; |
| 111 | + bool authenticated_ = false; |
| 112 | + bool heartbeatStop_ = false; |
| 113 | + std::string hwid_; |
| 114 | + std::unordered_set<std::string> knownServerErrors_ = { |
| 115 | + "invalid_app", |
| 116 | + "invalid_key", |
| 117 | + "expired", |
| 118 | + "revoked", |
| 119 | + "hwid_mismatch", |
| 120 | + "no_credits", |
| 121 | + "app_burn_cap_reached", |
| 122 | + "blocked", |
| 123 | + "rate_limited", |
| 124 | + "replay_detected", |
| 125 | + "app_disabled", |
| 126 | + "session_expired", |
| 127 | + "bad_request", |
| 128 | + "system_error", |
| 129 | + }; |
| 130 | +}; |
| 131 | + |
| 132 | +} // namespace authforge |
0 commit comments