Skip to content

Commit 4753b2e

Browse files
Create methods to retrieve all client scopes, a single client scope, and to add a mapper to a client scope
1 parent 3980ec8 commit 4753b2e

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

keycloak/keycloak_admin.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
URL_ADMIN_USER_GROUPS, URL_ADMIN_CLIENTS, URL_ADMIN_FLOWS_EXECUTIONS, URL_ADMIN_GROUPS, URL_ADMIN_USER_CLIENT_ROLES, \
3737
URL_ADMIN_REALM_IMPORT, URL_ADMIN_USERS_COUNT, URL_ADMIN_FLOWS, URL_ADMIN_GROUP, URL_ADMIN_CLIENT_AUTHZ_SETTINGS, \
3838
URL_ADMIN_GROUP_MEMBERS, URL_ADMIN_USER_STORAGE, URL_ADMIN_GROUP_PERMISSIONS, URL_ADMIN_IDPS, \
39-
URL_ADMIN_USER_CLIENT_ROLES_AVAILABLE, URL_ADMIN_USERS
39+
URL_ADMIN_USER_CLIENT_ROLES_AVAILABLE, URL_ADMIN_USERS, URL_ADMIN_CLIENT_SCOPES, \
40+
URL_ADMIN_CLIENT_SCOPES_ADD_MAPPER, URL_ADMIN_CLIENT_SCOPE
4041

4142

4243
class KeycloakAdmin:
@@ -865,3 +866,43 @@ def sync_users(self, storage_id, action):
865866
data_raw = self.connection.raw_post(URL_ADMIN_USER_STORAGE.format(**params_path),
866867
data=json.dumps(data), **params_query)
867868
return raise_error_from_response(data_raw, KeycloakGetError)
869+
870+
def get_client_scopes(self):
871+
"""
872+
Get representation of the client scopes for the realm where we are connected to
873+
https://www.keycloak.org/docs-api/4.5/rest-api/index.html#_getclientscopes
874+
875+
:return: Keycloak server response Array of (ClientScopeRepresentation)
876+
"""
877+
878+
params_path = {"realm-name": self.realm_name}
879+
data_raw = self.connection.raw_get(URL_ADMIN_CLIENT_SCOPES.format(**params_path))
880+
return raise_error_from_response(data_raw, KeycloakGetError)
881+
882+
def get_client_scope(self, client_scope_id):
883+
"""
884+
Get representation of the client scopes for the realm where we are connected to
885+
https://www.keycloak.org/docs-api/4.5/rest-api/index.html#_getclientscopes
886+
887+
:return: Keycloak server response (ClientScopeRepresentation)
888+
"""
889+
890+
params_path = {"realm-name": self.realm_name, "scope-id": client_scope_id}
891+
data_raw = self.connection.raw_get(URL_ADMIN_CLIENT_SCOPE.format(**params_path))
892+
return raise_error_from_response(data_raw, KeycloakGetError)
893+
894+
895+
def add_mapper_to_client_scope(self, client_scope_id, payload):
896+
"""
897+
Add a mapper to a client scope
898+
https://www.keycloak.org/docs-api/4.5/rest-api/index.html#_create_mapper
899+
900+
:param payload: ProtocolMapperRepresentation
901+
:return: Keycloak server Response
902+
"""
903+
904+
params_path = {"realm-name": self.realm_name, "scope-id": client_scope_id}
905+
906+
data_raw = self.connection.raw_post(URL_ADMIN_CLIENT_SCOPES_ADD_MAPPER.format(**params_path), data=json.dumps(payload))
907+
908+
return raise_error_from_response(data_raw, KeycloakGetError, expected_code=201)

keycloak/urls_patterns.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@
6464
URL_ADMIN_CLIENT_AUTHZ_RESOURCES = "admin/realms/{realm-name}/clients/{id}/authz/resource-server/resource"
6565
URL_ADMIN_CLIENT_CERTS = "admin/realms/{realm-name}/clients/{id}/certificates/{attr}"
6666

67+
URL_ADMIN_CLIENT_SCOPES = "admin/realms/{realm-name}/client-scopes"
68+
URL_ADMIN_CLIENT_SCOPE = URL_ADMIN_CLIENT_SCOPES + "/{scope-id}"
69+
URL_ADMIN_CLIENT_SCOPES_ADD_MAPPER = URL_ADMIN_CLIENT_SCOPE + "/protocol-mappers/models"
70+
6771
URL_ADMIN_REALM_ROLES = "admin/realms/{realm-name}/roles"
6872
URL_ADMIN_REALM_IMPORT = "admin/realms"
6973
URL_ADMIN_IDPS = "admin/realms/{realm-name}/identity-provider/instances"

0 commit comments

Comments
 (0)