You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by pe...@apache.org on 2022/03/12 02:41:00 UTC
[pulsar] 07/14: [C++] Fix wrong unit of Access Token Response's `expires_in` field (#14554)
This is an automated email from the ASF dual-hosted git repository.
penghui pushed a commit to branch branch-2.10
in repository https://gitbox.apache.org/repos/asf/pulsar.git
commit d16f049c17485f2b8dfed95d8a48e69ab1a3a072
Author: Yunze Xu <xy...@163.com>
AuthorDate: Tue Mar 8 12:53:29 2022 +0800
[C++] Fix wrong unit of Access Token Response's `expires_in` field (#14554)
### Motivation
The `expires_in` field of Access Token Response is in seconds. See
https://datatracker.ietf.org/doc/html/rfc6749#section-4.2.2. However,
C++ client treats it as milliseconds currently. It will leads to an
earlier expiration of the token.
### Modifications
Record the time point via the `std::time_point` class, which supports
add operations with a `std::duration` object. Then converts the
`expires_in` field via `std::chrono::second` function and calculate the
expired time point.
It also removes the usage of Boost time functions and makes code more clear.
(cherry picked from commit 95c1581d494d59c4d93782eb18547cec5427b503)
---
pulsar-client-cpp/lib/auth/AuthOauth2.cc | 15 ++-------------
pulsar-client-cpp/lib/auth/AuthOauth2.h | 5 ++++-
2 files changed, 6 insertions(+), 14 deletions(-)
diff --git a/pulsar-client-cpp/lib/auth/AuthOauth2.cc b/pulsar-client-cpp/lib/auth/AuthOauth2.cc
index 0fc935a..334289d 100644
--- a/pulsar-client-cpp/lib/auth/AuthOauth2.cc
+++ b/pulsar-client-cpp/lib/auth/AuthOauth2.cc
@@ -23,7 +23,6 @@
#include <stdexcept>
#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/ptree.hpp>
-#include <boost/date_time/posix_time/posix_time.hpp>
#include <lib/LogUtils.h>
DECLARE_LOG_OBJECT()
@@ -86,22 +85,12 @@ CachedToken::~CachedToken() {}
// Oauth2CachedToken
-static int64_t currentTimeMillis() {
- using namespace boost::posix_time;
- using boost::posix_time::milliseconds;
- using boost::posix_time::seconds;
- static ptime time_t_epoch(boost::gregorian::date(1970, 1, 1));
-
- time_duration diff = microsec_clock::universal_time() - time_t_epoch;
- return diff.total_milliseconds();
-}
-
Oauth2CachedToken::Oauth2CachedToken(Oauth2TokenResultPtr token) {
latest_ = token;
int64_t expiredIn = token->getExpiresIn();
if (expiredIn > 0) {
- expiresAt_ = expiredIn + currentTimeMillis();
+ expiresAt_ = Clock::now() + std::chrono::seconds(expiredIn);
} else {
throw std::runtime_error("ExpiresIn in Oauth2TokenResult invalid value: " +
std::to_string(expiredIn));
@@ -113,7 +102,7 @@ AuthenticationDataPtr Oauth2CachedToken::getAuthData() { return authData_; }
Oauth2CachedToken::~Oauth2CachedToken() {}
-bool Oauth2CachedToken::isExpired() { return expiresAt_ < currentTimeMillis(); }
+bool Oauth2CachedToken::isExpired() { return expiresAt_ < Clock::now(); }
// OauthFlow
diff --git a/pulsar-client-cpp/lib/auth/AuthOauth2.h b/pulsar-client-cpp/lib/auth/AuthOauth2.h
index b3cc952..59e8ad9 100644
--- a/pulsar-client-cpp/lib/auth/AuthOauth2.h
+++ b/pulsar-client-cpp/lib/auth/AuthOauth2.h
@@ -20,6 +20,7 @@
#pragma once
#include <pulsar/Authentication.h>
+#include <chrono>
#include <mutex>
#include <string>
@@ -69,13 +70,15 @@ class ClientCredentialFlow : public Oauth2Flow {
class Oauth2CachedToken : public CachedToken {
public:
+ using Clock = std::chrono::high_resolution_clock;
+
Oauth2CachedToken(Oauth2TokenResultPtr token);
~Oauth2CachedToken();
bool isExpired();
AuthenticationDataPtr getAuthData();
private:
- int64_t expiresAt_;
+ std::chrono::time_point<Clock> expiresAt_;
Oauth2TokenResultPtr latest_;
AuthenticationDataPtr authData_;
};