Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add matchingUri support for listing resources with wildcards #592

Merged
merged 2 commits into from
Sep 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/keycloak/keycloak_uma.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ def resource_set_list_ids(
owner: str = "",
resource_type: str = "",
scope: str = "",
matchingUri: bool = False,
first: int = 0,
maximum: int = -1,
):
Expand All @@ -230,6 +231,8 @@ def resource_set_list_ids(
:type resource_type: str
:param scope: query resource scope
:type scope: str
:param matchingUri: enable URI matching
:type matchingUri: bool
:param first: index of first matching resource to return
:type first: int
:param maximum: maximum number of resources to return (-1 for all)
Expand All @@ -250,6 +253,8 @@ def resource_set_list_ids(
query["type"] = resource_type
if scope:
query["scope"] = scope
if matchingUri:
query["matchingUri"] = "true"
if first > 0:
query["first"] = first
if maximum >= 0:
Expand Down Expand Up @@ -544,6 +549,7 @@ async def a_resource_set_list_ids(
owner: str = "",
resource_type: str = "",
scope: str = "",
matchingUri: bool = False,
first: int = 0,
maximum: int = -1,
):
Expand All @@ -565,6 +571,8 @@ async def a_resource_set_list_ids(
:param scope: query resource scope
:type scope: str
:param first: index of first matching resource to return
:param matchingUri: enable URI matching
:type matchingUri: bool
:type first: int
:param maximum: maximum number of resources to return (-1 for all)
:type maximum: int
Expand All @@ -584,6 +592,8 @@ async def a_resource_set_list_ids(
query["type"] = resource_type
if scope:
query["scope"] = scope
if matchingUri:
query["matchingUri"] = "true"
if first > 0:
query["first"] = first
if maximum >= 0:
Expand Down
22 changes: 22 additions & 0 deletions tests/test_keycloak_uma.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,23 @@ def test_uma_resource_sets(uma: KeycloakUMA):
"name": "mytest",
"scopes": ["test:read", "test:write"],
"type": "urn:test",
"uris": ["/some_resources/*"],
}
created_resource = uma.resource_set_create(resource_to_create)
assert created_resource
assert created_resource["_id"], created_resource
assert set(resource_to_create).issubset(set(created_resource)), created_resource

# Test getting resource with wildcard
# Without matchingUri query option
resource_set_list_ids = uma.resource_set_list_ids(uri="/some_resources/resource")
assert len(resource_set_list_ids) == 0
# With matchingUri query option
resource_set_list_ids = uma.resource_set_list_ids(
uri="/some_resources/resource", matchingUri=True
)
assert len(resource_set_list_ids) == 1

# Test create the same resource set
with pytest.raises(KeycloakPostError) as err:
uma.resource_set_create(resource_to_create)
Expand Down Expand Up @@ -382,12 +393,23 @@ async def test_a_uma_resource_sets(uma: KeycloakUMA):
"name": "mytest",
"scopes": ["test:read", "test:write"],
"type": "urn:test",
"uris": ["/some_resources/*"],
}
created_resource = await uma.a_resource_set_create(resource_to_create)
assert created_resource
assert created_resource["_id"], created_resource
assert set(resource_to_create).issubset(set(created_resource)), created_resource

# Test getting resource with wildcard
# Without matchingUri query option
resource_set_list_ids = await uma.a_resource_set_list_ids(uri="/some_resources/resource")
assert len(resource_set_list_ids) == 0
# With matchingUri query option
resource_set_list_ids = await uma.a_resource_set_list_ids(
uri="/some_resources/resource", matchingUri=True
)
assert len(resource_set_list_ids) == 1

# Test create the same resource set
with pytest.raises(KeycloakPostError) as err:
await uma.a_resource_set_create(resource_to_create)
Expand Down