Skip to content

Commit 94e9dfb

Browse files
mehtatejasJonasKs
authored andcommitted
configurable leeway
1 parent 3ff82f7 commit 94e9dfb

File tree

4 files changed

+54
-1
lines changed

4 files changed

+54
-1
lines changed

docs/docs/settings/b2c.mdx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ Scopes, these are the ones you've configured in Azure AD B2C. Key is scope, valu
2828
}
2929
```
3030

31+
-----------------
32+
33+
### leeway: int
34+
**Default:** `0`
35+
36+
By adding leeway, you define a tolerance window in terms of seconds, allowing the token to be
37+
considered valid even if it falls within the leeway time before or after the "exp" or "nbf" times.
38+
39+
-----------------
40+
3141
### validate_iss: bool
3242
**Default:** `True`
3343

docs/docs/settings/multi_tenant.mdx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ Scopes, these are the ones you've configured in Azure AD. Key is scope, value is
3030
}
3131
```
3232

33+
-----------------
34+
35+
### leeway: int
36+
**Default:** `0`
37+
38+
By adding leeway, you define a tolerance window in terms of seconds, allowing the token to be
39+
considered valid even if it falls within the leeway time before or after the "exp" or "nbf" times.
40+
41+
-----------------
42+
3343
### validate_iss: bool
3444
**Default:** `True`
3545

docs/docs/settings/single_tenant.mdx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ Scopes, these are the ones you've configured in Azure AD. Key is scope, value is
3939

4040
-----------------
4141

42+
### leeway: int
43+
**Default:** `0`
44+
45+
By adding leeway, you define a tolerance window in terms of seconds, allowing the token to be
46+
considered valid even if it falls within the leeway time before or after the "exp" or "nbf" times.
47+
48+
-----------------
49+
4250
### token_version: `Literal[1, 2]`
4351
**Default:** `2`
4452

fastapi_azure_auth/auth.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ def __init__(
2626
tenant_id: Optional[str] = None,
2727
scopes: Optional[Dict[str, str]] = None,
2828
multi_tenant: bool = False,
29+
leeway: int = 0,
2930
validate_iss: bool = True,
3031
iss_callable: Optional[Callable[[str], Awaitable[str]]] = None,
3132
token_version: Literal[1, 2] = 2,
@@ -54,6 +55,9 @@ def __init__(
5455
5556
:param multi_tenant: bool
5657
Whether this is a multi tenant or single tenant application.
58+
:param leeway: int
59+
By adding leeway, you define a tolerance window in terms of seconds, allowing the token to be
60+
considered valid even if it falls within the leeway time before or after the "exp" or "nbf" times.
5761
:param validate_iss: bool
5862
**Only used for multi-tenant applications**
5963
Whether to validate the token `iss` (issuer) or not. This can be skipped to allow anyone to log in.
@@ -101,6 +105,7 @@ def __init__(
101105
config_url=openid_config_url or None,
102106
)
103107

108+
self.leeway: int = leeway
104109
self.validate_iss: bool = validate_iss
105110
self.iss_callable: Optional[Callable[..., Any]] = iss_callable
106111
self.token_version: int = token_version
@@ -193,7 +198,7 @@ async def __call__(self, request: Request, security_scopes: SecurityScopes) -> O
193198
'require_sub': True,
194199
'require_jti': False,
195200
'require_at_hash': False,
196-
'leeway': 0,
201+
'leeway': self.leeway,
197202
}
198203
# Validate token
199204
token = jwt.decode(
@@ -238,6 +243,7 @@ def __init__(
238243
tenant_id: str,
239244
auto_error: bool = True,
240245
scopes: Optional[Dict[str, str]] = None,
246+
leeway: int = 0,
241247
allow_guest_users: bool = False,
242248
token_version: Literal[1, 2] = 2,
243249
openid_config_use_app_id: bool = False,
@@ -260,6 +266,11 @@ def __init__(
260266
{
261267
f'api://{settings.APP_CLIENT_ID}/user_impersonation': 'user impersonation'
262268
}
269+
270+
:param leeway: int
271+
By adding leeway, you define a tolerance window in terms of seconds, allowing the token to be
272+
considered valid even if it falls within the leeway time before or after the "exp" or "nbf" times.
273+
263274
:param allow_guest_users: bool
264275
Whether to allow guest users or not. Guest users can be added manually, or by other services, such as
265276
inviting them to a teams channel. Most developers do _not_ want guest users in their applications.
@@ -282,6 +293,7 @@ def __init__(
282293
auto_error=auto_error,
283294
tenant_id=tenant_id,
284295
scopes=scopes,
296+
leeway=leeway,
285297
allow_guest_users=allow_guest_users,
286298
token_version=token_version,
287299
openid_config_use_app_id=openid_config_use_app_id,
@@ -298,6 +310,7 @@ def __init__(
298310
app_client_id: str,
299311
auto_error: bool = True,
300312
scopes: Optional[Dict[str, str]] = None,
313+
leeway: int = 0,
301314
validate_iss: bool = True,
302315
iss_callable: Optional[Callable[[str], Awaitable[str]]] = None,
303316
allow_guest_users: bool = False,
@@ -320,6 +333,10 @@ def __init__(
320333
f'api://{settings.APP_CLIENT_ID}/user_impersonation': 'user impersonation'
321334
}
322335
336+
:param leeway: int
337+
By adding leeway, you define a tolerance window in terms of seconds, allowing the token to be
338+
considered valid even if it falls within the leeway time before or after the "exp" or "nbf" times.
339+
323340
:param validate_iss: bool
324341
Whether to validate the token `iss` (issuer) or not. This can be skipped to allow anyone to log in.
325342
:param iss_callable: Async Callable
@@ -346,6 +363,7 @@ def __init__(
346363
app_client_id=app_client_id,
347364
auto_error=auto_error,
348365
scopes=scopes,
366+
leeway=leeway,
349367
validate_iss=validate_iss,
350368
iss_callable=iss_callable,
351369
allow_guest_users=allow_guest_users,
@@ -364,6 +382,7 @@ def __init__(
364382
app_client_id: str,
365383
auto_error: bool = True,
366384
scopes: Optional[Dict[str, str]] = None,
385+
leeway: int = 0,
367386
validate_iss: bool = True,
368387
iss_callable: Optional[Callable[[str], Awaitable[str]]] = None,
369388
openid_config_use_app_id: bool = False,
@@ -386,6 +405,11 @@ def __init__(
386405
{
387406
f'api://{settings.APP_CLIENT_ID}/user_impersonation': 'user impersonation'
388407
}
408+
409+
:param leeway: int
410+
By adding leeway, you define a tolerance window in terms of seconds, allowing the token to be
411+
considered valid even if it falls within the leeway time before or after the "exp" or "nbf" times.
412+
389413
:param validate_iss: bool
390414
Whether to validate the token `iss` (issuer) or not. This can be skipped to allow anyone to log in.
391415
:param iss_callable: Async Callable
@@ -406,6 +430,7 @@ def __init__(
406430
app_client_id=app_client_id,
407431
auto_error=auto_error,
408432
scopes=scopes,
433+
leeway=leeway,
409434
validate_iss=validate_iss,
410435
iss_callable=iss_callable,
411436
multi_tenant=True,

0 commit comments

Comments
 (0)