diff options
| author | Dan Engelbrecht <[email protected]> | 2026-03-18 22:28:14 +0100 |
|---|---|---|
| committer | GitHub Enterprise <[email protected]> | 2026-03-18 22:28:14 +0100 |
| commit | 59bc08385515997a34fe2b4b3cbbfd03dd9a7c5b (patch) | |
| tree | 0a65fca5537909f41b5f8b0d87daa7dbcd967677 /src/zenhttp/include | |
| parent | Update libcurl to 8.19.0 (#862) (diff) | |
| download | archived-zen-59bc08385515997a34fe2b4b3cbbfd03dd9a7c5b.tar.xz archived-zen-59bc08385515997a34fe2b4b3cbbfd03dd9a7c5b.zip | |
improve auth token refresh (#863)
Authentication callbacks are not thread safe, ensured call sites does single threaded calls
Diffstat (limited to 'src/zenhttp/include')
| -rw-r--r-- | src/zenhttp/include/zenhttp/httpclient.h | 26 | ||||
| -rw-r--r-- | src/zenhttp/include/zenhttp/httpclientauth.h | 4 |
2 files changed, 25 insertions, 5 deletions
diff --git a/src/zenhttp/include/zenhttp/httpclient.h b/src/zenhttp/include/zenhttp/httpclient.h index e878c900f..9531b9366 100644 --- a/src/zenhttp/include/zenhttp/httpclient.h +++ b/src/zenhttp/include/zenhttp/httpclient.h @@ -68,14 +68,30 @@ struct HttpClientAccessToken static constexpr int64_t ExpireMarginInSeconds = 60 * 5; - std::string Value; - TimePoint ExpireTime; + HttpClientAccessToken() {} - bool IsValid() const + HttpClientAccessToken(std::string_view InValue, const TimePoint& InExpireTime) : Value(InValue), ExpireTime(InExpireTime) {} + + std::optional<std::string> GetValue() const { - return Value.empty() == false && - ExpireMarginInSeconds < std::chrono::duration_cast<std::chrono::seconds>(ExpireTime - Clock::now()).count(); + if (IsValid()) + { + return Value; + } + return {}; } + + bool NeedsRefresh() const + { + return Value.empty() == true || (Clock::now() + std::chrono::seconds(ExpireMarginInSeconds)) >= ExpireTime; + } + + bool HasExpired() const { return Clock::now() >= ExpireTime; } + bool IsValid() const { return !Value.empty() && !HasExpired(); } + +private: + std::string Value; + TimePoint ExpireTime; }; struct HttpClientSettings diff --git a/src/zenhttp/include/zenhttp/httpclientauth.h b/src/zenhttp/include/zenhttp/httpclientauth.h index 26f31ed2a..f1bccdca6 100644 --- a/src/zenhttp/include/zenhttp/httpclientauth.h +++ b/src/zenhttp/include/zenhttp/httpclientauth.h @@ -10,6 +10,10 @@ namespace zen { class AuthMgr; namespace httpclientauth { + + // The std::function<HttpClientAccessToken()> instances returned from these functions are not guarateed to + // be thread safe so caller must make sure they are not called from multiple threads in parallell + std::function<HttpClientAccessToken()> CreateFromStaticToken(HttpClientAccessToken Token); std::function<HttpClientAccessToken()> CreateFromStaticToken(std::string_view Token); |