forked from jpadilla/pyjwt
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial implementation of ttl jwk set cache
- Loading branch information
1 parent
a863a73
commit 479a7c1
Showing
3 changed files
with
73 additions
and
5 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,29 @@ | ||
from typing import Optional | ||
from datetime import datetime, timezone | ||
|
||
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: | ||
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 datetime.now(timezone.utc) > 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