Skip to content

Commit

Permalink
feat: Add endpoints to get/update management permissions on IdP, and
Browse files Browse the repository at this point in the history
delete mapper to IdP
  • Loading branch information
Kevin-Bannier authored and Kévin BANNIER committed Oct 25, 2023
1 parent 5957607 commit 7863467
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
70 changes: 69 additions & 1 deletion src/keycloak/keycloak_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import copy
import json
from builtins import isinstance
from typing import Optional
from typing import Any, Dict, Optional

import deprecation
from requests_toolbelt import MultipartEncoder
Expand Down Expand Up @@ -739,6 +739,23 @@ def update_mapper_in_idp(self, idp_alias, mapper_id, payload):

return raise_error_from_response(data_raw, KeycloakPutError, expected_codes=[204])

def delete_mapper_to_idp(self, idp_alias: str, mapper_id: str) -> Dict[str, Any]:
"""Delete an IDP mapper.
IdentityProviderRepresentation
https://www.keycloak.org/docs-api/22.0.5/rest-api/index.html#_identityprovidermapperrepresentation
:param: idp_alias: alias for Idp to add mapper in
:param: payload: IdentityProviderMapperRepresentation
"""
params_path = {
"realm-name": self.connection.realm_name, # type:ignore
"idp-alias": idp_alias,
"mapper-id": mapper_id,
}
data_raw = self.raw_delete(urls_patterns.URL_ADMIN_IDP_MAPPER_UPDATE.format(**params_path))
return raise_error_from_response(data_raw, KeycloakDeleteError, expected_codes=[204])

def get_idp_mappers(self, idp_alias):
"""Get IDP mappers.
Expand Down Expand Up @@ -785,6 +802,57 @@ def delete_idp(self, idp_alias):
data_raw = self.connection.raw_delete(urls_patterns.URL_ADMIN_IDP.format(**params_path))
return raise_error_from_response(data_raw, KeycloakDeleteError, expected_codes=[204])

def get_idp_management_permissions(self, idp_alias: str) -> Dict[str, Any]:
"""
Get management permissions for a client.
ManagementPermissionReference
https://www.keycloak.org/docs-api/22.0.5/rest-api/index.html#_managementpermissionreference
:param: idp_alias: idp alias name
:type idp_alias: str
:return: Keycloak server response
"""
params_path = {
"realm-name": self.connection.realm_name, # type:ignore
"alias": idp_alias,
}
data_raw = self.raw_get(
urls_patterns.URL_ADMIN_IDP_MANAGEMENT_PERMISSIONS.format(**params_path)
)
return raise_error_from_response(data_raw, KeycloakGetError, expected_codes=[200])

def update_idp_management_permissions(
self, idp_alias: str, payload: Dict[str, Any]
) -> Dict[str, Any]:
"""
Update management permissions for a client.
ManagementPermissionReference
https://www.keycloak.org/docs-api/22.0.5/rest-api/index.html#_managementpermissionreference
:param: idp_alias: idp alias name
:type idp_alias: str
:param payload: ManagementPermissionReference
:return: Keycloak server response
Payload example::
payload={
"enabled": true
}
"""
params_path = {
"realm-name": self.connection.realm_name, # type:ignore
"alias": idp_alias,
}
data_raw = self.raw_put(
urls_patterns.URL_ADMIN_IDP_MANAGEMENT_PERMISSIONS.format(**params_path),
data=json.dumps(payload),
)
return raise_error_from_response(data_raw, KeycloakPutError, expected_codes=[200])

def create_user(self, payload, exist_ok=False):
"""Create a new user.
Expand Down
1 change: 1 addition & 0 deletions src/keycloak/urls_patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@
URL_ADMIN_IDP_MAPPERS = "admin/realms/{realm-name}/identity-provider/instances/{idp-alias}/mappers"
URL_ADMIN_IDP_MAPPER_UPDATE = URL_ADMIN_IDP_MAPPERS + "/{mapper-id}"
URL_ADMIN_IDP = "admin/realms/{realm-name}/identity-provider/instances/{alias}"
URL_ADMIN_IDP_MANAGEMENT_PERMISSIONS = URL_ADMIN_IDP + "/management/permissions"
URL_ADMIN_REALM_ROLES_ROLE_BY_NAME = "admin/realms/{realm-name}/roles/{role-name}"
URL_ADMIN_REALM_ROLES_COMPOSITE_REALM_ROLE = (
"admin/realms/{realm-name}/roles/{role-name}/composites"
Expand Down

0 comments on commit 7863467

Please sign in to comment.