Skip to content

Commit ebb1f72

Browse files
Add use_local_time to AuthenticationSettings (#765)
Add use_local_time parameter to AuthenticationSettings to control if AuthenticationClient will try to get time from server or use local machine time instead. Resolves: OLPEDGE-1762 Signed-off-by: Serhii Lozynskyi <[email protected]>
1 parent a7c221a commit ebb1f72

File tree

3 files changed

+79
-3
lines changed

3 files changed

+79
-3
lines changed

olp-cpp-sdk-authentication/include/olp/authentication/AuthenticationSettings.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ struct AUTHENTICATION_API AuthenticationSettings {
7777
* in-memory cache.
7878
*/
7979
size_t token_cache_limit{100u};
80+
81+
/**
82+
* @brief Uses system system time in authentication requests rather than
83+
* requesting time from authentication server.
84+
*/
85+
bool use_system_time{false};
8086
};
8187

8288
} // namespace authentication

olp-cpp-sdk-authentication/src/AuthenticationClient.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ class AuthenticationClient::Impl final {
329329
using TimeResponse = client::ApiResponse<time_t, client::ApiError>;
330330
using TimeCallback = std::function<void(TimeResponse)>;
331331

332+
client::CancellationToken GetTime(TimeCallback callback);
332333
client::CancellationToken GetTimeFromServer(TimeCallback callback);
333334
static TimeResponse ParseTimeResponse(std::stringstream& payload);
334335

@@ -530,7 +531,7 @@ client::CancellationToken AuthenticationClient::Impl::SignInClient(
530531
};
531532

532533
context.ExecuteOrCancelled(
533-
[&]() { return GetTimeFromServer(std::move(time_callback)); }, []() {});
534+
[&]() { return GetTime(std::move(time_callback)); });
534535
return client::CancellationToken(
535536
[context]() mutable { context.CancelOperation(); });
536537
}
@@ -556,6 +557,15 @@ AuthenticationClient::Impl::ParseTimeResponse(std::stringstream& payload) {
556557
return timestamp_it->value.GetUint();
557558
}
558559

560+
client::CancellationToken AuthenticationClient::Impl::GetTime(TimeCallback callback) {
561+
if (settings_.use_system_time) {
562+
callback(
563+
std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()));
564+
return client::CancellationToken();
565+
}
566+
return GetTimeFromServer(std::move(callback));
567+
}
568+
559569
client::CancellationToken AuthenticationClient::Impl::GetTimeFromServer(
560570
TimeCallback callback) {
561571
std::weak_ptr<http::Network> weak_network(settings_.network_request_handler);

tests/integration/olp-cpp-sdk-authentication/AuthenticationClientTest.cpp

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ const std::string kTimestampUrl =
4242
const std::string kIntrospectUrl =
4343
R"(https://authentication.server.url/app/me)";
4444

45+
constexpr auto kTokenEndpointUrl = "https://authentication.server.url";
46+
4547
constexpr unsigned int kExpirtyTime = 3600;
4648
constexpr unsigned int kMaxExpiryTime = kExpirtyTime + 30;
4749
constexpr unsigned int kMinExpiryTime = kExpirtyTime - 10;
@@ -116,8 +118,7 @@ class AuthenticationClientTest : public ::testing::Test {
116118
AuthenticationClientTest()
117119
: key_("key"), secret_("secret"), scope_("scope") {}
118120
void SetUp() {
119-
client_ = std::make_unique<AuthenticationClient>(
120-
"https://authentication.server.url");
121+
client_ = std::make_unique<AuthenticationClient>(kTokenEndpointUrl);
121122

122123
network_ = std::make_shared<NetworkMock>();
123124
task_scheduler_ =
@@ -222,6 +223,65 @@ class AuthenticationClientTest : public ::testing::Test {
222223
const std::string scope_;
223224
};
224225

226+
TEST_F(AuthenticationClientTest, SignInClientUseLocalTime) {
227+
AuthenticationSettings settings;
228+
settings.network_request_handler = network_;
229+
settings.use_system_time = true;
230+
settings.token_endpoint_url = kTokenEndpointUrl;
231+
232+
auto client = std::make_unique<AuthenticationClient>(std::move(settings));
233+
234+
AuthenticationCredentials credentials(key_, secret_);
235+
std::promise<AuthenticationClient::SignInClientResponse> request;
236+
auto request_future = request.get_future();
237+
238+
EXPECT_CALL(*network_, Send(IsGetRequest(kTimestampUrl), _, _, _, _))
239+
.Times(0);
240+
241+
EXPECT_CALL(*network_, Send(_, _, _, _, _))
242+
.WillOnce([&](olp::http::NetworkRequest /*request*/,
243+
olp::http::Network::Payload payload,
244+
olp::http::Network::Callback callback,
245+
olp::http::Network::HeaderCallback /*header_callback*/,
246+
olp::http::Network::DataCallback data_callback) {
247+
olp::http::RequestId request_id(5);
248+
if (payload) {
249+
*payload << kResponseWithScope;
250+
}
251+
callback(olp::http::NetworkResponse()
252+
.WithRequestId(request_id)
253+
.WithStatus(olp::http::HttpStatusCode::OK));
254+
if (data_callback) {
255+
auto raw = const_cast<char*>(kResponseWithScope.c_str());
256+
data_callback(reinterpret_cast<uint8_t*>(raw), 0,
257+
kResponseWithScope.size());
258+
}
259+
260+
return olp::http::SendOutcome(request_id);
261+
});
262+
263+
AuthenticationClient::SignInProperties properties;
264+
properties.scope = scope_;
265+
std::time_t now = std::time(nullptr);
266+
client->SignInClient(
267+
credentials, properties,
268+
[&](const AuthenticationClient::SignInClientResponse& response) {
269+
request.set_value(response);
270+
});
271+
request_future.wait();
272+
273+
AuthenticationClient::SignInClientResponse response = request_future.get();
274+
EXPECT_TRUE(response.IsSuccessful());
275+
EXPECT_FALSE(response.GetResult().GetAccessToken().empty());
276+
EXPECT_EQ(kResponseToken, response.GetResult().GetAccessToken());
277+
EXPECT_GE(now + kMaxExpiryTime, response.GetResult().GetExpiryTime());
278+
EXPECT_LT(now + kMinExpiryTime, response.GetResult().GetExpiryTime());
279+
EXPECT_EQ("bearer", response.GetResult().GetTokenType());
280+
EXPECT_TRUE(response.GetResult().GetRefreshToken().empty());
281+
EXPECT_TRUE(response.GetResult().GetUserIdentifier().empty());
282+
EXPECT_EQ(response.GetResult().GetScope(), scope_);
283+
}
284+
225285
TEST_F(AuthenticationClientTest, SignInClientScope) {
226286
AuthenticationCredentials credentials(key_, secret_);
227287
std::promise<AuthenticationClient::SignInClientResponse> request;

0 commit comments

Comments
 (0)