himanshukukreja
08/16/2023, 1:00 PMrp_st
08/16/2023, 1:30 PMawait session.fetchAndSetClaim(userroleclaim)
as shown in this doc: https://supertokens.com/docs/userroles/managing-roles-and-usershimanshukukreja
08/17/2023, 7:52 AMmergeIntoAccessTokenPayload
(for setting the custom claims into the payload) instead of await session.fetch_and_set_claim(UserRoleClaim)
function . Will both functions do the same thing??rp_st
08/17/2023, 8:16 AMrp_st
08/17/2023, 8:16 AMhimanshukukreja
08/17/2023, 8:50 AM@router.put("/user-role/{userId}")
async def assign_role(user_id:str, request_role: UserRole.UserRole,session: SessionContainer = Depends(verify_session()):
role = request_role.role
res = await add_role_to_user(user_id, role)
if isinstance(res, UnknownRoleError):
logger.error(f"{role} role doesn't Exists")
raise HTTPException(status_code=404, detail=f"{role} role doesn't Exists")
if res.did_user_already_have_role:
logger.error(f"User already has {role} role")
raise HTTPException(status_code=409, detail=f"User already has {role} role")
await session.fetch_and_set_claim(UserRoleClaim)
await session.fetch_and_set_claim(PermissionClaim)
logger.info(f"Role {role} added to user {user_id}")
return {"message": f"Role {role} added to user {user_id}"}
So this is my API which I want to protect
@router.get("/test_claims")
async def test_excluded(session: SessionContainer = Depends(verify_session(override_global_claim_validators=lambda global_validators, session, user_context: global_validators + [UserRoleClaim.validators.includes("Admin")]))):
return {"message": "Hello World"}
Now this API gives me this response (not particularly error but an invalid claim exception)
{
"message": "invalid claim",
"claimValidationErrors": [
{
"id": "st-role",
"reason": {
"message": "wrong value",
"expectedToInclude": "Admin",
"actualValue": [
"role1",
"role2",
"role3"
]
}
}
]
}
rp_st
08/17/2023, 8:51 AMhimanshukukreja
08/17/2023, 8:53 AMaccess-control-allow-credentials: true
access-control-expose-headers: front-token,front-token,front-token
content-length: 71
content-type: application/json
date: Thu,17 Aug 2023 08:37:25 GMT
front-token: <front-token (jwt)>
server: uvicorn
x-process-time: 1.3052849769592285
rp_st
08/17/2023, 8:54 AMrp_st
08/17/2023, 8:54 AMKShivendu
08/17/2023, 8:58 AMrp_st
08/17/2023, 8:59 AMrp_st
08/17/2023, 8:59 AMrp_st
08/17/2023, 9:02 AMhimanshukukreja
08/17/2023, 9:03 AMHTTP/1.1 200 OK
date: Thu, 17 Aug 2023 08:59:01 GMT
server: uvicorn
content-length: 72
content-type: application/json
front-token: <jwt front token>
access-control-expose-headers: front-token,front-token
set-cookie: sAccessToken=<jwt access token>; Domain=localhost; expires=Sat, 24 Jul 2123 08:59:02 GMT; HttpOnly; Path=/; SameSite=lax
access-control-allow-credentials: true
x-process-time: 1.306797981262207
set-cookie header is present in the response
Also I tried to decode the access token from response headers from jwt.io
The role claim was not present in itrp_st
08/17/2023, 9:05 AMhimanshukukreja
08/17/2023, 9:13 AM<supertokens_python.recipe.userroles.interfaces.AddRoleToUserOkResult object at 0x000001D501ED3510>
rp_st
08/17/2023, 9:13 AMhimanshukukreja
08/17/2023, 9:14 AMrp_st
08/17/2023, 9:15 AMhimanshukukreja
08/17/2023, 9:16 AMrp_st
08/17/2023, 9:25 AMhimanshukukreja
08/17/2023, 9:26 AMhimanshukukreja
08/17/2023, 9:36 AMrp_st
08/17/2023, 9:41 AMrp_st
08/17/2023, 9:41 AMrp_st
08/17/2023, 9:41 AMrp_st
08/17/2023, 9:41 AMhimanshukukreja
08/17/2023, 9:48 AMrp_st
08/17/2023, 9:48 AMrp_st
08/17/2023, 9:49 AMhimanshukukreja
08/17/2023, 9:52 AMrp_st
08/17/2023, 9:52 AMhimanshukukreja
08/17/2023, 9:53 AMrp_st
08/17/2023, 9:54 AMhimanshukukreja
08/17/2023, 9:55 AM