@@ -594,6 +594,40 @@ async def test_refresh_token_expired(
594594 assert data ["detail" ] == "Invalid JWT: expired_token: The token is expired"
595595
596596
597+ async def test_access_token_expired (
598+ test_client , test_auth_settings : AuthSettings , auth_httpx_mock : HTTPXMock
599+ ):
600+ """Test the expiration date of the passed access token.
601+ - get an access token
602+ - decode it and change the expiration time
603+ - recode it (with the JWK of the server).
604+ """
605+ # Get access token
606+ initial_access_token = _get_tokens (test_client )["access_token" ]
607+
608+ # Decode it
609+ access_payload = jwt .decode (
610+ initial_access_token , options = {"verify_signature" : False }
611+ )
612+
613+ # Modify the expiration time (utc now - 5 hours)
614+ access_payload ["exp" ] = int (
615+ (datetime .now (tz = timezone .utc ) - timedelta (hours = 5 )).timestamp ()
616+ )
617+
618+ # Encode it differently
619+ new_access_token = create_token (access_payload , test_auth_settings )
620+
621+ headers = {"Authorization" : f"Bearer { new_access_token } " }
622+
623+ # Try to get the userinfo using the invalid access token
624+ # The server should detect that it is not encoded properly
625+ r = test_client .get ("/api/auth/userinfo" , headers = headers )
626+ data = r .json ()
627+ assert r .status_code == 401 , data
628+ assert data ["detail" ] == "Invalid JWT"
629+
630+
597631async def test_refresh_token_rotated_expiration_time (
598632 test_client , test_auth_settings : AuthSettings , auth_httpx_mock : HTTPXMock
599633):
0 commit comments