Skip to content

Commit 3447044

Browse files
committed
fix: generate now timestamp
#68
1 parent a9b67da commit 3447044

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

src/joserfc/_rfc7519/claims.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import datetime
55
import calendar
66
from json import JSONEncoder
7-
from typing import TypedDict, Type, Any
7+
from typing import TypedDict, Type, Any, Callable
88
from ..util import to_bytes
99
from ..errors import (
1010
MissingClaimError,
@@ -83,13 +83,26 @@ def validate(self, claims: dict[str, Any]) -> None:
8383

8484

8585
class JWTClaimsRegistry(ClaimsRegistry):
86-
def __init__(self, now: int | None = None, leeway: int = 0, **kwargs: ClaimsOption):
86+
"""A claims registry for validating JWT claims.
87+
88+
:param now: timestamp of "now" time
89+
:param leeway: leeway time in seconds
90+
:param **kwargs: claims options
91+
"""
92+
93+
def __init__(self, now: int | Callable[[], int] | None = None, leeway: int = 0, **kwargs: ClaimsOption) -> None:
8794
if now is None:
88-
now = int(time.time())
89-
self.now = now
95+
now = _generate_now
96+
self._now = now
9097
self.leeway = leeway
9198
super().__init__(**kwargs)
9299

100+
@property
101+
def now(self) -> int:
102+
if callable(self._now):
103+
return self._now()
104+
return self._now
105+
93106
def validate_exp(self, value: int) -> None:
94107
"""The "exp" (expiration time) claim identifies the expiration time on
95108
or after which the JWT MUST NOT be accepted for processing. The
@@ -135,3 +148,7 @@ def validate_iat(self, value: int) -> None:
135148

136149
def _validate_numeric_time(s: int) -> bool:
137150
return isinstance(s, (int, float))
151+
152+
153+
def _generate_now() -> int:
154+
return int(time.time())

0 commit comments

Comments
 (0)