-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathclientroles.py
More file actions
116 lines (93 loc) · 3.5 KB
/
clientroles.py
File metadata and controls
116 lines (93 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import json
from collections import OrderedDict
from keycloak.admin import KeycloakAdminBase
ROLE_KWARGS = [
'description',
'id',
'client_role',
'composite',
'composites',
'container_id',
'scope_param_required'
]
__all__ = ('to_camel_case', 'ClientRole', 'ClientRoles',)
def to_camel_case(snake_cased_str):
components = snake_cased_str.split('_')
# We capitalize the first letter of each component except the first one
# with the 'title' method and join them together.
return components[0] + ''.join(map(str.capitalize, components[1:]))
class ClientRoles(KeycloakAdminBase):
_client_id = None
_realm_name = None
_paths = {
'collection': '/admin/realms/{realm}/clients/{id}/roles'
}
def __init__(self, realm_name, client_id, *args, **kwargs):
self._client_id = client_id
self._realm_name = realm_name
super(ClientRoles, self).__init__(*args, **kwargs)
def by_name(self, role_name):
return ClientRole(realm_name=self._realm_name,
client_id=self._client_id,
role_name=role_name, client=self._client)
def create(self, name, **kwargs):
"""
Create new role
http://www.keycloak.org/docs-api/3.4/rest-api/index.html
#_roles_resource
:param str name: Name for the role
:param str description: (optional)
:param str id: (optional)
:param bool client_role: (optional)
:param bool composite: (optional)
:param object composites: (optional)
:param str container_id: (optional)
:param bool scope_param_required: (optional)
"""
payload = OrderedDict(name=name)
for key in ROLE_KWARGS:
if key in kwargs:
payload[to_camel_case(key)] = kwargs[key]
return self._client.post(
url=self._client.get_full_url(
self.get_path('collection',
realm=self._realm_name,
id=self._client_id)
),
data=json.dumps(payload, sort_keys=True)
)
class ClientRole(KeycloakAdminBase):
_paths = {
'single': '/admin/realms/{realm}/clients/{id}/roles/{role_name}'
}
def __init__(self, realm_name, client_id, role_name, *args, **kwargs):
self._client_id = client_id
self._realm_name = realm_name
self._role_name = role_name
super(ClientRole, self).__init__(*args, **kwargs)
def update(self, name, **kwargs):
"""
Update existing role.
http://www.keycloak.org/docs-api/3.4/rest-api/index.html#_roles_resource
:param str name: Name for the role
:param str description: (optional)
:param str id: (optional)
:param bool client_role: (optional)
:param bool composite: (optional)
:param object composites: (optional)
:param str container_id: (optional)
:param bool scope_param_required: (optional)
"""
payload = OrderedDict(name=name)
for key in ROLE_KWARGS:
if key in kwargs:
payload[to_camel_case(key)] = kwargs[key]
return self._client.put(
url=self._client.get_full_url(
self.get_path('single',
realm=self._realm_name,
id=self._client_id,
role_name=self._role_name)
),
data=json.dumps(payload, sort_keys=True)
)