Skip to content

feat(rest): Parse JWT exp claim from token in AuthProperties#600

Open
wgtmac wants to merge 3 commits intoapache:mainfrom
wgtmac:parse-jwt-exp-claim-409089801954026600
Open

feat(rest): Parse JWT exp claim from token in AuthProperties#600
wgtmac wants to merge 3 commits intoapache:mainfrom
wgtmac:parse-jwt-exp-claim-409089801954026600

Conversation

@wgtmac
Copy link
Member

@wgtmac wgtmac commented Mar 24, 2026

  • Implement Base64UrlDecode in TransformUtil.
  • Update AuthProperties::FromProperties to parse the JWT exp claim from the token and set expires_at_millis_.
  • Add expires_at_millis() getter to AuthProperties.
  • Add unit tests for Base64UrlDecode and JWT expiration parsing.

google-labs-jules bot and others added 3 commits March 24, 2026 02:38
- Implement Base64UrlDecode in TransformUtil.
- Update AuthProperties::FromProperties to parse the JWT exp claim from the token and set expires_at_millis_.
- Add expires_at_millis() getter to AuthProperties.
- Add unit tests for Base64UrlDecode and JWT expiration parsing.

Co-authored-by: wgtmac <4684607+wgtmac@users.noreply.github.com>
- Implement robust Base64UrlDecode in TransformUtil using unsigned integers to avoid undefined behavior.
- Update AuthProperties::FromProperties to parse the JWT exp claim from the token and set expires_at_millis_.
- Add expires_at_millis() getter to AuthProperties.
- Add unit tests for Base64UrlDecode and JWT expiration parsing, using standard matchers and explicit type casting for Windows/MSVC compatibility.

Co-authored-by: wgtmac <4684607+wgtmac@users.noreply.github.com>
- Implement robust Base64UrlDecode in TransformUtil using unsigned integers to avoid undefined behavior.
- Update AuthProperties::FromProperties to parse the JWT exp claim from the token and set expires_at_millis_.
- Add expires_at_millis() getter to AuthProperties.
- Add unit tests for Base64UrlDecode and JWT expiration parsing.
- Improve HasValue matcher to support non-matcher arguments and fix MSVC template issues by using SafeMatcherCast and DescribeMatcher.

Co-authored-by: wgtmac <4684607+wgtmac@users.noreply.github.com>
else if (c == '-' || c == '+')
v = 62;
else if (c == '_' || c == '/')
v = 63;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Base64UrlDecode accepts + and / which are standard Base64 characters, not Base64Url. This means inputs that should be rejected are silently accepted, which contradicts the function name and the error message.

Base64Url spec only uses - (62) and _ (63). + and / should fall through to the invalid character check.

Suggested fix:

  else if (c == '-')
    v = 62;
  else if (c == '_')
    v = 63;

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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is_number() returns true for both integers and floats, but get<int64_t>()
throws nlohmann::json::type_error when the value is a float (e.g. "exp": 1735689600.0).
This exception is not caught by the parse_error handler, so it would propagate
unexpectedly.

// 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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: last_dot is a bit misleading — it could imply rfind was used.
Since JWT has exactly three parts, second_dot is more precise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants