Skip to content

Commit 6f4b9fa

Browse files
Use system clock time as a fallback (#616)
* Use system clock time as a fallback in AuthenticationClient::SignInClient() request when GetTimeFromServer() call fails. Resolves: OLPEDGE-1499 Signed-off-by: Serhii Lozynskyi <[email protected]>
1 parent 12d415b commit 6f4b9fa

File tree

3 files changed

+31
-13
lines changed

3 files changed

+31
-13
lines changed

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -278,19 +278,16 @@ client::CancellationToken AuthenticationClient::Impl::SignInClient(
278278
client::CancellationContext context;
279279

280280
auto time_callback = [=](TimeResponse response) mutable {
281-
if (!response.IsSuccessful()) {
282-
callback(AuthenticationError(
283-
static_cast<int>(response.GetError().GetHttpStatusCode()),
284-
response.GetError().GetMessage()));
285-
return;
286-
}
281+
// As a fallback use local system time as a timestamp
282+
time_t timestamp =
283+
response.IsSuccessful() ? response.GetResult() : std::time(nullptr);
287284

288285
std::string url = server_url_;
289286
url.append(kOauthEndpoint);
290287
http::NetworkRequest request(url);
291288
request.WithVerb(http::NetworkRequest::HttpVerb::POST);
292289
request.WithHeader(http::kAuthorizationHeader,
293-
generateHeader(credentials, url, response.GetResult()));
290+
generateHeader(credentials, url, timestamp));
294291
request.WithHeader(http::kContentTypeHeader, kApplicationJson);
295292
request.WithHeader(http::kUserAgentHeader, http::kOlpSdkUserAgent);
296293
request.WithSettings(network_settings_);

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,12 @@ class AuthenticationClientTest : public ::testing::Test {
172172

173173
EXPECT_CALL(*network_, Send(testing::_, testing::_, testing::_, testing::_,
174174
testing::_))
175-
.Times(1)
176-
.WillOnce([&](olp::http::NetworkRequest request,
177-
olp::http::Network::Payload payload,
178-
olp::http::Network::Callback callback,
179-
olp::http::Network::HeaderCallback header_callback,
180-
olp::http::Network::DataCallback data_callback) {
175+
.Times(2) // First is GetTimeFromServer(). Second is actual SignIn.
176+
.WillRepeatedly([&](olp::http::NetworkRequest request,
177+
olp::http::Network::Payload payload,
178+
olp::http::Network::Callback callback,
179+
olp::http::Network::HeaderCallback header_callback,
180+
olp::http::Network::DataCallback data_callback) {
181181
olp::http::RequestId request_id(5);
182182
if (payload) {
183183
*payload << data;

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,28 @@ TEST_F(HereAccountOauth2Test, AutoRefreshingTokenCancelAsync) {
274274

275275
return olp::http::SendOutcome(request_id);
276276
});
277+
EXPECT_CALL(*network_, Send(IsGetRequest(kTimestampUrl), _, _, _, _))
278+
.Times(2)
279+
.WillRepeatedly([&](olp::http::NetworkRequest request,
280+
olp::http::Network::Payload payload,
281+
olp::http::Network::Callback callback,
282+
olp::http::Network::HeaderCallback header_callback,
283+
olp::http::Network::DataCallback data_callback) {
284+
olp::http::RequestId request_id(5);
285+
if (payload) {
286+
*payload << kResponseTime;
287+
}
288+
callback(olp::http::NetworkResponse()
289+
.WithRequestId(request_id)
290+
.WithStatus(olp::http::HttpStatusCode::OK));
291+
if (data_callback) {
292+
auto raw = const_cast<char*>(kResponseTime.c_str());
293+
data_callback(reinterpret_cast<uint8_t*>(raw), 0,
294+
kResponseTime.size());
295+
}
277296

297+
return olp::http::SendOutcome(request_id);
298+
});
278299
Settings settings({key_, secret_});
279300
settings.network_request_handler = network_;
280301
TokenEndpoint token_endpoint(settings);

0 commit comments

Comments
 (0)