Skip to content

Commit

Permalink
Merge pull request #1 from marcospereirampj/master
Browse files Browse the repository at this point in the history
merge latest master
  • Loading branch information
chmller authored Jun 4, 2020
2 parents 294df15 + 4315d90 commit 5253e2f
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 5 deletions.
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include LICENSE
4 changes: 2 additions & 2 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@
# built documents.
#
# The short X.Y version.
version = '0.19.0'
version = '0.20.0'
# The full version, including alpha/beta/rc tags.
release = '0.19.0'
release = '0.20.0'

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
Expand Down
8 changes: 8 additions & 0 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,14 @@ Main methods::
# realm_name="example_realm",
# verify=True,
# custom_headers={'CustomHeader': 'value'})
#
# You can also authenticate with client_id and client_secret
#keycloak_admin = KeycloakAdmin(server_url="http://localhost:8080/auth/",
# client_id="example_client",
# client_secret_key="secret",
# realm_name="example_realm",
# verify=True,
# custom_headers={'CustomHeader': 'value'})

# Add user
new_user = keycloak_admin.create_user({"email": "[email protected]",
Expand Down
2 changes: 1 addition & 1 deletion keycloak/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ def raw_put(self, path, data, **kwargs):
raise KeycloakConnectionError(
"Can't connect to server (%s)" % e)

def raw_delete(self, path, data, **kwargs):
def raw_delete(self, path, data={}, **kwargs):
""" Submit delete request to the path.
:arg
Expand Down
59 changes: 58 additions & 1 deletion keycloak/keycloak_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
from builtins import isinstance
from typing import List, Iterable

from keycloak.urls_patterns import URL_ADMIN_GROUPS_REALM_ROLES, \
URL_ADMIN_GET_GROUPS_REALM_ROLES
from .connection import ConnectionManager
from .exceptions import raise_error_from_response, KeycloakGetError
from .keycloak_openid import KeycloakOpenID
Expand Down Expand Up @@ -60,7 +62,7 @@ class KeycloakAdmin:
_custom_headers = None
_user_realm_name = None

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

def assign_group_realm_roles(self, group_id, roles):
"""
Assign realm roles to a group
:param group_id: id of groupp
:param roles: roles list or role (use GroupRoleRepresentation)
:return Keycloak server response
"""

payload = roles if isinstance(roles, list) else [roles]
params_path = {"realm-name": self.realm_name, "id": group_id}
data_raw = self.raw_post(URL_ADMIN_GROUPS_REALM_ROLES.format(**params_path),
data=json.dumps(payload))
return raise_error_from_response(data_raw, KeycloakGetError, expected_code=204)

def delete_group_realm_roles(self, group_id, roles):
"""
Delete realm roles of a group
:param group_id: id of group
:param roles: roles list or role (use GroupRoleRepresentation)
:return Keycloak server response
"""

payload = roles if isinstance(roles, list) else [roles]
params_path = {"realm-name": self.realm_name, "id": group_id}
data_raw = self.raw_delete(URL_ADMIN_GROUPS_REALM_ROLES.format(**params_path),
data=json.dumps(payload))
return raise_error_from_response(data_raw, KeycloakGetError, expected_code=204)

def get_group_realm_roles(self, group_id):
"""
Get all realm roles for a group.
:param user_id: id of the group
:return: Keycloak server response (array RoleRepresentation)
"""
params_path = {"realm-name": self.realm_name, "id": group_id}
data_raw = self.raw_get(URL_ADMIN_GET_GROUPS_REALM_ROLES.format(**params_path))
return raise_error_from_response(data_raw, KeycloakGetError)

def get_client_roles_of_user(self, user_id, client_id):
"""
Get all client roles for a user.
Expand Down Expand Up @@ -1103,6 +1146,20 @@ def add_mapper_to_client_scope(self, client_scope_id, payload):

return raise_error_from_response(data_raw, KeycloakGetError, expected_code=201)

def generate_client_secrets(self, client_id):
"""
Generate a new secret for the client
https://www.keycloak.org/docs-api/8.0/rest-api/index.html#_regeneratesecret
:param client_id: id of client (not client-id)
:return: Keycloak server response (ClientRepresentation)
"""

params_path = {"realm-name": self.realm_name, "id": client_id}
data_raw = self.raw_post(URL_ADMIN_CLIENT_SECRETS.format(**params_path), data=None)
return raise_error_from_response(data_raw, KeycloakGetError)

def get_client_secrets(self, client_id):
"""
Expand Down
2 changes: 2 additions & 0 deletions keycloak/urls_patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
URL_ADMIN_GET_SESSIONS = "admin/realms/{realm-name}/users/{id}/sessions"
URL_ADMIN_USER_CLIENT_ROLES = "admin/realms/{realm-name}/users/{id}/role-mappings/clients/{client-id}"
URL_ADMIN_USER_REALM_ROLES = "admin/realms/{realm-name}/users/{id}/role-mappings/realm"
URL_ADMIN_GROUPS_REALM_ROLES = "admin/realms/{realm-name}/groups/{id}/role-mappings/realm"
URL_ADMIN_GET_GROUPS_REALM_ROLES = "admin/realms/{realm-name}/groups/{id}/role-mappings"
URL_ADMIN_USER_CLIENT_ROLES_AVAILABLE = "admin/realms/{realm-name}/users/{id}/role-mappings/clients/{client-id}/available"
URL_ADMIN_USER_CLIENT_ROLES_COMPOSITE = "admin/realms/{realm-name}/users/{id}/role-mappings/clients/{client-id}/composite"
URL_ADMIN_USER_GROUP = "admin/realms/{realm-name}/users/{id}/groups/{group-id}"
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

setup(
name='python-keycloak',
version='0.19.0',
version='0.20.0',
url='https://github.com/marcospereirampj/python-keycloak',
license='The MIT License',
author='Marcos Pereira',
Expand Down

0 comments on commit 5253e2f

Please sign in to comment.