Skip to content

Commit 6e6ddde

Browse files
committed
adding jwt_leeway param to allow configuring the leeway param on PyJWT jwt.decode method
1 parent 2c0ad47 commit 6e6ddde

File tree

5 files changed

+25
-0
lines changed

5 files changed

+25
-0
lines changed

workos/_base_client.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class BaseClient(ClientConfiguration):
2525
_base_url: str
2626
_client_id: str
2727
_request_timeout: int
28+
_jwt_leeway: float
2829

2930
def __init__(
3031
self,
@@ -33,6 +34,7 @@ def __init__(
3334
client_id: Optional[str],
3435
base_url: Optional[str] = None,
3536
request_timeout: Optional[int] = None,
37+
jwt_leeway: float = 0,
3638
) -> None:
3739
api_key = api_key or os.getenv("WORKOS_API_KEY")
3840
if api_key is None:
@@ -63,6 +65,8 @@ def __init__(
6365
if request_timeout
6466
else int(os.getenv("WORKOS_REQUEST_TIMEOUT", DEFAULT_REQUEST_TIMEOUT))
6567
)
68+
69+
self._jwt_leeway = jwt_leeway
6670

6771
@property
6872
@abstractmethod
@@ -122,3 +126,7 @@ def client_id(self) -> str:
122126
@property
123127
def request_timeout(self) -> int:
124128
return self._request_timeout
129+
130+
@property
131+
def jwt_leeway(self) -> float:
132+
return self._jwt_leeway

workos/async_client.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@ def __init__(
2828
client_id: Optional[str] = None,
2929
base_url: Optional[str] = None,
3030
request_timeout: Optional[int] = None,
31+
jwt_leeway: float = 0,
3132
):
3233
super().__init__(
3334
api_key=api_key,
3435
client_id=client_id,
3536
base_url=base_url,
3637
request_timeout=request_timeout,
38+
jwt_leeway=jwt_leeway,
3739
)
3840
self._http_client = AsyncHTTPClient(
3941
api_key=self._api_key,

workos/client.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@ def __init__(
2828
client_id: Optional[str] = None,
2929
base_url: Optional[str] = None,
3030
request_timeout: Optional[int] = None,
31+
jwt_leeway: float = 0,
3132
):
3233
super().__init__(
3334
api_key=api_key,
3435
client_id=client_id,
3536
base_url=base_url,
3637
request_timeout=request_timeout,
38+
jwt_leeway=jwt_leeway,
3739
)
3840
self._http_client = SyncHTTPClient(
3941
api_key=self._api_key,

workos/session.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class SessionModule(Protocol):
2828
cookie_password: str
2929
jwks: PyJWKClient
3030
jwk_algorithms: List[str]
31+
jwt_leeway: float
3132

3233
def __init__(
3334
self,
@@ -36,6 +37,7 @@ def __init__(
3637
client_id: str,
3738
session_data: str,
3839
cookie_password: str,
40+
jwt_leeway: float = 0,
3941
) -> None:
4042
# If the cookie password is not provided, throw an error
4143
if cookie_password is None or cookie_password == "":
@@ -45,6 +47,7 @@ def __init__(
4547
self.client_id = client_id
4648
self.session_data = session_data
4749
self.cookie_password = cookie_password
50+
self.jwt_leeway = jwt_leeway
4851

4952
self.jwks = PyJWKClient(self.user_management.get_jwks_url())
5053

@@ -89,6 +92,7 @@ def authenticate(
8992
signing_key.key,
9093
algorithms=self.jwk_algorithms,
9194
options={"verify_aud": False},
95+
leeway=self.jwt_leeway,
9296
)
9397

9498
return AuthenticateWithSessionCookieSuccessResponse(
@@ -136,6 +140,7 @@ def _is_valid_jwt(self, token: str) -> bool:
136140
signing_key.key,
137141
algorithms=self.jwk_algorithms,
138142
options={"verify_aud": False},
143+
leeway=self.jwt_leeway,
139144
)
140145
return True
141146
except jwt.exceptions.InvalidTokenError:
@@ -167,6 +172,7 @@ def __init__(
167172
client_id: str,
168173
session_data: str,
169174
cookie_password: str,
175+
jwt_leeway: float = 0,
170176
) -> None:
171177
# If the cookie password is not provided, throw an error
172178
if cookie_password is None or cookie_password == "":
@@ -176,6 +182,7 @@ def __init__(
176182
self.client_id = client_id
177183
self.session_data = session_data
178184
self.cookie_password = cookie_password
185+
self.jwt_leeway = jwt_leeway
179186

180187
self.jwks = PyJWKClient(self.user_management.get_jwks_url())
181188

@@ -228,6 +235,7 @@ def refresh(
228235
signing_key.key,
229236
algorithms=self.jwk_algorithms,
230237
options={"verify_aud": False},
238+
leeway=self.jwt_leeway,
231239
)
232240

233241
return RefreshWithSessionCookieSuccessResponse(
@@ -257,6 +265,7 @@ def __init__(
257265
client_id: str,
258266
session_data: str,
259267
cookie_password: str,
268+
jwt_leeway: float = 0,
260269
) -> None:
261270
# If the cookie password is not provided, throw an error
262271
if cookie_password is None or cookie_password == "":
@@ -266,6 +275,7 @@ def __init__(
266275
self.client_id = client_id
267276
self.session_data = session_data
268277
self.cookie_password = cookie_password
278+
self.jwt_leeway = jwt_leeway
269279

270280
self.jwks = PyJWKClient(self.user_management.get_jwks_url())
271281

@@ -318,6 +328,7 @@ async def refresh(
318328
signing_key.key,
319329
algorithms=self.jwk_algorithms,
320330
options={"verify_aud": False},
331+
leeway=self.jwt_leeway,
321332
)
322333

323334
return RefreshWithSessionCookieSuccessResponse(

workos/user_management.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,7 @@ def load_sealed_session(
866866
client_id=self._http_client.client_id,
867867
session_data=sealed_session,
868868
cookie_password=cookie_password,
869+
jwt_leeway=self._client_configuration.jwt_leeway,
869870
)
870871

871872
def get_user(self, user_id: str) -> User:
@@ -1491,6 +1492,7 @@ async def load_sealed_session(
14911492
client_id=self._http_client.client_id,
14921493
session_data=sealed_session,
14931494
cookie_password=cookie_password,
1495+
jwt_leeway=self._client_configuration.jwt_leeway,
14941496
)
14951497

14961498
async def get_user(self, user_id: str) -> User:

0 commit comments

Comments
 (0)