da.fant
06/22/2023, 9:09 AMrp_st
06/22/2023, 9:11 AMrp_st
06/22/2023, 9:11 AMda.fant
06/22/2023, 9:54 AMget_roles_for_user
, add_role_to_user
, etc? I'm assuming not because this isn't a user, it's just a token that doesn't have any concept of user/permissionsda.fant
06/22/2023, 10:14 AM{token_id}@tokens.example.com
with some pass stored in our DB (e.g. Token(organization_id, password)
)
4. Then each org can have multiple token users, and permissions can be regulated on a per-user levelda.fant
06/22/2023, 10:22 AMrp_st
06/22/2023, 10:26 AMrp_st
06/22/2023, 10:27 AMrp_st
06/22/2023, 10:27 AMda.fant
06/22/2023, 1:11 PM{
"sub": "my-custom-arbitrary-id",
"exp": 1687442684,
"iat": 1687439084,
"sessionHandle": "a8c22f92-dc04-4d4d-968d-a371849300fa",
"refreshTokenHash1": "2936c8c0c0ad8bb3fa1a1d892a018bfb6b9fd0af4331b060f68c40b95684e1c2",
"parentRefreshTokenHash1": null,
"antiCsrfToken": null,
"iss": "http://localhost:8000/auth"
}
I ideally want to use something similar to what I'm currently doing:
python
@router.post('...')
async def endpoint(
...,
organization_id: UUID,
auth_session: SessionContainer = Depends(verify_session()),
) -> ActionRunListItem:
await permissions.assert_permission(
auth_session.get_user_id(),
organization_id,
'organization.write',
)
# ...
async def assert_permission(user_id: str, organization_id: UUID, permission: Permission):
roles = (await get_roles_for_user(user_id)).roles
# logic to check if any roles have the permission
da.fant
06/22/2023, 1:13 PMverify_session
that does something like the /like-comment example here? https://supertokens.com/docs/microservice_auth/jwt-verification/index
Basically, wrap the current verify session, and if that returns no session we check if the bearer token resolves to this custom "API user id" we created and in that case return that user id, session, etcda.fant
06/22/2023, 1:25 PMrp_st
06/22/2023, 1:47 PMrp_st
06/22/2023, 1:51 PMda.fant
06/22/2023, 4:59 PMDo that , you can get from the session’s access token payload
- what does this mean? You mean I'd feed in the different fields from the JSON dump above (e.g. sub, sessionHandle, but not e.g. exp)?rp_st
06/22/2023, 5:02 PMrp_st
06/22/2023, 5:02 PMrp_st
06/22/2023, 5:03 PMda.fant
06/22/2023, 5:54 PMverify_session
(created with create_new_session_without_request_response
)
json
{
"sub": "sana-labs",
"exp": 1687459722,
"iat": 1687456122,
"sessionHandle": "b6442d9b-3cae-470f-bbbc-79a7874fe825",
"refreshTokenHash1": "e04bdb94ae7c1d0cdffb7d86fb9be2d57ee93ae85e61bca4e12c608208f1bd89",
"parentRefreshTokenHash1": null,
"antiCsrfToken": null,
"iss": "http://localhost:8000/auth"
}
```json
But the following doesn't:
```json
{
"sub": "sana-labs",
"source": "microservice",
"sessionHandle": "b6442d9b-3cae-470f-bbbc-79a7874fe825",
"iat": 1687456122,
"exp": 4841056122,
"iss": "http://localhost:8000"
}
This is the code:
python
user_id = "sana-labs"
session = await create_new_session_without_request_response(user_id)
jwtResponse = await asyncio.create_jwt({
'source': 'microservice',
'sub': session.get_user_id(),
'sessionHandle': session.get_handle(),
})
da.fant
06/22/2023, 5:55 PMrp_st
06/22/2023, 5:55 PMrp_st
06/22/2023, 5:55 PMda.fant
06/22/2023, 6:11 PMorganization:{organization_id}:{token_id}
where token_id is just some UUID. I want to list all of an organization's API users, which basically is all users where id starts with organization:{organization_id}:
Searched in the API ref but couldn't find it: https://app.swaggerhub.com/apis/supertokens/CDI/3.0.1rp_st
06/22/2023, 6:13 PMrp_st
06/22/2023, 6:13 PMda.fant
06/22/2023, 7:58 PMpython
async def create_organization_token(organization_id: UUID) -> str:
user_id = f"organization:{organization_id}:{uuid4()}"
session = await create_new_session_without_request_response(
user_id,
session_data_in_database={ 'organization_id': str(organization_id) }
)
session_access_token = session.get_access_token()
session_access_token_data = jwt.decode(session_access_token, options={"verify_signature": False})
del session_access_token_data['iat']
del session_access_token_data['exp']
# jwt_response = await jwt_asyncio.create_jwt({
# 'source': 'organization_token',
# **session_access_token_data,
# })
jwt_response = await jwt_asyncio.create_jwt({
"sub": "organization:614a7d4c-a1c3-4ebc-bd39-df90c503a264:0a16471f-69dc-48b3-bdee-8a8b713b0f4c",
"exp": 1687467128,
"iat": 1687463528,
"sessionHandle": "c00e7b86-653a-43e0-8f29-3750a6f21044",
"refreshTokenHash1": "27eb9e16c0a430ef4678508c002863fc0b8d76b136f8a955e636e23789077a34",
"parentRefreshTokenHash1": None,
"antiCsrfToken": None,
"iss": "http://localhost:8000/auth"
})
if not isinstance(jwt_response, CreateJwtOkResult):
raise Exception("Unable to create organization token")
return jwt_response.jwt
Any ideas?da.fant
06/22/2023, 7:59 PMrp_st
06/22/2023, 8:02 PMrp_st
06/22/2023, 8:03 PMda.fant
06/22/2023, 8:05 PMrp_st
06/22/2023, 8:57 PMpython
s = await create_new_session_without_request_response("userId", {}, {})
payload = s.get_access_token_payload()
del payload["iat"]
del payload["exp"]
now = get_timestamp_ms()
# expiry jwt after 10sec
jwt_expiry = now + 10 * 1000
jwt = await create_jwt(payload, jwt_expiry, use_static_signing_key=False)
s_ = await get_session_without_request_response(jwt.jwt)
rp_st
06/22/2023, 8:57 PMrp_st
06/22/2023, 8:57 PMrp_st
06/22/2023, 8:58 PMjwt_expiry
to anything, as well as add custom claims to payload
before passing it into create_jwt
da.fant
06/22/2023, 9:42 PMtry refresh token
error
1. shows original token (which works)
2. shows the jwt token with long expiry (where jwt.jwt
and s_.get_access_token()
in your code above have the same value)
Differences:
1. header in original token has version: 3
2. iat/exp
3. ordering of fields
Any idaes?rp_st
06/23/2023, 12:23 AMda.fant
06/23/2023, 10:02 PMda.fant
06/23/2023, 10:03 PMrp_st
06/24/2023, 4:12 AM