From 7f0ed66803808c58fe2a21e37a6f498e281c4cd4 Mon Sep 17 00:00:00 2001 From: ggallard Date: Wed, 19 Aug 2020 18:31:04 -0300 Subject: [PATCH 1/3] Added create_client_scope --- keycloak/keycloak_admin.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/keycloak/keycloak_admin.py b/keycloak/keycloak_admin.py index ae443b6..7603a88 100644 --- a/keycloak/keycloak_admin.py +++ b/keycloak/keycloak_admin.py @@ -1252,6 +1252,22 @@ class KeycloakAdmin: data_raw = self.raw_get(URL_ADMIN_CLIENT_SCOPE.format(**params_path)) return raise_error_from_response(data_raw, KeycloakGetError) + def create_client_scope(self, payload, skip_exists=False): + """ + Create a client scope + + ClientScopeRepresentation: https://www.keycloak.org/docs-api/8.0/rest-api/index.html#_getclientscopes + + :param payload: ClientScopeRepresentation + :param skip_exists: If true then do not raise an error if client scope already exists + :return: Keycloak server response (ClientScopeRepresentation) + """ + + params_path = {"realm-name": self.realm_name} + data_raw = self.raw_post(URL_ADMIN_CLIENT_SCOPES.format(**params_path), + data=json.dumps(payload)) + return raise_error_from_response(data_raw, KeycloakGetError, expected_code=201, skip_exists=skip_exists) + def add_mapper_to_client_scope(self, client_scope_id, payload): """ Add a mapper to a client scope From a02195cfd696322de836b45497cfe3f790c04979 Mon Sep 17 00:00:00 2001 From: ggallard Date: Thu, 20 Aug 2020 17:17:49 -0300 Subject: [PATCH 2/3] fixed for updated parameter on raise_error_from_response() --- keycloak/keycloak_admin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/keycloak/keycloak_admin.py b/keycloak/keycloak_admin.py index 7603a88..d265a54 100644 --- a/keycloak/keycloak_admin.py +++ b/keycloak/keycloak_admin.py @@ -1266,7 +1266,7 @@ class KeycloakAdmin: params_path = {"realm-name": self.realm_name} data_raw = self.raw_post(URL_ADMIN_CLIENT_SCOPES.format(**params_path), data=json.dumps(payload)) - return raise_error_from_response(data_raw, KeycloakGetError, expected_code=201, skip_exists=skip_exists) + return raise_error_from_response(data_raw, KeycloakGetError, expected_codes=[201], skip_exists=skip_exists) def add_mapper_to_client_scope(self, client_scope_id, payload): """ From f7a358b034bc73f3966dce98cd25daddc05ea1d5 Mon Sep 17 00:00:00 2001 From: ggallard Date: Thu, 20 Aug 2020 18:38:20 -0300 Subject: [PATCH 3/3] added get_client_service_account_user --- keycloak/keycloak_admin.py | 15 ++++++++++++++- keycloak/urls_patterns.py | 1 + 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/keycloak/keycloak_admin.py b/keycloak/keycloak_admin.py index d265a54..82d8323 100644 --- a/keycloak/keycloak_admin.py +++ b/keycloak/keycloak_admin.py @@ -43,7 +43,7 @@ from .urls_patterns import URL_ADMIN_SERVER_INFO, URL_ADMIN_CLIENT_AUTHZ_RESOURC URL_ADMIN_USER_CLIENT_ROLES_AVAILABLE, URL_ADMIN_USERS, URL_ADMIN_CLIENT_SCOPES, \ URL_ADMIN_CLIENT_SCOPES_ADD_MAPPER, URL_ADMIN_CLIENT_SCOPE, URL_ADMIN_CLIENT_SECRETS, \ URL_ADMIN_USER_REALM_ROLES, URL_ADMIN_REALM, URL_ADMIN_COMPONENTS, URL_ADMIN_COMPONENT, URL_ADMIN_KEYS, \ - URL_ADMIN_USER_FEDERATED_IDENTITY, URL_ADMIN_USER_FEDERATED_IDENTITIES + URL_ADMIN_USER_FEDERATED_IDENTITY, URL_ADMIN_USER_FEDERATED_IDENTITIES, URL_ADMIN_CLIENT_SERVICE_ACCOUNT_USER class KeycloakAdmin: @@ -802,6 +802,19 @@ class KeycloakAdmin: data_raw = self.raw_get(URL_ADMIN_CLIENT_AUTHZ_RESOURCES.format(**params_path)) return data_raw + def get_client_service_account_user(self, client_id): + """ + Get service account user from client. + + :param client_id: id in ClientRepresentation + https://www.keycloak.org/docs-api/8.0/rest-api/index.html#_clientrepresentation + :return: UserRepresentation + """ + + params_path = {"realm-name": self.realm_name, "id": client_id} + data_raw = self.raw_get(URL_ADMIN_CLIENT_SERVICE_ACCOUNT_USER.format(**params_path)) + return raise_error_from_response(data_raw, KeycloakGetError) + def create_client(self, payload, skip_exists=False): """ Create a client diff --git a/keycloak/urls_patterns.py b/keycloak/urls_patterns.py index 3e28de8..3369d40 100644 --- a/keycloak/urls_patterns.py +++ b/keycloak/urls_patterns.py @@ -68,6 +68,7 @@ URL_ADMIN_CLIENT_ROLES = URL_ADMIN_CLIENT + "/roles" URL_ADMIN_CLIENT_ROLE = URL_ADMIN_CLIENT + "/roles/{role-name}" URL_ADMIN_CLIENT_AUTHZ_SETTINGS = URL_ADMIN_CLIENT + "/authz/resource-server/settings" URL_ADMIN_CLIENT_AUTHZ_RESOURCES = URL_ADMIN_CLIENT + "/authz/resource-server/resource" +URL_ADMIN_CLIENT_SERVICE_ACCOUNT_USER = URL_ADMIN_CLIENT + "/service-account-user" URL_ADMIN_CLIENT_CERTS = URL_ADMIN_CLIENT + "/certificates/{attr}" URL_ADMIN_CLIENT_SCOPES = "admin/realms/{realm-name}/client-scopes"