Skip to content

Commit 28dda50

Browse files
authored
Impersonate with custom claims (#470)
* Impersonate with custom claims And with selected tenant related to descope/etc#8724 * Properly mark dict as optional * fix test
1 parent 4848bad commit 28dda50

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,7 +1062,9 @@ The response would be a refresh JWT of the impersonated user
10621062
refresh_jwt = descope_client.mgmt.jwt.impersonate(
10631063
impersonator_id="<Login ID impersonator>",
10641064
login_id="<Login ID of impersonated person>",
1065-
validate_consent=True
1065+
validate_consent=True,
1066+
custom_claims={"key1":"value1"},
1067+
tenant_id="<One of the tenants the impersonated user belongs to>"
10661068
)
10671069
```
10681070

@@ -1126,8 +1128,8 @@ type org
11261128
type folder
11271129
relation parent: folder
11281130
relation owner: user | org#member
1129-
relation editor: user
1130-
relation viewer: user
1131+
relation editor: user
1132+
relation viewer: user
11311133

11321134
permission can_create: owner | parent.owner
11331135
permission can_edit: editor | can_create
@@ -1136,8 +1138,8 @@ type folder
11361138
type doc
11371139
relation parent: folder
11381140
relation owner: user | org#member
1139-
relation editor: user
1140-
relation viewer: user
1141+
relation editor: user
1142+
relation viewer: user
11411143

11421144
permission can_create: owner | parent.owner
11431145
permission can_edit: editor | can_create

descope/management/jwt.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Optional
2+
13
from descope._auth_base import AuthBase
24
from descope.exceptions import ERROR_TYPE_INVALID_ARGUMENT, AuthException
35
from descope.management.common import MgmtV1
@@ -27,7 +29,12 @@ def update_jwt(self, jwt: str, custom_claims: dict) -> str:
2729
return response.json().get("jwt", "")
2830

2931
def impersonate(
30-
self, impersonator_id: str, login_id: str, validate_consent: bool
32+
self,
33+
impersonator_id: str,
34+
login_id: str,
35+
validate_consent: bool,
36+
custom_claims: Optional[dict] = None,
37+
tenant_id: Optional[str] = None,
3138
) -> str:
3239
"""
3340
Impersonate to another user
@@ -36,6 +43,8 @@ def impersonate(
3643
impersonator_id (str): login id / user id of impersonator, must have "impersonation" permission.
3744
login_id (str): login id of the user whom to which to impersonate to.
3845
validate_consent (bool): Indicate whether to allow impersonation in any case or only if a consent to this operation was granted.
46+
customClaims dict: Custom claims to add to JWT
47+
tenant_id (str): tenant id to set on DCT claim.
3948
4049
Return value (str): A JWT of the impersonated user
4150
@@ -56,6 +65,8 @@ def impersonate(
5665
"loginId": login_id,
5766
"impersonatorId": impersonator_id,
5867
"validateConsent": validate_consent,
68+
"cusotmClaims": custom_claims,
69+
"selectedTenant": tenant_id,
5970
},
6071
pswd=self._auth.management_key,
6172
)

tests/management/test_jwt.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ def test_impersonate(self):
107107
"loginId": "imp2",
108108
"impersonatorId": "imp1",
109109
"validateConsent": True,
110+
"cusotmClaims": None,
111+
"selectedTenant": None,
110112
},
111113
allow_redirects=False,
112114
verify=True,

0 commit comments

Comments
 (0)