-
-
Notifications
You must be signed in to change notification settings - Fork 720
Fixes #921: Add now parameter to decode
#1119
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: master
Are you sure you want to change the base?
Conversation
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.
Pull Request Overview
This PR adds a now parameter to the JWT decode methods to allow manual specification of the current time for token validation. This enables testing time-dependent token validation (expiration, not-before, issued-at) without relying on system time.
- Adds
nowparameter acceptingfloat,datetime, orNonetodecode(),decode_complete(), and_validate_claims()methods - Updates validation logic to use the provided
nowvalue instead of always using current system time - Adds tests to verify the
nowparameter works correctly with expiration validation using both numeric timestamps and datetime objects
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| jwt/api_jwt.py | Adds now parameter to decode methods and validation logic, with type checking and conversion for float/datetime inputs |
| tests/test_api_jwt.py | Adds two test cases verifying the now parameter behavior with both numeric timestamps and datetime objects |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
jwt/api_jwt.py
Outdated
| elif isinstance(now, datetime): | ||
| now = now.timestamp() | ||
| elif not isinstance(now, (int, float)): | ||
| raise TypeError("now must be a float, datetime or None") |
Copilot
AI
Nov 2, 2025
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.
The type check only validates against int and float, but line 393 already handles the datetime case. This condition will always be reached after handling None and datetime, so it could be simplified to just check not isinstance(now, (int, float)) or provide a more specific error message that clarifies the type has already been partially validated.
| raise TypeError("now must be a float, datetime or None") | |
| raise TypeError("now must be a number (int or float)") |
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.
The TypeError message reflects what the original value coming into the method is allowed to be, not what the value may be by the time we've reached that point in the code. I.e. the initial now value as passed in must be a float, datetime or None. (Or if you prefer, a number, datetime or None.)
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Add a
nowparameter to the decode functions so that one may manually specify what the current time is.Also add some tests, though only for
expon the assumption that the same code paths are used fornbfandiatas the non-nowtests.