Skip to content

Commit f5f85b7

Browse files
authored
Option to bind access key to user (#342)
* Option to bind access key to user related to descope/etc#4312 * Try to fix build
1 parent 79288a3 commit f5f85b7

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,7 @@ You can create, update, delete or load access keys, as well as search according
688688
# An access key must have a name and expiration, other fields are optional.
689689
# Roles should be set directly if no tenants exist, otherwise set
690690
# on a per-tenant basis.
691+
# If user_id is supplied, then authorization would be ignored, and access key would be bound to the users authorization
691692
create_resp = descope_client.mgmt.access_key.create(
692693
name="name",
693694
expire_time=1677844931,

descope/management/access_key.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ def create(
1515
expire_time: int = 0,
1616
role_names: Optional[List[str]] = None,
1717
key_tenants: Optional[List[AssociatedTenant]] = None,
18+
user_id: Optional[str] = None,
1819
) -> dict:
1920
"""
2021
Create a new access key.
@@ -26,6 +27,8 @@ def create(
2627
mutually exclusive with the `key_tenant` roles, which take precedence over them.
2728
key_tenants (List[AssociatedTenant]): An optional list of the access key's tenants, and optionally, their roles per tenant. These roles are
2829
mutually exclusive with the general `role_names`, and take precedence over them.
30+
user_id (str): Bind access key to this user id
31+
If user_id is supplied, then authorization would be ignored, and access key would be bound to the users authorization
2932
3033
Return value (dict):
3134
Return dict in the format
@@ -44,7 +47,9 @@ def create(
4447

4548
response = self._auth.do_post(
4649
MgmtV1.access_key_create_path,
47-
AccessKey._compose_create_body(name, expire_time, role_names, key_tenants),
50+
AccessKey._compose_create_body(
51+
name, expire_time, role_names, key_tenants, user_id
52+
),
4853
pswd=self._auth.management_key,
4954
)
5055
return response.json()
@@ -188,10 +193,12 @@ def _compose_create_body(
188193
expire_time: int,
189194
role_names: List[str],
190195
key_tenants: List[AssociatedTenant],
196+
user_id: Optional[str] = None,
191197
) -> dict:
192198
return {
193199
"name": name,
194200
"expireTime": expire_time,
195201
"roleNames": role_names,
196202
"keyTenants": associated_tenants_to_dict(key_tenants),
203+
"userId": user_id,
197204
}

tests/management/test_access_key.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ def test_create(self):
5656
AssociatedTenant("tenant1"),
5757
AssociatedTenant("tenant2", ["role1", "role2"]),
5858
],
59+
user_id="userid",
5960
)
6061
access_key = resp["key"]
6162
self.assertEqual(access_key["id"], "ak1")
@@ -74,6 +75,7 @@ def test_create(self):
7475
{"tenantId": "tenant1", "roleNames": []},
7576
{"tenantId": "tenant2", "roleNames": ["role1", "role2"]},
7677
],
78+
"userId": "userid",
7779
},
7880
allow_redirects=False,
7981
verify=True,

0 commit comments

Comments
 (0)