Skip to content

Commit f921d12

Browse files
authoredJan 27, 2025··
Allow updating jwt expiry via SDK (#478)
Supported as part of jwt update flow +test fixes descope/etc#8900
1 parent 912c683 commit f921d12

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed
 

‎descope/management/jwt.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@
66

77

88
class JWT(AuthBase):
9-
def update_jwt(self, jwt: str, custom_claims: dict) -> str:
9+
def update_jwt(
10+
self, jwt: str, custom_claims: dict, refresh_duration: Optional[int]
11+
) -> str:
1012
"""
1113
Given a valid JWT, update it with custom claims, and update its authz claims as well
1214
1315
Args:
1416
token (str): valid jwt.
1517
custom_claims (dict): Custom claims to add to JWT, system claims will be filtered out
18+
refresh_duration (int): duration in seconds for which the new JWT will be valid
1619
1720
Return value (str): the newly updated JWT
1821
@@ -23,7 +26,11 @@ def update_jwt(self, jwt: str, custom_claims: dict) -> str:
2326
raise AuthException(400, ERROR_TYPE_INVALID_ARGUMENT, "jwt cannot be empty")
2427
response = self._auth.do_post(
2528
MgmtV1.update_jwt_path,
26-
{"jwt": jwt, "customClaims": custom_claims},
29+
{
30+
"jwt": jwt,
31+
"customClaims": custom_claims,
32+
"refreshDuration": refresh_duration,
33+
},
2734
pswd=self._auth.management_key,
2835
)
2936
return response.json().get("jwt", "")

‎tests/management/test_jwt.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ def test_update_jwt(self):
3636
with patch("requests.post") as mock_post:
3737
mock_post.return_value.ok = False
3838
self.assertRaises(
39-
AuthException, client.mgmt.jwt.update_jwt, "jwt", {"k1": "v1"}
39+
AuthException, client.mgmt.jwt.update_jwt, "jwt", {"k1": "v1"}, 0
4040
)
4141

4242
self.assertRaises(
43-
AuthException, client.mgmt.jwt.update_jwt, "", {"k1": "v1"}
43+
AuthException, client.mgmt.jwt.update_jwt, "", {"k1": "v1"}, 0
4444
)
4545

4646
# Test success flow
@@ -49,7 +49,7 @@ def test_update_jwt(self):
4949
network_resp.ok = True
5050
network_resp.json.return_value = json.loads("""{"jwt": "response"}""")
5151
mock_post.return_value = network_resp
52-
resp = client.mgmt.jwt.update_jwt("test", {"k1": "v1"})
52+
resp = client.mgmt.jwt.update_jwt("test", {"k1": "v1"}, 40)
5353
self.assertEqual(resp, "response")
5454
expected_uri = f"{common.DEFAULT_BASE_URL}{MgmtV1.update_jwt_path}"
5555
mock_post.assert_called_with(
@@ -58,7 +58,11 @@ def test_update_jwt(self):
5858
**common.default_headers,
5959
"Authorization": f"Bearer {self.dummy_project_id}:{self.dummy_management_key}",
6060
},
61-
json={"jwt": "test", "customClaims": {"k1": "v1"}},
61+
json={
62+
"jwt": "test",
63+
"customClaims": {"k1": "v1"},
64+
"refreshDuration": 40,
65+
},
6266
allow_redirects=False,
6367
verify=True,
6468
params=None,

0 commit comments

Comments
 (0)
Please sign in to comment.