From 2ae9122f7535c2ec935a5472117f51f3fb807871 Mon Sep 17 00:00:00 2001 From: Taras Yatsurak Date: Fri, 13 Sep 2024 14:44:27 +0300 Subject: [PATCH 1/2] feat: add matchingUri support for listing resources with wildcards --- src/keycloak/keycloak_uma.py | 10 ++++++++++ tests/test_keycloak_uma.py | 21 +++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/src/keycloak/keycloak_uma.py b/src/keycloak/keycloak_uma.py index d2d8bfa2..c64193f5 100644 --- a/src/keycloak/keycloak_uma.py +++ b/src/keycloak/keycloak_uma.py @@ -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, ): @@ -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) @@ -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: @@ -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, ): @@ -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 @@ -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: diff --git a/tests/test_keycloak_uma.py b/tests/test_keycloak_uma.py index aabc0676..ca240b6b 100644 --- a/tests/test_keycloak_uma.py +++ b/tests/test_keycloak_uma.py @@ -96,12 +96,22 @@ 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) @@ -382,12 +392,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) From 0f56ad30111e223dde639943f6adb38b420f3b8c Mon Sep 17 00:00:00 2001 From: Taras Yatsurak Date: Fri, 13 Sep 2024 14:52:37 +0300 Subject: [PATCH 2/2] fix: change formatting --- tests/test_keycloak_uma.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_keycloak_uma.py b/tests/test_keycloak_uma.py index ca240b6b..09f7aa8b 100644 --- a/tests/test_keycloak_uma.py +++ b/tests/test_keycloak_uma.py @@ -109,7 +109,8 @@ def test_uma_resource_sets(uma: KeycloakUMA): 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) + uri="/some_resources/resource", matchingUri=True + ) assert len(resource_set_list_ids) == 1 # Test create the same resource set