Skip to content

Commit 1c395a7

Browse files
committed
refactor: Serialize ClaimValidationError in default implementation of invalid claim handler
1 parent 0322472 commit 1c395a7

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

supertokens_python/recipe/session/exceptions.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,7 @@ class TryRefreshTokenError(SuperTokensSessionError):
5656
class InvalidClaimsError(SuperTokensSessionError):
5757
def __init__(self, msg: str, payload: List[ClaimValidationError]):
5858
super().__init__(msg)
59-
self.payload: List[Dict[str, Any]] = []
60-
for p in payload:
61-
res = (
62-
p.__dict__.copy()
63-
) # Must be JSON serializable as it will be used in response
64-
if p.reason is None:
65-
res.pop("reason")
66-
self.payload.append(res)
59+
self.payload = payload
6760

6861

6962
class ClaimValidationError:

supertokens_python/recipe/session/utils.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def __init__(
137137
Union[BaseResponse, Awaitable[BaseResponse]],
138138
],
139139
on_invalid_claim: Callable[
140-
[BaseRequest, List[Dict[str, Any]], BaseResponse],
140+
[BaseRequest, List[ClaimValidationError], BaseResponse],
141141
Union[BaseResponse, Awaitable[BaseResponse]],
142142
],
143143
):
@@ -185,7 +185,7 @@ async def on_invalid_claim(
185185
self,
186186
recipe: SessionRecipe,
187187
request: BaseRequest,
188-
claim_validation_errors: List[Dict[str, Any]],
188+
claim_validation_errors: List[ClaimValidationError],
189189
response: BaseResponse,
190190
):
191191
_ = recipe
@@ -214,7 +214,7 @@ def __init__(
214214
] = None,
215215
on_invalid_claim: Union[
216216
Callable[
217-
[BaseRequest, List[Dict[str, Any]], BaseResponse],
217+
[BaseRequest, List[ClaimValidationError], BaseResponse],
218218
Union[BaseResponse, Awaitable[BaseResponse]],
219219
],
220220
None,
@@ -275,13 +275,23 @@ async def default_token_theft_detected_callback(
275275

276276
async def default_invalid_claim_callback(
277277
_: BaseRequest,
278-
claim_validation_errors: List[Dict[str, Any]],
278+
claim_validation_errors: List[ClaimValidationError],
279279
response: BaseResponse,
280280
) -> BaseResponse:
281281
from .recipe import SessionRecipe
282282

283+
payload: List[Dict[str, Any]] = []
284+
285+
for p in claim_validation_errors:
286+
res = (
287+
p.__dict__.copy()
288+
) # Must be JSON serializable as it will be used in response
289+
if p.reason is None:
290+
res.pop("reason")
291+
payload.append(res)
292+
283293
return send_non_200_response(
284-
{"message": "invalid claim", "claimValidationErrors": claim_validation_errors},
294+
{"message": "invalid claim", "claimValidationErrors": payload},
285295
SessionRecipe.get_instance().config.invalid_claim_status_code,
286296
response,
287297
)

0 commit comments

Comments
 (0)