|
|
@ -726,18 +726,23 @@ class KeycloakAdmin: |
|
|
|
data_raw = self.raw_get(urls_patterns.URL_ADMIN_USER.format(**params_path)) |
|
|
|
return raise_error_from_response(data_raw, KeycloakGetError) |
|
|
|
|
|
|
|
def get_user_groups(self, user_id): |
|
|
|
def get_user_groups(self, user_id, brief_representation=True): |
|
|
|
"""Get user groups. |
|
|
|
|
|
|
|
Returns a list of groups of which the user is a member |
|
|
|
|
|
|
|
:param user_id: User id |
|
|
|
:type user_id: str |
|
|
|
:param brief_representation: whether to omit attributes in the response |
|
|
|
:type brief_representation: bool |
|
|
|
:return: user groups list |
|
|
|
:rtype: list |
|
|
|
""" |
|
|
|
params = {"briefRepresentation": brief_representation} |
|
|
|
params_path = {"realm-name": self.realm_name, "id": user_id} |
|
|
|
data_raw = self.raw_get(urls_patterns.URL_ADMIN_USER_GROUPS.format(**params_path)) |
|
|
|
data_raw = self.raw_get( |
|
|
|
urls_patterns.URL_ADMIN_USER_GROUPS.format(**params_path), **params |
|
|
|
) |
|
|
|
return raise_error_from_response(data_raw, KeycloakGetError) |
|
|
|
|
|
|
|
def update_user(self, user_id, payload): |
|
|
@ -1562,17 +1567,22 @@ class KeycloakAdmin: |
|
|
|
) |
|
|
|
return raise_error_from_response(data_raw, KeycloakGetError, expected_codes=[200]) |
|
|
|
|
|
|
|
def get_realm_roles(self): |
|
|
|
def get_realm_roles(self, brief_representation=True): |
|
|
|
"""Get all roles for the realm or client. |
|
|
|
|
|
|
|
RoleRepresentation |
|
|
|
https://www.keycloak.org/docs-api/18.0/rest-api/index.html#_rolerepresentation |
|
|
|
|
|
|
|
:param brief_representation: whether to omit role attributes in the response |
|
|
|
:type brief_representation: bool |
|
|
|
:return: Keycloak server response (RoleRepresentation) |
|
|
|
:rtype: list |
|
|
|
""" |
|
|
|
params_path = {"realm-name": self.realm_name} |
|
|
|
data_raw = self.raw_get(urls_patterns.URL_ADMIN_REALM_ROLES.format(**params_path)) |
|
|
|
params = {"briefRepresentation": brief_representation} |
|
|
|
data_raw = self.raw_get( |
|
|
|
urls_patterns.URL_ADMIN_REALM_ROLES.format(**params_path), **params |
|
|
|
) |
|
|
|
return raise_error_from_response(data_raw, KeycloakGetError) |
|
|
|
|
|
|
|
def get_realm_role_members(self, role_name, query=None): |
|
|
@ -1592,7 +1602,7 @@ class KeycloakAdmin: |
|
|
|
urls_patterns.URL_ADMIN_REALM_ROLES_MEMBERS.format(**params_path), query |
|
|
|
) |
|
|
|
|
|
|
|
def get_client_roles(self, client_id): |
|
|
|
def get_client_roles(self, client_id, brief_representation=True): |
|
|
|
"""Get all roles for the client. |
|
|
|
|
|
|
|
RoleRepresentation |
|
|
@ -1600,11 +1610,16 @@ class KeycloakAdmin: |
|
|
|
|
|
|
|
:param client_id: id of client (not client-id) |
|
|
|
:type client_id: str |
|
|
|
:param brief_representation: whether to omit role attributes in the response |
|
|
|
:type brief_representation: bool |
|
|
|
:return: Keycloak server response (RoleRepresentation) |
|
|
|
:rtype: list |
|
|
|
""" |
|
|
|
params_path = {"realm-name": self.realm_name, "id": client_id} |
|
|
|
data_raw = self.raw_get(urls_patterns.URL_ADMIN_CLIENT_ROLES.format(**params_path)) |
|
|
|
params = {"briefRepresentation": brief_representation} |
|
|
|
data_raw = self.raw_get( |
|
|
|
urls_patterns.URL_ADMIN_CLIENT_ROLES.format(**params_path), **params |
|
|
|
) |
|
|
|
return raise_error_from_response(data_raw, KeycloakGetError) |
|
|
|
|
|
|
|
def get_client_role(self, client_id, role_name): |
|
|
@ -1617,7 +1632,7 @@ class KeycloakAdmin: |
|
|
|
|
|
|
|
:param client_id: id of client (not client-id) |
|
|
|
:type client_id: str |
|
|
|
:param role_name: role’s name (not id!) |
|
|
|
:param role_name: role's name (not id!) |
|
|
|
:type role_name: str |
|
|
|
:return: role_id |
|
|
|
:rtype: str |
|
|
@ -1915,6 +1930,124 @@ class KeycloakAdmin: |
|
|
|
) |
|
|
|
return raise_error_from_response(data_raw, KeycloakGetError) |
|
|
|
|
|
|
|
def assign_realm_roles_to_client_scope(self, client_id, roles): |
|
|
|
"""Assign realm roles to a client's scope. |
|
|
|
|
|
|
|
:param client_id: id of client (not client-id) |
|
|
|
:type client_id: str |
|
|
|
:param roles: roles list or role (use RoleRepresentation) |
|
|
|
:type roles: list |
|
|
|
:return: Keycloak server response |
|
|
|
:rtype: dict |
|
|
|
""" |
|
|
|
payload = roles if isinstance(roles, list) else [roles] |
|
|
|
params_path = {"realm-name": self.realm_name, "id": client_id} |
|
|
|
data_raw = self.raw_post( |
|
|
|
urls_patterns.URL_ADMIN_CLIENT_SCOPE_MAPPINGS_REALM_ROLES.format(**params_path), |
|
|
|
data=json.dumps(payload), |
|
|
|
) |
|
|
|
return raise_error_from_response(data_raw, KeycloakPostError, expected_codes=[204]) |
|
|
|
|
|
|
|
def delete_realm_roles_of_client_scope(self, client_id, roles): |
|
|
|
"""Delete realm roles of a client's scope. |
|
|
|
|
|
|
|
:param client_id: id of client (not client-id) |
|
|
|
:type client_id: str |
|
|
|
:param roles: roles list or role (use RoleRepresentation) |
|
|
|
:type roles: list |
|
|
|
:return: Keycloak server response |
|
|
|
:rtype: dict |
|
|
|
""" |
|
|
|
payload = roles if isinstance(roles, list) else [roles] |
|
|
|
params_path = {"realm-name": self.realm_name, "id": client_id} |
|
|
|
data_raw = self.raw_delete( |
|
|
|
urls_patterns.URL_ADMIN_CLIENT_SCOPE_MAPPINGS_REALM_ROLES.format(**params_path), |
|
|
|
data=json.dumps(payload), |
|
|
|
) |
|
|
|
return raise_error_from_response(data_raw, KeycloakDeleteError, expected_codes=[204]) |
|
|
|
|
|
|
|
def get_realm_roles_of_client_scope(self, client_id): |
|
|
|
"""Get all realm roles for a client's scope. |
|
|
|
|
|
|
|
:param client_id: id of client (not client-id) |
|
|
|
:type client_id: str |
|
|
|
:return: Keycloak server response (array RoleRepresentation) |
|
|
|
:rtype: dict |
|
|
|
""" |
|
|
|
params_path = {"realm-name": self.realm_name, "id": client_id} |
|
|
|
data_raw = self.raw_get( |
|
|
|
urls_patterns.URL_ADMIN_CLIENT_SCOPE_MAPPINGS_REALM_ROLES.format(**params_path) |
|
|
|
) |
|
|
|
return raise_error_from_response(data_raw, KeycloakGetError) |
|
|
|
|
|
|
|
def assign_client_roles_to_client_scope(self, client_id, client_roles_owner_id, roles): |
|
|
|
"""Assign client roles to a client's scope. |
|
|
|
|
|
|
|
:param client_id: id of client (not client-id) who is assigned the roles |
|
|
|
:type client_id: str |
|
|
|
:param client_roles_owner_id: id of client (not client-id) who has the roles |
|
|
|
:type client_roles_owner_id: str |
|
|
|
:param roles: roles list or role (use RoleRepresentation) |
|
|
|
:type roles: list |
|
|
|
:return: Keycloak server response |
|
|
|
:rtype: dict |
|
|
|
""" |
|
|
|
payload = roles if isinstance(roles, list) else [roles] |
|
|
|
params_path = { |
|
|
|
"realm-name": self.realm_name, |
|
|
|
"id": client_id, |
|
|
|
"client": client_roles_owner_id, |
|
|
|
} |
|
|
|
data_raw = self.raw_post( |
|
|
|
urls_patterns.URL_ADMIN_CLIENT_SCOPE_MAPPINGS_CLIENT_ROLES.format(**params_path), |
|
|
|
data=json.dumps(payload), |
|
|
|
) |
|
|
|
return raise_error_from_response(data_raw, KeycloakPostError, expected_codes=[204]) |
|
|
|
|
|
|
|
def delete_client_roles_of_client_scope(self, client_id, client_roles_owner_id, roles): |
|
|
|
"""Delete client roles of a client's scope. |
|
|
|
|
|
|
|
:param client_id: id of client (not client-id) who is assigned the roles |
|
|
|
:type client_id: str |
|
|
|
:param client_roles_owner_id: id of client (not client-id) who has the roles |
|
|
|
:type client_roles_owner_id: str |
|
|
|
:param roles: roles list or role (use RoleRepresentation) |
|
|
|
:type roles: list |
|
|
|
:return: Keycloak server response |
|
|
|
:rtype: dict |
|
|
|
""" |
|
|
|
payload = roles if isinstance(roles, list) else [roles] |
|
|
|
params_path = { |
|
|
|
"realm-name": self.realm_name, |
|
|
|
"id": client_id, |
|
|
|
"client": client_roles_owner_id, |
|
|
|
} |
|
|
|
data_raw = self.raw_delete( |
|
|
|
urls_patterns.URL_ADMIN_CLIENT_SCOPE_MAPPINGS_CLIENT_ROLES.format(**params_path), |
|
|
|
data=json.dumps(payload), |
|
|
|
) |
|
|
|
return raise_error_from_response(data_raw, KeycloakDeleteError, expected_codes=[204]) |
|
|
|
|
|
|
|
def get_client_roles_of_client_scope(self, client_id, client_roles_owner_id): |
|
|
|
"""Get all client roles for a client's scope. |
|
|
|
|
|
|
|
:param client_id: id of client (not client-id) |
|
|
|
:type client_id: str |
|
|
|
:param client_roles_owner_id: id of client (not client-id) who has the roles |
|
|
|
:type client_roles_owner_id: str |
|
|
|
:return: Keycloak server response (array RoleRepresentation) |
|
|
|
:rtype: dict |
|
|
|
""" |
|
|
|
params_path = { |
|
|
|
"realm-name": self.realm_name, |
|
|
|
"id": client_id, |
|
|
|
"client": client_roles_owner_id, |
|
|
|
} |
|
|
|
data_raw = self.raw_get( |
|
|
|
urls_patterns.URL_ADMIN_CLIENT_SCOPE_MAPPINGS_CLIENT_ROLES.format(**params_path) |
|
|
|
) |
|
|
|
return raise_error_from_response(data_raw, KeycloakGetError) |
|
|
|
|
|
|
|
def assign_realm_roles(self, user_id, roles): |
|
|
|
"""Assign realm roles to a user. |
|
|
|
|
|
|
@ -1977,17 +2110,20 @@ class KeycloakAdmin: |
|
|
|
) |
|
|
|
return raise_error_from_response(data_raw, KeycloakGetError) |
|
|
|
|
|
|
|
def get_composite_realm_roles_of_user(self, user_id): |
|
|
|
def get_composite_realm_roles_of_user(self, user_id, brief_representation=True): |
|
|
|
"""Get all composite (i.e. implicit) realm roles for a user. |
|
|
|
|
|
|
|
:param user_id: id of user |
|
|
|
:type user_id: str |
|
|
|
:param brief_representation: whether to omit role attributes in the response |
|
|
|
:type brief_representation: bool |
|
|
|
:return: Keycloak server response (array RoleRepresentation) |
|
|
|
:rtype: list |
|
|
|
""" |
|
|
|
params_path = {"realm-name": self.realm_name, "id": user_id} |
|
|
|
params = {"briefRepresentation": brief_representation} |
|
|
|
data_raw = self.raw_get( |
|
|
|
urls_patterns.URL_ADMIN_USER_REALM_ROLES_COMPOSITE.format(**params_path) |
|
|
|
urls_patterns.URL_ADMIN_USER_REALM_ROLES_COMPOSITE.format(**params_path), **params |
|
|
|
) |
|
|
|
return raise_error_from_response(data_raw, KeycloakGetError) |
|
|
|
|
|
|
@ -2027,16 +2163,21 @@ class KeycloakAdmin: |
|
|
|
) |
|
|
|
return raise_error_from_response(data_raw, KeycloakDeleteError, expected_codes=[204]) |
|
|
|
|
|
|
|
def get_group_realm_roles(self, group_id): |
|
|
|
def get_group_realm_roles(self, group_id, brief_representation=True): |
|
|
|
"""Get all realm roles for a group. |
|
|
|
|
|
|
|
:param group_id: id of the group |
|
|
|
:type group_id: str |
|
|
|
:param brief_representation: whether to omit role attributes in the response |
|
|
|
:type brief_representation: bool |
|
|
|
:return: Keycloak server response (array RoleRepresentation) |
|
|
|
:rtype: list |
|
|
|
""" |
|
|
|
params_path = {"realm-name": self.realm_name, "id": group_id} |
|
|
|
data_raw = self.raw_get(urls_patterns.URL_ADMIN_GROUPS_REALM_ROLES.format(**params_path)) |
|
|
|
params = {"briefRepresentation": brief_representation} |
|
|
|
data_raw = self.raw_get( |
|
|
|
urls_patterns.URL_ADMIN_GROUPS_REALM_ROLES.format(**params_path), **params |
|
|
|
) |
|
|
|
return raise_error_from_response(data_raw, KeycloakGetError) |
|
|
|
|
|
|
|
def assign_group_client_roles(self, group_id, client_id, roles): |
|
|
@ -2121,21 +2262,26 @@ class KeycloakAdmin: |
|
|
|
urls_patterns.URL_ADMIN_USER_CLIENT_ROLES_AVAILABLE, user_id, client_id |
|
|
|
) |
|
|
|
|
|
|
|
def get_composite_client_roles_of_user(self, user_id, client_id): |
|
|
|
def get_composite_client_roles_of_user(self, user_id, client_id, brief_representation=False): |
|
|
|
"""Get composite client role-mappings for a user. |
|
|
|
|
|
|
|
:param user_id: id of user |
|
|
|
:type user_id: str |
|
|
|
:param client_id: id of client (not client-id) |
|
|
|
:type client_id: str |
|
|
|
:param brief_representation: whether to omit attributes in the response |
|
|
|
:type brief_representation: bool |
|
|
|
:return: Keycloak server response (array RoleRepresentation) |
|
|
|
:rtype: list |
|
|
|
""" |
|
|
|
params = {"briefRepresentation": brief_representation} |
|
|
|
return self._get_client_roles_of_user( |
|
|
|
urls_patterns.URL_ADMIN_USER_CLIENT_ROLES_COMPOSITE, user_id, client_id |
|
|
|
urls_patterns.URL_ADMIN_USER_CLIENT_ROLES_COMPOSITE, user_id, client_id, **params |
|
|
|
) |
|
|
|
|
|
|
|
def _get_client_roles_of_user(self, client_level_role_mapping_url, user_id, client_id): |
|
|
|
def _get_client_roles_of_user( |
|
|
|
self, client_level_role_mapping_url, user_id, client_id, **params |
|
|
|
): |
|
|
|
"""Get client roles of a single user helper. |
|
|
|
|
|
|
|
:param client_level_role_mapping_url: Url for the client role mapping |
|
|
@ -2144,11 +2290,13 @@ class KeycloakAdmin: |
|
|
|
:type user_id: str |
|
|
|
:param client_id: Client id |
|
|
|
:type client_id: str |
|
|
|
:param params: Additional parameters |
|
|
|
:type params: dict |
|
|
|
:returns: Client roles of a user |
|
|
|
:rtype: list |
|
|
|
""" |
|
|
|
params_path = {"realm-name": self.realm_name, "id": user_id, "client-id": client_id} |
|
|
|
data_raw = self.raw_get(client_level_role_mapping_url.format(**params_path)) |
|
|
|
data_raw = self.raw_get(client_level_role_mapping_url.format(**params_path), **params) |
|
|
|
return raise_error_from_response(data_raw, KeycloakGetError) |
|
|
|
|
|
|
|
def delete_client_roles_of_user(self, user_id, client_id, roles): |
|
|
@ -3352,19 +3500,22 @@ class KeycloakAdmin: |
|
|
|
) |
|
|
|
return raise_error_from_response(data_raw, KeycloakPostError, expected_codes=[201]) |
|
|
|
|
|
|
|
def get_composite_client_roles_of_group(self, client_id, group_id): |
|
|
|
def get_composite_client_roles_of_group(self, client_id, group_id, brief_representation=True): |
|
|
|
"""Get the composite client roles of the given group for the given client. |
|
|
|
|
|
|
|
:param client_id: id of the client. |
|
|
|
:type client_id: str |
|
|
|
:param group_id: id of the group. |
|
|
|
:type group_id: str |
|
|
|
:param brief_representation: whether to omit attributes in the response |
|
|
|
:type brief_representation: bool |
|
|
|
:return: the composite client roles of the group (list of RoleRepresentation). |
|
|
|
:rtype: list |
|
|
|
""" |
|
|
|
params_path = {"realm-name": self.realm_name, "id": group_id, "client-id": client_id} |
|
|
|
params = {"briefRepresentation": brief_representation} |
|
|
|
data_raw = self.raw_get( |
|
|
|
urls_patterns.URL_ADMIN_GROUPS_CLIENT_ROLES_COMPOSITE.format(**params_path) |
|
|
|
urls_patterns.URL_ADMIN_GROUPS_CLIENT_ROLES_COMPOSITE.format(**params_path), **params |
|
|
|
) |
|
|
|
return raise_error_from_response(data_raw, KeycloakGetError) |
|
|
|
|
|
|
|