-
-
Notifications
You must be signed in to change notification settings - Fork 694
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add cacheing functionality for JWK set (#781)
* Initial implementation of ttl jwk set cache (cherry picked from commit 479a7c1) * Add unit test for jwk set cache * Fix failed unit test * Disable cache signing key by default * Add a negative unit test for get_jwk_set * Add functionality to force refresh the jwk set cache when no matching signing key can be found from the cache * Add unit test for refresh cache * Add unit test to unset cache when the network call throws error * fix naming typo * Update unit test naming * Update comment * Add check for lifespan * Update comments for get_signing_key * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix ci error * Add type declaration to fix CI error * Add more unit tests to improve coverage * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Try to increase test coverage to 100% Co-authored-by: Jerry Wu <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
ae3da74
commit fc5b94e
Showing
4 changed files
with
263 additions
and
33 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import time | ||
from typing import Optional | ||
|
||
from .api_jwk import PyJWKSet, PyJWTSetWithTimestamp | ||
|
||
|
||
class JWKSetCache: | ||
def __init__(self, lifespan: int): | ||
self.jwk_set_with_timestamp: Optional[PyJWTSetWithTimestamp] = None | ||
self.lifespan = lifespan | ||
|
||
def put(self, jwk_set: PyJWKSet): | ||
if jwk_set is not None: | ||
self.jwk_set_with_timestamp = PyJWTSetWithTimestamp(jwk_set) | ||
else: | ||
# clear cache | ||
self.jwk_set_with_timestamp = None | ||
|
||
def get(self) -> Optional[PyJWKSet]: | ||
if self.jwk_set_with_timestamp is None or self.is_expired(): | ||
return None | ||
|
||
return self.jwk_set_with_timestamp.get_jwk_set() | ||
|
||
def is_expired(self) -> bool: | ||
|
||
return ( | ||
self.jwk_set_with_timestamp is not None | ||
and self.lifespan > -1 | ||
and time.monotonic() | ||
> self.jwk_set_with_timestamp.get_timestamp() + self.lifespan | ||
) |
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
Oops, something went wrong.