Skip to content

Commit 5253e2f

Browse files
authored
Merge pull request #1 from marcospereirampj/master
merge latest master
2 parents 294df15 + 4315d90 commit 5253e2f

File tree

7 files changed

+73
-5
lines changed

7 files changed

+73
-5
lines changed

MANIFEST.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include LICENSE

docs/source/conf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@
6060
# built documents.
6161
#
6262
# The short X.Y version.
63-
version = '0.19.0'
63+
version = '0.20.0'
6464
# The full version, including alpha/beta/rc tags.
65-
release = '0.19.0'
65+
release = '0.20.0'
6666

6767
# The language for content autogenerated by Sphinx. Refer to documentation
6868
# for a list of supported languages.

docs/source/index.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,14 @@ Main methods::
158158
# realm_name="example_realm",
159159
# verify=True,
160160
# custom_headers={'CustomHeader': 'value'})
161+
#
162+
# You can also authenticate with client_id and client_secret
163+
#keycloak_admin = KeycloakAdmin(server_url="http://localhost:8080/auth/",
164+
# client_id="example_client",
165+
# client_secret_key="secret",
166+
# realm_name="example_realm",
167+
# verify=True,
168+
# custom_headers={'CustomHeader': 'value'})
161169

162170
# Add user
163171
new_user = keycloak_admin.create_user({"email": "[email protected]",

keycloak/connection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ def raw_put(self, path, data, **kwargs):
199199
raise KeycloakConnectionError(
200200
"Can't connect to server (%s)" % e)
201201

202-
def raw_delete(self, path, data, **kwargs):
202+
def raw_delete(self, path, data={}, **kwargs):
203203
""" Submit delete request to the path.
204204
205205
:arg

keycloak/keycloak_admin.py

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
from builtins import isinstance
2929
from typing import List, Iterable
3030

31+
from keycloak.urls_patterns import URL_ADMIN_GROUPS_REALM_ROLES, \
32+
URL_ADMIN_GET_GROUPS_REALM_ROLES
3133
from .connection import ConnectionManager
3234
from .exceptions import raise_error_from_response, KeycloakGetError
3335
from .keycloak_openid import KeycloakOpenID
@@ -60,7 +62,7 @@ class KeycloakAdmin:
6062
_custom_headers = None
6163
_user_realm_name = None
6264

63-
def __init__(self, server_url, username, password, realm_name='master', client_id='admin-cli', verify=True,
65+
def __init__(self, server_url, username=None, password=None, realm_name='master', client_id='admin-cli', verify=True,
6466
client_secret_key=None, custom_headers=None, user_realm_name=None, auto_refresh_token=None):
6567
"""
6668
@@ -936,6 +938,47 @@ def assign_realm_roles(self, user_id, client_id, roles):
936938
data=json.dumps(payload))
937939
return raise_error_from_response(data_raw, KeycloakGetError, expected_code=204)
938940

941+
def assign_group_realm_roles(self, group_id, roles):
942+
"""
943+
Assign realm roles to a group
944+
945+
:param group_id: id of groupp
946+
:param roles: roles list or role (use GroupRoleRepresentation)
947+
:return Keycloak server response
948+
"""
949+
950+
payload = roles if isinstance(roles, list) else [roles]
951+
params_path = {"realm-name": self.realm_name, "id": group_id}
952+
data_raw = self.raw_post(URL_ADMIN_GROUPS_REALM_ROLES.format(**params_path),
953+
data=json.dumps(payload))
954+
return raise_error_from_response(data_raw, KeycloakGetError, expected_code=204)
955+
956+
def delete_group_realm_roles(self, group_id, roles):
957+
"""
958+
Delete realm roles of a group
959+
960+
:param group_id: id of group
961+
:param roles: roles list or role (use GroupRoleRepresentation)
962+
:return Keycloak server response
963+
"""
964+
965+
payload = roles if isinstance(roles, list) else [roles]
966+
params_path = {"realm-name": self.realm_name, "id": group_id}
967+
data_raw = self.raw_delete(URL_ADMIN_GROUPS_REALM_ROLES.format(**params_path),
968+
data=json.dumps(payload))
969+
return raise_error_from_response(data_raw, KeycloakGetError, expected_code=204)
970+
971+
def get_group_realm_roles(self, group_id):
972+
"""
973+
Get all realm roles for a group.
974+
975+
:param user_id: id of the group
976+
:return: Keycloak server response (array RoleRepresentation)
977+
"""
978+
params_path = {"realm-name": self.realm_name, "id": group_id}
979+
data_raw = self.raw_get(URL_ADMIN_GET_GROUPS_REALM_ROLES.format(**params_path))
980+
return raise_error_from_response(data_raw, KeycloakGetError)
981+
939982
def get_client_roles_of_user(self, user_id, client_id):
940983
"""
941984
Get all client roles for a user.
@@ -1103,6 +1146,20 @@ def add_mapper_to_client_scope(self, client_scope_id, payload):
11031146

11041147
return raise_error_from_response(data_raw, KeycloakGetError, expected_code=201)
11051148

1149+
def generate_client_secrets(self, client_id):
1150+
"""
1151+
1152+
Generate a new secret for the client
1153+
https://www.keycloak.org/docs-api/8.0/rest-api/index.html#_regeneratesecret
1154+
1155+
:param client_id: id of client (not client-id)
1156+
:return: Keycloak server response (ClientRepresentation)
1157+
"""
1158+
1159+
params_path = {"realm-name": self.realm_name, "id": client_id}
1160+
data_raw = self.raw_post(URL_ADMIN_CLIENT_SECRETS.format(**params_path), data=None)
1161+
return raise_error_from_response(data_raw, KeycloakGetError)
1162+
11061163
def get_client_secrets(self, client_id):
11071164
"""
11081165

keycloak/urls_patterns.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
URL_ADMIN_GET_SESSIONS = "admin/realms/{realm-name}/users/{id}/sessions"
4444
URL_ADMIN_USER_CLIENT_ROLES = "admin/realms/{realm-name}/users/{id}/role-mappings/clients/{client-id}"
4545
URL_ADMIN_USER_REALM_ROLES = "admin/realms/{realm-name}/users/{id}/role-mappings/realm"
46+
URL_ADMIN_GROUPS_REALM_ROLES = "admin/realms/{realm-name}/groups/{id}/role-mappings/realm"
47+
URL_ADMIN_GET_GROUPS_REALM_ROLES = "admin/realms/{realm-name}/groups/{id}/role-mappings"
4648
URL_ADMIN_USER_CLIENT_ROLES_AVAILABLE = "admin/realms/{realm-name}/users/{id}/role-mappings/clients/{client-id}/available"
4749
URL_ADMIN_USER_CLIENT_ROLES_COMPOSITE = "admin/realms/{realm-name}/users/{id}/role-mappings/clients/{client-id}/composite"
4850
URL_ADMIN_USER_GROUP = "admin/realms/{realm-name}/users/{id}/groups/{group-id}"

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
setup(
99
name='python-keycloak',
10-
version='0.19.0',
10+
version='0.20.0',
1111
url='https://github.com/marcospereirampj/python-keycloak',
1212
license='The MIT License',
1313
author='Marcos Pereira',

0 commit comments

Comments
 (0)