From 4404f06aa648e5e07304a8b86ded583160dd3180 Mon Sep 17 00:00:00 2001 From: Thomas Anderson Date: Thu, 17 Oct 2019 15:52:15 +1100 Subject: [PATCH] Add function to KeycloakAdmin to add a role to a realm --- keycloak/keycloak_admin.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/keycloak/keycloak_admin.py b/keycloak/keycloak_admin.py index 5d57661..c707ba6 100644 --- a/keycloak/keycloak_admin.py +++ b/keycloak/keycloak_admin.py @@ -827,6 +827,21 @@ class KeycloakAdmin: data=json.dumps(payload)) return raise_error_from_response(data_raw, KeycloakGetError, expected_code=204) + def create_realm_role(self, payload, skip_exists=False): + """ + Create a new role for the realm or client + + :param realm: realm name (not id) + :param rep: RoleRepresentation https://www.keycloak.org/docs-api/5.0/rest-api/index.html#_rolerepresentation + :return Keycloak server response + """ + + params_path = {"realm-name": self.realm_name} + data_raw = self.connection.raw_post(URL_ADMIN_REALM_ROLES.format(**params_path), + data=json.dumps(payload)) + return raise_error_from_response(data_raw, KeycloakGetError, expected_code=201, skip_exists=skip_exists) + + def assign_realm_roles(self, user_id, client_id, roles): """ Assign realm roles to a user