@@ -415,21 +415,9 @@ def _decode_tokens(result: dict) -> tuple[dict, dict]:
415415 return access_payload , refresh_payload
416416
417417 @staticmethod
418- def _assert_lifetimes_unchanged (
419- access_payload : dict , refresh_payload : dict , before : datetime .datetime , after : datetime .datetime
420- ):
421- access_delta = datetime .timedelta (minutes = settings .authentication .access_token .expiration )
422- refresh_delta = datetime .timedelta (minutes = settings .authentication .refresh_token .expiration )
423- assert (
424- int ((before + access_delta ).timestamp ())
425- <= access_payload ["exp" ]
426- <= int ((after + access_delta ).timestamp ()) + 1
427- )
428- assert (
429- int ((before + refresh_delta ).timestamp ())
430- <= refresh_payload ["exp" ]
431- <= int ((after + refresh_delta ).timestamp ()) + 1
432- )
418+ def _assert_expires_in (exp : int , minutes : int , before : datetime .datetime , after : datetime .datetime ):
419+ delta = datetime .timedelta (minutes = minutes )
420+ assert int ((before + delta ).timestamp ()) <= exp <= int ((after + delta ).timestamp ()) + 1
433421
434422 @pytest .mark .parametrize ("content_source" , ("web" , "admin" , "mobile" ))
435423 async def test_login_embeds_client_claim (self , client : TestClient , user : User , content_source : str ):
@@ -444,7 +432,16 @@ async def test_login_embeds_client_claim(self, client: TestClient, user: User, c
444432 access_payload , refresh_payload = self ._decode_tokens (resp .json ()["result" ])
445433 assert access_payload ["client" ] == content_source
446434 assert refresh_payload ["client" ] == content_source
447- self ._assert_lifetimes_unchanged (access_payload , refresh_payload , before , after )
435+ # web/admin get the short lifetimes; mobile keeps the defaults.
436+ if content_source in ("web" , "admin" ):
437+ expected_access = settings .authentication .access_token .web_admin_expiration
438+ expected_refresh = settings .authentication .refresh_token .web_admin_expiration
439+ assert expected_access is not None and expected_refresh is not None
440+ else :
441+ expected_access = settings .authentication .access_token .expiration
442+ expected_refresh = settings .authentication .refresh_token .expiration
443+ self ._assert_expires_in (access_payload ["exp" ], expected_access , before , after )
444+ self ._assert_expires_in (refresh_payload ["exp" ], expected_refresh , before , after )
448445
449446 async def test_login_audit_event_records_client_source (self , client : TestClient , user : User , mocker : MockerFixture ):
450447 audit_log = mocker .patch ("apps.authentication.api.auth.log" )
@@ -504,7 +501,9 @@ async def test_login_without_client_claim(self, client: TestClient, user: User,
504501 access_payload , refresh_payload = self ._decode_tokens (resp .json ()["result" ])
505502 assert "client" not in access_payload
506503 assert "client" not in refresh_payload
507- self ._assert_lifetimes_unchanged (access_payload , refresh_payload , before , after )
504+ # No client claim -> default (mobile) lifetimes.
505+ self ._assert_expires_in (access_payload ["exp" ], settings .authentication .access_token .expiration , before , after )
506+ self ._assert_expires_in (refresh_payload ["exp" ], settings .authentication .refresh_token .expiration , before , after )
508507
509508
510509class TestShortLivedWebAdminTokens (BaseTest ):
0 commit comments