From f3de47e1b374a1035d59c1fbcab54af12069982a Mon Sep 17 00:00:00 2001 From: Bruno Bonfils Date: Thu, 2 Jun 2022 11:37:29 +0200 Subject: [PATCH] feat: Add update_mapper_in_idp --- src/keycloak/keycloak_admin.py | 27 ++++++++++++++++++++++++++- src/keycloak/urls_patterns.py | 1 + tests/test_keycloak_admin.py | 21 ++++++++++++++++----- 3 files changed, 43 insertions(+), 6 deletions(-) diff --git a/src/keycloak/keycloak_admin.py b/src/keycloak/keycloak_admin.py index e31102a..57dfb47 100644 --- a/src/keycloak/keycloak_admin.py +++ b/src/keycloak/keycloak_admin.py @@ -413,6 +413,31 @@ class KeycloakAdmin: ) return raise_error_from_response(data_raw, KeycloakPostError, expected_codes=[201]) + def update_mapper_in_idp(self, idp_alias, mapper_id, payload): + """ + Update an IdP mapper + + IdentityProviderMapperRepresentation + https://www.keycloak.org/docs-api/18.0/rest-api/index.html#_update + + :param: idp_alias: alias for Idp to fetch mappers + :param: mapper_id: Mapper Id to update + :param: payload: IdentityProviderMapperRepresentation + :return: Http response + """ + params_path = { + "realm-name": self.realm_name, + "idp-alias": idp_alias, + "mapper-id": mapper_id, + } + + data_raw = self.raw_put( + urls_patterns.URL_ADMIN_IDP_MAPPER_UPDATE.format(**params_path), + data=json.dumps(payload), + ) + + return raise_error_from_response(data_raw, KeycloakPutError, expected_codes=[204]) + def get_idp_mappers(self, idp_alias): """ Returns a list of ID Providers mappers @@ -2208,7 +2233,7 @@ class KeycloakAdmin: """ params_path = {"realm-name": self.realm_name, "scope-id": client_scope_id} data_raw = self.raw_get( - urls_patterns.URL_ADMIN_CLIENT_SCOPES_ADD_MAPPER.format(**params_path), + urls_patterns.URL_ADMIN_CLIENT_SCOPES_ADD_MAPPER.format(**params_path) ) return raise_error_from_response(data_raw, KeycloakGetError, expected_codes=[200]) diff --git a/src/keycloak/urls_patterns.py b/src/keycloak/urls_patterns.py index d836ed4..16f348a 100644 --- a/src/keycloak/urls_patterns.py +++ b/src/keycloak/urls_patterns.py @@ -119,6 +119,7 @@ URL_ADMIN_REALMS = "admin/realms" URL_ADMIN_REALM = "admin/realms/{realm-name}" URL_ADMIN_IDPS = "admin/realms/{realm-name}/identity-provider/instances" 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_REALM_ROLES_ROLE_BY_NAME = "admin/realms/{realm-name}/roles/{role-name}" URL_ADMIN_REALM_ROLES_COMPOSITE_REALM_ROLE = ( diff --git a/tests/test_keycloak_admin.py b/tests/test_keycloak_admin.py index 87c093d..a2cd5d7 100644 --- a/tests/test_keycloak_admin.py +++ b/tests/test_keycloak_admin.py @@ -337,10 +337,23 @@ def test_idps(admin: KeycloakAdmin, realm: str): assert err.match('404: b\'{"error":"HTTP 404 Not Found"}\'') # Test IdP mappers listing - idp_mappers = admin.get_idp_mappers( + idp_mappers = admin.get_idp_mappers(idp_alias="github") + assert len(idp_mappers) == 1 + + # Test IdP mapper update + res = admin.update_mapper_in_idp( idp_alias="github", + mapper_id=idp_mappers[0]["id"], + # For an obscure reason, keycloak expect all fields + payload={ + "id": idp_mappers[0]["id"], + "identityProviderAlias": "github-alias", + "identityProviderMapper": "github-user-attribute-mapper", + "name": "test", + "config": idp_mappers[0]["config"], + }, ) - assert len(idp_mappers) == 1 + assert res == dict(), res # Test delete res = admin.delete_idp(idp_alias="github") @@ -1452,9 +1465,7 @@ def test_client_scopes(admin: KeycloakAdmin, realm: str): assert err.match('404: b\'{"error":"Could not find client scope"}\'') test_mapper["config"]["user.attribute"] = "test" res_update = admin.update_mapper_in_client_scope( - client_scope_id=res, - protocol_mapper_id=test_mapper["id"], - payload=test_mapper, + client_scope_id=res, protocol_mapper_id=test_mapper["id"], payload=test_mapper ) assert res_update == dict() assert (