From c767c21177cf4b68a98b5c7fda259772c43c4072 Mon Sep 17 00:00:00 2001 From: Akos Hencz Date: Tue, 30 Jul 2019 17:22:33 +0200 Subject: [PATCH 1/4] Add pagination wrapper --- keycloak/keycloak_admin.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/keycloak/keycloak_admin.py b/keycloak/keycloak_admin.py index 4e2b398..300554f 100644 --- a/keycloak/keycloak_admin.py +++ b/keycloak/keycloak_admin.py @@ -42,6 +42,8 @@ from .urls_patterns import URL_ADMIN_SERVER_INFO, URL_ADMIN_CLIENT_AUTHZ_RESOURC class KeycloakAdmin: + PAGE_SIZE = 100 + def __init__(self, server_url, username, password, realm_name='master', client_id='admin-cli', verify=True, client_secret_key=None): """ @@ -120,6 +122,35 @@ class KeycloakAdmin: def token(self, value): self._token = value + + def __fetch_all(self, url, query=None): + '''Wrapper function to paginate GET requests + + :param url: The url on which the query is executed + :param query: Existing query parameters (optional) + + :return: Combined results of paginated queries + ''' + results = [] + + # initalize query if it was called with None + if not query: + query = {} + page = 0 + query['max'] = self.PAGE_SIZE + + # fetch until we can + while True: + query['first'] = page*self.PAGE_SIZE + partial_results = raise_error_from_response( + self.connection.raw_get(url, **query), + KeycloakGetError) + if not partial_results: + break + results.extend(partial_results) + page += 1 + return results + def import_realm(self, payload): """ Import a new realm from a RealmRepresentation. Realm name must be unique. From 2225a1283d205fc4a9f250f52ee9d1d41f0a1ea1 Mon Sep 17 00:00:00 2001 From: Akos Hencz Date: Tue, 30 Jul 2019 17:23:08 +0200 Subject: [PATCH 2/4] Paginate get_users --- keycloak/keycloak_admin.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/keycloak/keycloak_admin.py b/keycloak/keycloak_admin.py index 300554f..33742dd 100644 --- a/keycloak/keycloak_admin.py +++ b/keycloak/keycloak_admin.py @@ -199,8 +199,7 @@ class KeycloakAdmin: :return: users list """ params_path = {"realm-name": self.realm_name} - data_raw = self.connection.raw_get(URL_ADMIN_USERS.format(**params_path), **query) - return raise_error_from_response(data_raw, KeycloakGetError) + return self.__fetch_all(URL_ADMIN_USERS.format(**params_path), query) def get_idps(self): """ From ffdb7509c25b1e8d2665683242e6a992d9e7f96b Mon Sep 17 00:00:00 2001 From: Akos Hencz Date: Tue, 30 Jul 2019 17:23:26 +0200 Subject: [PATCH 3/4] Paginate get_groups --- keycloak/keycloak_admin.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/keycloak/keycloak_admin.py b/keycloak/keycloak_admin.py index 33742dd..d07b76b 100644 --- a/keycloak/keycloak_admin.py +++ b/keycloak/keycloak_admin.py @@ -419,8 +419,7 @@ class KeycloakAdmin: :return: array GroupRepresentation """ params_path = {"realm-name": self.realm_name} - data_raw = self.connection.raw_get(URL_ADMIN_GROUPS.format(**params_path)) - return raise_error_from_response(data_raw, KeycloakGetError) + return self.__fetch_all(URL_ADMIN_GROUPS.format(**params_path)) def get_group(self, group_id): """ From 147646e56f244234fa32408b9beb7aeeba7fe4a1 Mon Sep 17 00:00:00 2001 From: Akos Hencz Date: Wed, 31 Jul 2019 10:05:17 +0200 Subject: [PATCH 4/4] Paginate get_group_members --- keycloak/keycloak_admin.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/keycloak/keycloak_admin.py b/keycloak/keycloak_admin.py index d07b76b..d9b76d2 100644 --- a/keycloak/keycloak_admin.py +++ b/keycloak/keycloak_admin.py @@ -465,8 +465,7 @@ class KeycloakAdmin: :return: Keycloak server response (UserRepresentation) """ params_path = {"realm-name": self.realm_name, "id": group_id} - data_raw = self.connection.raw_get(URL_ADMIN_GROUP_MEMBERS.format(**params_path), **query) - return raise_error_from_response(data_raw, KeycloakGetError) + return self.__fetch_all(URL_ADMIN_GROUP_MEMBERS.format(**params_path), query) def get_group_by_path(self, path, search_in_subgroups=False): """