Skip to content

Commit 14051de

Browse files
authored
feat: add matchingUri support for listing resources with wildcards (#592)
* feat: add matchingUri support for listing resources with wildcards * fix: change formatting
1 parent 78a0aa1 commit 14051de

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/keycloak/keycloak_uma.py

+10
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ def resource_set_list_ids(
210210
owner: str = "",
211211
resource_type: str = "",
212212
scope: str = "",
213+
matchingUri: bool = False,
213214
first: int = 0,
214215
maximum: int = -1,
215216
):
@@ -230,6 +231,8 @@ def resource_set_list_ids(
230231
:type resource_type: str
231232
:param scope: query resource scope
232233
:type scope: str
234+
:param matchingUri: enable URI matching
235+
:type matchingUri: bool
233236
:param first: index of first matching resource to return
234237
:type first: int
235238
:param maximum: maximum number of resources to return (-1 for all)
@@ -250,6 +253,8 @@ def resource_set_list_ids(
250253
query["type"] = resource_type
251254
if scope:
252255
query["scope"] = scope
256+
if matchingUri:
257+
query["matchingUri"] = "true"
253258
if first > 0:
254259
query["first"] = first
255260
if maximum >= 0:
@@ -544,6 +549,7 @@ async def a_resource_set_list_ids(
544549
owner: str = "",
545550
resource_type: str = "",
546551
scope: str = "",
552+
matchingUri: bool = False,
547553
first: int = 0,
548554
maximum: int = -1,
549555
):
@@ -565,6 +571,8 @@ async def a_resource_set_list_ids(
565571
:param scope: query resource scope
566572
:type scope: str
567573
:param first: index of first matching resource to return
574+
:param matchingUri: enable URI matching
575+
:type matchingUri: bool
568576
:type first: int
569577
:param maximum: maximum number of resources to return (-1 for all)
570578
:type maximum: int
@@ -584,6 +592,8 @@ async def a_resource_set_list_ids(
584592
query["type"] = resource_type
585593
if scope:
586594
query["scope"] = scope
595+
if matchingUri:
596+
query["matchingUri"] = "true"
587597
if first > 0:
588598
query["first"] = first
589599
if maximum >= 0:

tests/test_keycloak_uma.py

+22
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,23 @@ def test_uma_resource_sets(uma: KeycloakUMA):
9696
"name": "mytest",
9797
"scopes": ["test:read", "test:write"],
9898
"type": "urn:test",
99+
"uris": ["/some_resources/*"],
99100
}
100101
created_resource = uma.resource_set_create(resource_to_create)
101102
assert created_resource
102103
assert created_resource["_id"], created_resource
103104
assert set(resource_to_create).issubset(set(created_resource)), created_resource
104105

106+
# Test getting resource with wildcard
107+
# Without matchingUri query option
108+
resource_set_list_ids = uma.resource_set_list_ids(uri="/some_resources/resource")
109+
assert len(resource_set_list_ids) == 0
110+
# With matchingUri query option
111+
resource_set_list_ids = uma.resource_set_list_ids(
112+
uri="/some_resources/resource", matchingUri=True
113+
)
114+
assert len(resource_set_list_ids) == 1
115+
105116
# Test create the same resource set
106117
with pytest.raises(KeycloakPostError) as err:
107118
uma.resource_set_create(resource_to_create)
@@ -382,12 +393,23 @@ async def test_a_uma_resource_sets(uma: KeycloakUMA):
382393
"name": "mytest",
383394
"scopes": ["test:read", "test:write"],
384395
"type": "urn:test",
396+
"uris": ["/some_resources/*"],
385397
}
386398
created_resource = await uma.a_resource_set_create(resource_to_create)
387399
assert created_resource
388400
assert created_resource["_id"], created_resource
389401
assert set(resource_to_create).issubset(set(created_resource)), created_resource
390402

403+
# Test getting resource with wildcard
404+
# Without matchingUri query option
405+
resource_set_list_ids = await uma.a_resource_set_list_ids(uri="/some_resources/resource")
406+
assert len(resource_set_list_ids) == 0
407+
# With matchingUri query option
408+
resource_set_list_ids = await uma.a_resource_set_list_ids(
409+
uri="/some_resources/resource", matchingUri=True
410+
)
411+
assert len(resource_set_list_ids) == 1
412+
391413
# Test create the same resource set
392414
with pytest.raises(KeycloakPostError) as err:
393415
await uma.a_resource_set_create(resource_to_create)

0 commit comments

Comments
 (0)