Skip to content

IOTSDK-24021: Fix parsing of server DateTime #1499

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

Merged
merged 1 commit into from
Apr 23, 2024
Merged
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
33 changes: 29 additions & 4 deletions olp-cpp-sdk-authentication/src/AuthenticationClientUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "ResponseFromJsonBuilder.h"
#include "olp/core/http/NetworkResponse.h"
#include "olp/core/http/NetworkUtils.h"
#include "olp/core/logging/Log.h"
#include "olp/core/utils/Base64.h"
#include "olp/core/utils/Url.h"

Expand All @@ -55,6 +56,7 @@ constexpr auto kOauthTimestamp = "oauth_timestamp";
constexpr auto kOauthSignatureMethod = "oauth_signature_method";
constexpr auto kVersion = "1.0";
constexpr auto kHmac = "HMAC-SHA256";
constexpr auto kLogTag = "AuthenticationClientUtils";

std::string Base64Encode(const Crypto::Sha256Digest& digest) {
std::string ret = olp::utils::Base64Encode(digest.data(), digest.size());
Expand Down Expand Up @@ -95,16 +97,39 @@ namespace client = olp::client;
constexpr auto kDate = "date";

#ifdef _WIN32
#define timegm _mkgmtime
#endif

std::time_t ParseTime(const std::string& value) {
// Windows does not have ::strptime and ::timegm
std::time_t DoParseTime(const std::string& value) {
std::tm tm = {};
std::istringstream ss(value);
ss >> std::get_time(&tm, "%a, %d %b %Y %H:%M:%S %z");
return _mkgmtime(&tm);
}

#else

std::time_t DoParseTime(const std::string& value) {
std::tm tm = {};
const auto format = "%a, %d %b %Y %H:%M:%S %Z";
const auto parsed_until = ::strptime(value.c_str(), format, &tm);
if (parsed_until != value.c_str() + value.size()) {
OLP_SDK_LOG_WARNING(kLogTag, "Timestamp is not fully parsed" << value);
}
return timegm(&tm);
}

#endif

std::time_t GmtEpochOffset() {
const auto epoch_as_date_time = "Thu, 1 Jan 1970 0:00:00 GMT";
return DoParseTime(epoch_as_date_time);
}

std::time_t ParseTime(const std::string& value) {
const auto time = DoParseTime(value);
const auto offset = GmtEpochOffset();
return time - offset;
}

boost::optional<std::time_t> GetTimestampFromHeaders(
const olp::http::Headers& headers) {
auto it =
Expand Down
Loading