Skip to content

feat: Refactor session claims and tests #211

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions supertokens_python/recipe/session/api/implementation.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
)
from supertokens_python.types import MaybeAwaitable
from supertokens_python.utils import normalise_http_method
from ..utils import get_required_claim_validators

if TYPE_CHECKING:
from supertokens_python.recipe.session.interfaces import APIOptions
Expand Down Expand Up @@ -67,7 +68,7 @@ async def verify_session(
session_required: bool,
override_global_claim_validators: Optional[
Callable[
[SessionContainer, List[SessionClaimValidator], Dict[str, Any]],
[List[SessionClaimValidator], SessionContainer, Dict[str, Any]],
MaybeAwaitable[List[SessionClaimValidator]],
]
],
Expand All @@ -91,10 +92,15 @@ async def verify_session(
)

if session is not None:
await api_options.recipe_implementation.assert_claims(
claim_validators = await get_required_claim_validators(
session,
override_global_claim_validators,
user_context,
)
await api_options.recipe_implementation.assert_claims(
session,
claim_validators,
user_context,
)

return session
121 changes: 117 additions & 4 deletions supertokens_python/recipe/session/asyncio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,15 @@
SessionInformationResult,
SessionClaim,
SessionClaimValidator,
SessionDoesntExistError,
ValidateClaimsOkResult,
JSONObject,
GetClaimValueOkResult,
)
from supertokens_python.recipe.session.recipe import SessionRecipe
from supertokens_python.types import MaybeAwaitable
from supertokens_python.utils import FRAMEWORKS
from supertokens_python.utils import FRAMEWORKS, resolve
from ..utils import get_required_claim_validators
from ...jwt.interfaces import (
CreateJwtOkResult,
CreateJwtResultUnsupportedAlgorithm,
Expand Down Expand Up @@ -53,11 +58,118 @@ async def create_new_session(
)


async def validate_claims_for_session_handle(
session_handle: str,
override_global_claim_validators: Optional[
Callable[
[
List[SessionClaimValidator],
SessionInformationResult,
Dict[str, Any],
], # Prev. 2nd arg was SessionContainer
MaybeAwaitable[List[SessionClaimValidator]],
]
] = None,
user_context: Union[None, Dict[str, Any]] = None,
) -> Union[SessionDoesntExistError, ValidateClaimsOkResult]:
if user_context is None:
user_context = {}

recipe_impl = SessionRecipe.get_instance().recipe_implementation
session_info = await recipe_impl.get_session_information(
session_handle, user_context
)

if session_info is None:
return SessionDoesntExistError()

claim_validators_added_by_other_recipes = (
SessionRecipe.get_claim_validators_added_by_other_recipes()
)
global_claim_validators = await resolve(
recipe_impl.get_global_claim_validators(
session_info.user_id,
claim_validators_added_by_other_recipes,
user_context,
)
)

if override_global_claim_validators is not None:
claim_validators = await resolve(
override_global_claim_validators(
global_claim_validators, session_info, user_context
)
)
else:
claim_validators = global_claim_validators

return await recipe_impl.validate_claims_for_session_handle(
session_info, claim_validators, user_context
)


async def validate_claims_in_jwt_payload(
user_id: str,
jwt_payload: JSONObject,
override_global_claim_validators: Optional[
Callable[
[
List[SessionClaimValidator],
str,
Dict[str, Any],
], # Prev. 2nd arg was SessionContainer
MaybeAwaitable[List[SessionClaimValidator]],
]
] = None,
user_context: Union[None, Dict[str, Any]] = None,
):
if user_context is None:
user_context = {}

recipe_impl = SessionRecipe.get_instance().recipe_implementation

claim_validators_added_by_other_recipes = (
SessionRecipe.get_claim_validators_added_by_other_recipes()
)
global_claim_validators = await resolve(
recipe_impl.get_global_claim_validators(
user_id,
claim_validators_added_by_other_recipes,
user_context,
)
)

if override_global_claim_validators is not None:
claim_validators = await resolve(
override_global_claim_validators(
global_claim_validators, user_id, user_context
)
)
else:
claim_validators = global_claim_validators

return await recipe_impl.validate_claims_in_jwt_payload(
user_id, jwt_payload, claim_validators, user_context
)


async def fetch_and_set_claim(
session_handle: str,
claim: SessionClaim[Any],
user_context: Union[None, Dict[str, Any]] = None,
) -> bool:
if user_context is None:
user_context = {}
return await SessionRecipe.get_instance().recipe_implementation.fetch_and_set_claim(
session_handle, claim, user_context
)


async def get_claim_value(
session_handle: str,
claim: SessionClaim[_T],
user_context: Union[None, Dict[str, Any]] = None,
) -> Union[_T, None]:
) -> Union[SessionDoesntExistError, GetClaimValueOkResult[_T]]:
if user_context is None:
user_context = {}
return await SessionRecipe.get_instance().recipe_implementation.get_claim_value(
Expand Down Expand Up @@ -96,7 +208,7 @@ async def get_session(
session_required: bool = True,
override_global_claim_validators: Optional[
Callable[
[SessionContainer, List[SessionClaimValidator], Dict[str, Any]],
[List[SessionClaimValidator], SessionContainer, Dict[str, Any]],
MaybeAwaitable[List[SessionClaimValidator]],
]
] = None,
Expand All @@ -119,9 +231,10 @@ async def get_session(
)

if session is not None:
await session_recipe_impl.assert_claims(
claim_validators = await get_required_claim_validators(
session, override_global_claim_validators, user_context
)
await session_recipe_impl.assert_claims(session, claim_validators, user_context)

return session

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def should_refetch(
):
return claim.get_value_from_payload(payload, user_context) is None

def validate(
async def validate(
self,
payload: JSONObject,
user_context: Union[Dict[str, Any], None] = None,
Expand Down Expand Up @@ -93,23 +93,26 @@ def should_refetch(
claim.get_value_from_payload(payload, user_context) is None
) or (payload[claim.key]["t"] < time.time() - max_age_in_sec * 1000)

def validate(
async def validate(
self,
payload: JSONObject,
user_context: Union[Dict[str, Any], None] = None,
):
claim_val = claim.get_value_from_payload(payload, user_context)
if claim_val != val:
if claim_val is None:
return {
"isValid": False,
"reason": {
"message": "wrong value",
"message": "value does not exist",
"expectedValue": val,
"actualValue": claim_val,
},
}

age_in_sec = (time.time() - payload[claim.key]["t"]) / 1000
age_in_sec = (
time.time()
- float(claim.get_last_refetch_time(payload, user_context) or 0)
) / 1000
if age_in_sec > max_age_in_sec:
return {
"isValid": False,
Expand All @@ -119,6 +122,15 @@ def validate(
"maxAgeInSeconds": max_age_in_sec,
},
}
if claim_val != val:
return {
"isValid": False,
"reason": {
"message": "wrong value",
"expectedValue": val,
"actualValue": claim_val,
},
}

return {"isValid": True}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def verify_session(
user_context: Union[None, Dict[str, Any]] = None,
override_global_claim_validators: Optional[
Callable[
[SessionContainer, List[SessionClaimValidator], Dict[str, Any]],
[List[SessionClaimValidator], SessionContainer, Dict[str, Any]],
MaybeAwaitable[List[SessionClaimValidator]],
]
] = None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def verify_session(
user_context: Union[None, Dict[str, Any]] = None,
override_global_claim_validators: Optional[
Callable[
[SessionContainer, List[SessionClaimValidator], Dict[str, Any]],
[List[SessionClaimValidator], SessionContainer, Dict[str, Any]],
MaybeAwaitable[List[SessionClaimValidator]],
]
] = None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def verify_session(
user_context: Union[None, Dict[str, Any]] = None,
override_global_claim_validators: Optional[
Callable[
[SessionContainer, List[SessionClaimValidator], Dict[str, Any]],
[List[SessionClaimValidator], SessionContainer, Dict[str, Any]],
MaybeAwaitable[List[SessionClaimValidator]],
]
] = None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def verify_session(
user_context: Union[None, Dict[str, Any]] = None,
override_global_claim_validators: Optional[
Callable[
[SessionContainer, List[SessionClaimValidator], Dict[str, Any]],
[List[SessionClaimValidator], SessionContainer, Dict[str, Any]],
MaybeAwaitable[List[SessionClaimValidator]],
]
] = None,
Expand Down
Loading