-
Notifications
You must be signed in to change notification settings - Fork 119
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
Timezone-aware datetime Object Support #325
Open
Flat-Irons
wants to merge
2
commits into
latchset:main
Choose a base branch
from
Flat-Irons:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
import copy | ||
import time | ||
import uuid | ||
from datetime import datetime | ||
|
||
from deprecated import deprecated | ||
|
||
|
@@ -172,7 +173,9 @@ def __init__(self, header=None, claims=None, jwt=None, key=None, | |
"""Creates a JWT object. | ||
|
||
:param header: A dict or a JSON string with the JWT Header data. | ||
:param claims: A dict or a string with the JWT Claims data. | ||
:param claims: A dict or a string with the JWT Claims data. If | ||
the 'exp' or 'nbf' are datetime objects, they are automatically | ||
converted to integer unix timestamps. Otherwise, they are left as is. | ||
:param jwt: a 'raw' JWT token | ||
:param key: A (:class:`jwcrypto.jwk.JWK`) key to deserialize | ||
the token. A (:class:`jwcrypto.jwk.JWKSet`) can also be used. | ||
|
@@ -225,6 +228,14 @@ def __init__(self, header=None, claims=None, jwt=None, key=None, | |
self._check_claims = check_claims | ||
|
||
if claims is not None: | ||
# Check for datetime objects | ||
if 'exp' in claims and isinstance(claims['exp'], datetime): | ||
# Check if timezone aware | ||
claims['exp'] = self._check_and_convert_dt(claims['exp'], 'exp') | ||
if 'nbf' in claims and isinstance(claims['nbf'], datetime): | ||
# Check if timezone aware | ||
claims['nbf'] = self._check_and_convert_dt(claims['nbf'], 'nbf') | ||
|
||
self.claims = claims | ||
|
||
if jwt is not None: | ||
|
@@ -388,6 +399,18 @@ def expected_type(self, v): | |
else: | ||
raise ValueError("Invalid value, must be 'JWS' or 'JWE'") | ||
|
||
def _check_and_convert_dt(self, dt, claim_prop): | ||
if ( | ||
dt.tzinfo is not None | ||
and dt.tzinfo.utcoffset(dt) is not None | ||
): | ||
dt_timestamp = int(dt.timestamp()) | ||
return dt_timestamp | ||
else: | ||
raise ValueError( | ||
f"'{claim_prop}' datetime object must be timezone aware" | ||
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. Why does it need to be timezone-aware? Can't we just assume UTC if no timezone is set? |
||
) | ||
|
||
def _add_optional_claim(self, name, claims): | ||
if name in claims: | ||
return | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I do not think we should modify the array being passed in.
We should instead make a copy in self.claims and modify them there