-
Notifications
You must be signed in to change notification settings - Fork 99
feat(rest): Parse JWT exp claim from token in AuthProperties #600
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,7 +21,11 @@ | |
|
|
||
| #include <utility> | ||
|
|
||
| #include <nlohmann/json.hpp> | ||
|
|
||
| #include "iceberg/catalog/rest/catalog_properties.h" | ||
| #include "iceberg/util/macros.h" | ||
| #include "iceberg/util/transform_util.h" | ||
|
|
||
| namespace iceberg::rest::auth { | ||
|
|
||
|
|
@@ -75,7 +79,25 @@ Result<AuthProperties> AuthProperties::FromProperties( | |
| } | ||
| } | ||
|
|
||
| // TODO(lishuxu): Parse JWT exp claim from token to set expires_at_millis_. | ||
| // Parse JWT exp claim from token to set expires_at_millis_. | ||
| if (auto token = config.token(); !token.empty()) { | ||
| auto first_dot = token.find('.'); | ||
| auto last_dot = token.find('.', first_dot + 1); | ||
| if (first_dot != std::string::npos && last_dot != std::string::npos) { | ||
| auto payload_encoded = token.substr(first_dot + 1, last_dot - first_dot - 1); | ||
| auto payload_decoded = TransformUtil::Base64UrlDecode(payload_encoded); | ||
| if (payload_decoded.has_value()) { | ||
| try { | ||
| auto payload_json = nlohmann::json::parse(payload_decoded.value()); | ||
| if (payload_json.contains("exp") && payload_json["exp"].is_number()) { | ||
| config.expires_at_millis_ = payload_json["exp"].get<int64_t>() * 1000; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
| } catch (const nlohmann::json::parse_error& e) { | ||
| // Ignore parse errors from invalid JWT payloads. | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return config; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -283,4 +283,40 @@ std::string TransformUtil::Base64Encode(std::string_view str_to_encode) { | |
| return encoded; | ||
| } | ||
|
|
||
| Result<std::string> TransformUtil::Base64UrlDecode(std::string_view str_to_decode) { | ||
| std::string decoded; | ||
| decoded.reserve(str_to_decode.size() * 3 / 4); | ||
|
|
||
| uint32_t val = 0; | ||
| int32_t bits = 0; | ||
| for (char c : str_to_decode) { | ||
| if (c == '=') break; | ||
| int8_t v = -1; | ||
| if (c >= 'A' && c <= 'Z') | ||
| v = static_cast<int8_t>(c - 'A'); | ||
| else if (c >= 'a' && c <= 'z') | ||
| v = static_cast<int8_t>(c - 'a' + 26); | ||
| else if (c >= '0' && c <= '9') | ||
| v = static_cast<int8_t>(c - '0' + 52); | ||
| else if (c == '-' || c == '+') | ||
| v = 62; | ||
| else if (c == '_' || c == '/') | ||
| v = 63; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Base64Url spec only uses Suggested fix: |
||
|
|
||
| if (v == -1) { | ||
| return InvalidArgument("Invalid character in Base64Url string: '{}'", c); | ||
| } | ||
|
|
||
| val = (val << 6) | static_cast<uint32_t>(v); | ||
| bits += 6; | ||
|
|
||
| if (bits >= 8) { | ||
| bits -= 8; | ||
| decoded.push_back(static_cast<char>((val >> bits) & 0xFF)); | ||
| val &= (1U << bits) - 1; | ||
| } | ||
| } | ||
| return decoded; | ||
| } | ||
|
|
||
| } // namespace iceberg | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit:
last_dotis a bit misleading — it could implyrfindwas used.Since JWT has exactly three parts,
second_dotis more precise.