Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[C++] Fix wrong unit of Access Token Response's expires_in field #14554

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 2 additions & 13 deletions pulsar-client-cpp/lib/auth/AuthOauth2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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));
Expand All @@ -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

Expand Down
5 changes: 4 additions & 1 deletion pulsar-client-cpp/lib/auth/AuthOauth2.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#pragma once

#include <pulsar/Authentication.h>
#include <chrono>
#include <mutex>
#include <string>

Expand Down Expand Up @@ -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_;
};
Expand Down