forked from jpadilla/pyjwt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from wuhaoyujerry/jwk_set_cache
Add jwk set cache
- Loading branch information
Showing
4 changed files
with
218 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,30 @@ | ||
import time | ||
from typing import Optional | ||
|
||
from .api_jwk import PyJWKSet, PyJWTSetWithTimestamp | ||
|
||
|
||
class JWKSetCache: | ||
def __init__(self, lifespan: int): | ||
self.jwk_set_with_timestamp = 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.