|
| 1 | +import pytest |
| 2 | +from supertokens_python import init |
| 3 | +from supertokens_python.recipe import session |
| 4 | +from supertokens_python.recipe.session.asyncio import ( |
| 5 | + create_new_session_without_request_response, |
| 6 | + get_session_without_request_response, |
| 7 | + refresh_session_without_request_response, |
| 8 | +) |
| 9 | +from supertokens_python.recipe.session.session_class import SessionContainer |
| 10 | +from tests.utils import ( |
| 11 | + get_st_init_args, |
| 12 | + setup_function, |
| 13 | + start_st, |
| 14 | + teardown_function, |
| 15 | + reset, |
| 16 | +) |
| 17 | +from supertokens_python.recipe.session.jwt import ( |
| 18 | + parse_jwt_without_signature_verification, |
| 19 | +) |
| 20 | +from supertokens_python.recipe.session.interfaces import GetSessionTokensDangerouslyDict |
| 21 | + |
| 22 | +_ = setup_function # type:ignore |
| 23 | +_ = teardown_function # type:ignore |
| 24 | + |
| 25 | +pytestmark = pytest.mark.asyncio |
| 26 | + |
| 27 | + |
| 28 | +async def test_dynamic_key_switching(): |
| 29 | + init(**get_st_init_args([session.init(use_dynamic_access_token_signing_key=True)])) |
| 30 | + start_st() |
| 31 | + |
| 32 | + # Create a new session without an actual HTTP request-response flow |
| 33 | + create_res: SessionContainer = await create_new_session_without_request_response( |
| 34 | + "public", "test-user-id", {"tokenProp": True}, {"dbProp": True} |
| 35 | + ) |
| 36 | + |
| 37 | + # Extract session tokens for further testing |
| 38 | + tokens = create_res.get_all_session_tokens_dangerously() |
| 39 | + check_access_token_signing_key_type(tokens, True) |
| 40 | + |
| 41 | + # Reset and reinitialize with dynamic signing key disabled |
| 42 | + reset(stop_core=False) |
| 43 | + init(**get_st_init_args([session.init(use_dynamic_access_token_signing_key=False)])) |
| 44 | + |
| 45 | + caught_exception = None |
| 46 | + try: |
| 47 | + # Attempt to retrieve the session using previously obtained tokens |
| 48 | + await get_session_without_request_response( |
| 49 | + tokens["accessToken"], tokens["antiCsrfToken"] |
| 50 | + ) |
| 51 | + except Exception as e: |
| 52 | + caught_exception = e |
| 53 | + |
| 54 | + # Check for the expected exception due to token signing key mismatch |
| 55 | + assert ( |
| 56 | + caught_exception is not None |
| 57 | + ), "Expected an exception to be thrown, but none was." |
| 58 | + assert ( |
| 59 | + str(caught_exception) |
| 60 | + == "The access token doesn't match the use_dynamic_access_token_signing_key setting" |
| 61 | + ), f"Unexpected exception message: {str(caught_exception)}" |
| 62 | + |
| 63 | + |
| 64 | +async def test_refresh_session(): |
| 65 | + init(**get_st_init_args([session.init(use_dynamic_access_token_signing_key=True)])) |
| 66 | + start_st() |
| 67 | + |
| 68 | + # Create a new session without an actual HTTP request-response flow |
| 69 | + create_res: SessionContainer = await create_new_session_without_request_response( |
| 70 | + "public", "test-user-id", {"tokenProp": True}, {"dbProp": True} |
| 71 | + ) |
| 72 | + |
| 73 | + # Extract session tokens for further testing |
| 74 | + tokens = create_res.get_all_session_tokens_dangerously() |
| 75 | + check_access_token_signing_key_type(tokens, True) |
| 76 | + |
| 77 | + # Reset and reinitialize with dynamic signing key disabled |
| 78 | + reset(stop_core=False) |
| 79 | + init(**get_st_init_args([session.init(use_dynamic_access_token_signing_key=False)])) |
| 80 | + |
| 81 | + assert tokens["refreshToken"] is not None |
| 82 | + |
| 83 | + # Refresh session |
| 84 | + refreshed_session = await refresh_session_without_request_response( |
| 85 | + tokens["refreshToken"], True, tokens["antiCsrfToken"] |
| 86 | + ) |
| 87 | + tokens_after_refresh = refreshed_session.get_all_session_tokens_dangerously() |
| 88 | + assert tokens_after_refresh["accessAndFrontTokenUpdated"] is True |
| 89 | + check_access_token_signing_key_type(tokens_after_refresh, False) |
| 90 | + |
| 91 | + # Verify session after refresh |
| 92 | + verified_session = await get_session_without_request_response( |
| 93 | + tokens_after_refresh["accessToken"], tokens_after_refresh["antiCsrfToken"] |
| 94 | + ) |
| 95 | + assert verified_session is not None |
| 96 | + tokens_after_verify = verified_session.get_all_session_tokens_dangerously() |
| 97 | + assert tokens_after_verify["accessAndFrontTokenUpdated"] is True |
| 98 | + check_access_token_signing_key_type(tokens_after_verify, False) |
| 99 | + |
| 100 | + # Verify session again |
| 101 | + verified2_session = await get_session_without_request_response( |
| 102 | + tokens_after_verify["accessToken"], tokens_after_verify["antiCsrfToken"] |
| 103 | + ) |
| 104 | + assert verified2_session is not None |
| 105 | + tokens_after_verify2 = verified2_session.get_all_session_tokens_dangerously() |
| 106 | + assert tokens_after_verify2["accessAndFrontTokenUpdated"] is False |
| 107 | + |
| 108 | + |
| 109 | +def check_access_token_signing_key_type( |
| 110 | + tokens: GetSessionTokensDangerouslyDict, is_dynamic: bool |
| 111 | +): |
| 112 | + info = parse_jwt_without_signature_verification(tokens["accessToken"]) |
| 113 | + if is_dynamic: |
| 114 | + assert info.kid is not None and info.kid.startswith("d-") |
| 115 | + else: |
| 116 | + assert info.kid is not None and info.kid.startswith("s-") |
0 commit comments