Skip to content
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

Use same leeway for exp and nbf when parsing JWT #1312

Merged
merged 3 commits into from
Apr 16, 2024

Conversation

rachel-carvalho
Copy link
Contributor

@rachel-carvalho rachel-carvalho commented Apr 16, 2024

Description

Right now we specify a leeway of 10 seconds when validating JWT payload expiration, but no leeway for nbf (not valid before). The shopify-api JS library does allow for clockTolerance in both fields, and I think it makes sense for the Ruby gem to do the same.

How has this been tested?

I added unit tests.

Checklist:

  • My commit message follow the pattern described in here
  • I have performed a self-review of my own code.
  • I have added tests that prove my fix is effective or that my feature works.
  • I have updated the project documentation.
  • I have added a changelog line.

@@ -73,8 +73,7 @@ def ==(other)

sig { params(token: String, api_secret_key: String).returns(T::Hash[String, T.untyped]) }
def decode_token(token, api_secret_key)
JWT.decode(token, api_secret_key, true,
{ exp_leeway: JWT_EXPIRATION_LEEWAY, algorithm: "HS256" })[0]
JWT.decode(token, api_secret_key, true, leeway: JWT_EXPIRATION_LEEWAY, algorithm: "HS256")[0]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I would prefer to rename the constant to JWT_LEEWAY since it doesn't only apply to expiration anymore, but it's a public constant and might make this a breaking change, so I decided to keep it.

Copy link
Contributor

Choose a reason for hiding this comment

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

We can always alias it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh yeah good call, I'll do that then 👍

@rachel-carvalho rachel-carvalho marked this pull request as ready for review April 16, 2024 16:42
@rachel-carvalho rachel-carvalho requested a review from a team as a code owner April 16, 2024 16:42
Copy link
Contributor

@zzooeeyy zzooeeyy left a comment

Choose a reason for hiding this comment

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

LGTM!

@@ -73,8 +73,7 @@ def ==(other)

sig { params(token: String, api_secret_key: String).returns(T::Hash[String, T.untyped]) }
def decode_token(token, api_secret_key)
JWT.decode(token, api_secret_key, true,
{ exp_leeway: JWT_EXPIRATION_LEEWAY, algorithm: "HS256" })[0]
JWT.decode(token, api_secret_key, true, leeway: JWT_EXPIRATION_LEEWAY, algorithm: "HS256")[0]
Copy link
Contributor

Choose a reason for hiding this comment

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

We can always alias it

@rachel-carvalho rachel-carvalho merged commit c1163e0 into main Apr 16, 2024
11 checks passed
@rachel-carvalho rachel-carvalho deleted the add_leeway_for_jwt_nbf branch April 16, 2024 18:40
@rachel-carvalho rachel-carvalho self-assigned this Apr 16, 2024
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.

3 participants