You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

693 lines
25 KiB

7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
7 years ago
6 years ago
6 years ago
7 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
6 years ago
6 years ago
7 years ago
7 years ago
7 years ago
6 years ago
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2017 Marcos Pereira <marcospereira.mpj@gmail.com>
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU Lesser General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU Lesser General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Lesser General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. # Unless otherwise stated in the comments, "id", in e.g. user_id, refers to the
  18. # internal Keycloak server ID, usually a uuid string
  19. from keycloak.urls_patterns import URL_ADMIN_CLIENT_ROLE
  20. from .urls_patterns import \
  21. URL_ADMIN_USERS_COUNT, URL_ADMIN_USER, URL_ADMIN_USER_CONSENTS, \
  22. URL_ADMIN_SEND_UPDATE_ACCOUNT, URL_ADMIN_RESET_PASSWORD, URL_ADMIN_SEND_VERIFY_EMAIL, URL_ADMIN_GET_SESSIONS, \
  23. URL_ADMIN_SERVER_INFO, URL_ADMIN_CLIENTS, URL_ADMIN_CLIENT, URL_ADMIN_CLIENT_ROLES, URL_ADMIN_REALM_ROLES, \
  24. URL_ADMIN_GROUP, URL_ADMIN_GROUPS, URL_ADMIN_GROUP_CHILD, URL_ADMIN_USER_GROUP,\
  25. URL_ADMIN_GROUP_PERMISSIONS, URL_ADMIN_USER_CLIENT_ROLES, URL_ADMIN_USER_STORAGE
  26. from .keycloak_openid import KeycloakOpenID
  27. from .exceptions import raise_error_from_response, KeycloakGetError
  28. from .urls_patterns import (
  29. URL_ADMIN_USERS,
  30. )
  31. from .connection import ConnectionManager
  32. import json
  33. class KeycloakAdmin:
  34. def __init__(self, server_url, username, password, realm_name='master', client_id='admin-cli', verify=True):
  35. """
  36. :param server_url: Keycloak server url
  37. :param username: admin username
  38. :param password: admin password
  39. :param realm_name: realm name
  40. :param client_id: client id
  41. :param verify: True if want check connection SSL
  42. """
  43. self._username = username
  44. self._password = password
  45. self._client_id = client_id
  46. self._realm_name = realm_name
  47. # Get token Admin
  48. keycloak_openid = KeycloakOpenID(server_url=server_url, client_id=client_id, realm_name=realm_name,
  49. verify=verify)
  50. self._token = keycloak_openid.token(username, password)
  51. self._connection = ConnectionManager(base_url=server_url,
  52. headers={'Authorization': 'Bearer ' + self.token.get('access_token'),
  53. 'Content-Type': 'application/json'},
  54. timeout=60,
  55. verify=verify)
  56. @property
  57. def realm_name(self):
  58. return self._realm_name
  59. @realm_name.setter
  60. def realm_name(self, value):
  61. self._realm_name = value
  62. @property
  63. def connection(self):
  64. return self._connection
  65. @connection.setter
  66. def connection(self, value):
  67. self._connection = value
  68. @property
  69. def client_id(self):
  70. return self._client_id
  71. @client_id.setter
  72. def client_id(self, value):
  73. self._client_id = value
  74. @property
  75. def username(self):
  76. return self._username
  77. @username.setter
  78. def username(self, value):
  79. self._username = value
  80. @property
  81. def password(self):
  82. return self._password
  83. @password.setter
  84. def password(self, value):
  85. self._password = value
  86. @property
  87. def token(self):
  88. return self._token
  89. @token.setter
  90. def token(self, value):
  91. self._token = value
  92. def get_users(self, query=None):
  93. """
  94. Get users Returns a list of users, filtered according to query parameters
  95. :return: users list
  96. """
  97. params_path = {"realm-name": self.realm_name}
  98. data_raw = self.connection.raw_get(URL_ADMIN_USERS.format(**params_path), **query)
  99. return raise_error_from_response(data_raw, KeycloakGetError)
  100. def get_idps(self):
  101. """
  102. Returns a list of ID Providers,
  103. IdentityProviderRepresentation
  104. https://www.keycloak.org/docs-api/3.3/rest-api/index.html#_identityproviderrepresentation
  105. :return: array IdentityProviderRepresentation
  106. """
  107. params_path = {"realm-name": self.realm_name}
  108. data_raw = self.connection.raw_get(URL_ADMIN_IDPS.format(**params_path))
  109. return raise_error_from_response(data_raw, KeycloakGetError)
  110. def create_user(self, payload):
  111. """
  112. Create a new user Username must be unique
  113. UserRepresentation
  114. http://www.keycloak.org/docs-api/3.3/rest-api/index.html#_userrepresentation
  115. :param payload: UserRepresentation
  116. :return: UserRepresentation
  117. """
  118. params_path = {"realm-name": self.realm_name}
  119. exists = self.get_user_id(username=payload['username'])
  120. if exists is not None:
  121. return str(exists)
  122. data_raw = self.connection.raw_post(URL_ADMIN_USERS.format(**params_path),
  123. data=json.dumps(payload))
  124. return raise_error_from_response(data_raw, KeycloakGetError, expected_code=201)
  125. def users_count(self):
  126. """
  127. User counter
  128. :return: counter
  129. """
  130. params_path = {"realm-name": self.realm_name}
  131. data_raw = self.connection.raw_get(URL_ADMIN_USERS_COUNT.format(**params_path))
  132. return raise_error_from_response(data_raw, KeycloakGetError)
  133. def get_user_id(self, username):
  134. """
  135. Get internal keycloak user id from username
  136. This is required for further actions against this user.
  137. UserRepresentation
  138. http://www.keycloak.org/docs-api/3.3/rest-api/index.html#_userrepresentation
  139. :param username: id in UserRepresentation
  140. :return: user_id
  141. """
  142. params_path = {"realm-name": self.realm_name, "username": username}
  143. data_raw = self.connection.raw_get(URL_ADMIN_USERS.format(**params_path))
  144. data_content = raise_error_from_response(data_raw, KeycloakGetError)
  145. for user in data_content:
  146. this_use_rname = json.dumps(user["username"]).strip('"')
  147. if this_use_rname == username:
  148. return json.dumps(user["id"]).strip('"')
  149. return None
  150. def get_user(self, user_id):
  151. """
  152. Get representation of the user
  153. :param user_id: User id
  154. UserRepresentation: http://www.keycloak.org/docs-api/3.3/rest-api/index.html#_userrepresentation
  155. :return: UserRepresentation
  156. """
  157. params_path = {"realm-name": self.realm_name, "id": user_id}
  158. data_raw = self.connection.raw_get(URL_ADMIN_USER.format(**params_path))
  159. return raise_error_from_response(data_raw, KeycloakGetError)
  160. def update_user(self, user_id, payload):
  161. """
  162. Update the user
  163. :param user_id: User id
  164. :param payload: UserRepresentation
  165. :return: Http response
  166. """
  167. params_path = {"realm-name": self.realm_name, "id": user_id}
  168. data_raw = self.connection.raw_put(URL_ADMIN_USER.format(**params_path),
  169. data=json.dumps(payload))
  170. return raise_error_from_response(data_raw, KeycloakGetError, expected_code=204)
  171. def delete_user(self, user_id):
  172. """
  173. Delete the user
  174. :param user_id: User id
  175. :return: Http response
  176. """
  177. params_path = {"realm-name": self.realm_name, "id": user_id}
  178. data_raw = self.connection.raw_delete(URL_ADMIN_USER.format(**params_path))
  179. return raise_error_from_response(data_raw, KeycloakGetError, expected_code=204)
  180. def set_user_password(self, user_id, password, temporary=True):
  181. """
  182. Set up a password for the user. If temporary is True, the user will have to reset
  183. the temporary password next time they log in.
  184. http://www.keycloak.org/docs-api/3.2/rest-api/#_users_resource
  185. http://www.keycloak.org/docs-api/3.2/rest-api/#_credentialrepresentation
  186. :param user_id: User id
  187. :param password: New password
  188. :param temporary: True if password is temporary
  189. :return:
  190. """
  191. payload = {"type": "password", "temporary": temporary, "value": password}
  192. params_path = {"realm-name": self.realm_name, "id": user_id}
  193. data_raw = self.connection.raw_put(URL_ADMIN_RESET_PASSWORD.format(**params_path),
  194. data=json.dumps(payload))
  195. return raise_error_from_response(data_raw, KeycloakGetError, expected_code=204)
  196. def consents_user(self, user_id):
  197. """
  198. Get consents granted by the user
  199. :param user_id: User id
  200. :return: consents
  201. """
  202. params_path = {"realm-name": self.realm_name, "id": user_id}
  203. data_raw = self.connection.raw_get(URL_ADMIN_USER_CONSENTS.format(**params_path))
  204. return raise_error_from_response(data_raw, KeycloakGetError)
  205. def send_update_account(self, user_id, payload, client_id=None, lifespan=None, redirect_uri=None):
  206. """
  207. Send a update account email to the user An email contains a
  208. link the user can click to perform a set of required actions.
  209. :param user_id:
  210. :param payload:
  211. :param client_id:
  212. :param lifespan:
  213. :param redirect_uri:
  214. :return:
  215. """
  216. params_path = {"realm-name": self.realm_name, "id": user_id}
  217. params_query = {"client_id": client_id, "lifespan": lifespan, "redirect_uri": redirect_uri}
  218. data_raw = self.connection.raw_put(URL_ADMIN_SEND_UPDATE_ACCOUNT.format(**params_path),
  219. data=payload, **params_query)
  220. return raise_error_from_response(data_raw, KeycloakGetError)
  221. def send_verify_email(self, user_id, client_id=None, redirect_uri=None):
  222. """
  223. Send a update account email to the user An email contains a
  224. link the user can click to perform a set of required actions.
  225. :param user_id: User id
  226. :param client_id: Client id
  227. :param redirect_uri: Redirect uri
  228. :return:
  229. """
  230. params_path = {"realm-name": self.realm_name, "id": user_id}
  231. params_query = {"client_id": client_id, "redirect_uri": redirect_uri}
  232. data_raw = self.connection.raw_put(URL_ADMIN_SEND_VERIFY_EMAIL.format(**params_path),
  233. data={}, **params_query)
  234. return raise_error_from_response(data_raw, KeycloakGetError)
  235. def get_sessions(self, user_id):
  236. """
  237. Get sessions associated with the user
  238. :param user_id: id of user
  239. UserSessionRepresentation
  240. http://www.keycloak.org/docs-api/3.3/rest-api/index.html#_usersessionrepresentation
  241. :return: UserSessionRepresentation
  242. """
  243. params_path = {"realm-name": self.realm_name, "id": user_id}
  244. data_raw = self.connection.raw_get(URL_ADMIN_GET_SESSIONS.format(**params_path))
  245. return raise_error_from_response(data_raw, KeycloakGetError)
  246. def get_server_info(self):
  247. """
  248. Get themes, social providers, auth providers, and event listeners available on this server
  249. ServerInfoRepresentation
  250. http://www.keycloak.org/docs-api/3.3/rest-api/index.html#_serverinforepresentation
  251. :return: ServerInfoRepresentation
  252. """
  253. data_raw = self.connection.raw_get(URL_ADMIN_SERVER_INFO)
  254. return raise_error_from_response(data_raw, KeycloakGetError)
  255. def get_groups(self):
  256. """
  257. Get groups belonging to the realm. Returns a list of groups belonging to the realm
  258. GroupRepresentation
  259. http://www.keycloak.org/docs-api/3.2/rest-api/#_grouprepresentation
  260. :return: array GroupRepresentation
  261. """
  262. params_path = {"realm-name": self.realm_name}
  263. data_raw = self.connection.raw_get(URL_ADMIN_GROUPS.format(**params_path))
  264. return raise_error_from_response(data_raw, KeycloakGetError)
  265. def get_group(self, group_id):
  266. """
  267. Get group by id. Returns full group details
  268. GroupRepresentation
  269. http://www.keycloak.org/docs-api/3.2/rest-api/#_grouprepresentation
  270. :return: Keycloak server response (GroupRepresentation)
  271. """
  272. params_path = {"realm-name": self.realm_name, "id": group_id}
  273. data_raw = self.connection.raw_get(URL_ADMIN_GROUP.format(**params_path))
  274. return raise_error_from_response(data_raw, KeycloakGetError)
  275. def get_subgroups(self, group, path):
  276. """
  277. Utility function to iterate through nested group structures
  278. GroupRepresentation
  279. http://www.keycloak.org/docs-api/3.2/rest-api/#_grouprepresentation
  280. :param name: group (GroupRepresentation)
  281. :param path: group path (string)
  282. :return: Keycloak server response (GroupRepresentation)
  283. """
  284. for subgroup in group["subGroups"]:
  285. if subgroup['path'] == path:
  286. return subgroup
  287. elif subgroup["subGroups"]:
  288. for subgroup in group["subGroups"]:
  289. return self.get_subgroups(subgroup, path)
  290. return None
  291. def get_group_by_path(self, path, search_in_subgroups=False):
  292. """
  293. Get group id based on name or path.
  294. A straight name or path match with a top-level group will return first.
  295. Subgroups are traversed, the first to match path (or name with path) is returned.
  296. GroupRepresentation
  297. http://www.keycloak.org/docs-api/3.2/rest-api/#_grouprepresentation
  298. :param path: group path
  299. :param search_in_subgroups: True if want search in the subgroups
  300. :return: Keycloak server response (GroupRepresentation)
  301. """
  302. groups = self.get_groups()
  303. # TODO: Review this code is necessary
  304. for group in groups:
  305. if group['path'] == path:
  306. return group
  307. elif search_in_subgroups and group["subGroups"]:
  308. res = self.get_subgroups(group, path)
  309. if res != None:
  310. return res
  311. return None
  312. def create_group(self, payload, parent=None, skip_exists=False):
  313. """
  314. Creates a group in the Realm
  315. :param payload: GroupRepresentation
  316. :param parent: parent group's id. Required to create a sub-group.
  317. GroupRepresentation
  318. http://www.keycloak.org/docs-api/3.2/rest-api/#_grouprepresentation
  319. :return: Http response
  320. """
  321. name = payload['name']
  322. path = payload['path']
  323. exists = None
  324. if name is None and path is not None:
  325. path="/" + name
  326. elif path is not None:
  327. exists = self.get_group_by_path(path=path, search_in_subgroups=True)
  328. if exists is not None:
  329. return str(exists)
  330. if parent is None:
  331. params_path = {"realm-name": self.realm_name}
  332. data_raw = self.connection.raw_post(URL_ADMIN_GROUPS.format(**params_path),
  333. data=json.dumps(payload))
  334. else:
  335. params_path = {"realm-name": self.realm_name, "id": parent,}
  336. data_raw = self.connection.raw_post(URL_ADMIN_GROUP_CHILD.format(**params_path),
  337. data=json.dumps(payload))
  338. return raise_error_from_response(data_raw, KeycloakGetError, expected_code=201, skip_exists=skip_exists)
  339. def group_set_permissions(self, group_id, enabled=True):
  340. """
  341. Enable/Disable permissions for a group. Cannot delete group if disabled
  342. :param group_id: id of group
  343. :param enabled: boolean
  344. :return: Keycloak server response
  345. """
  346. params_path = {"realm-name": self.realm_name, "id": group_id}
  347. data_raw = self.connection.raw_put(URL_ADMIN_GROUP_PERMISSIONS.format(**params_path),
  348. data=json.dumps({"enabled": enabled}))
  349. return raise_error_from_response(data_raw, KeycloakGetError)
  350. def group_user_add(self, user_id, group_id):
  351. """
  352. Add user to group (user_id and group_id)
  353. :param group_id: id of group
  354. :param user_id: id of user
  355. :param group_id: id of group to add to
  356. :return: Keycloak server response
  357. """
  358. params_path = {"realm-name": self.realm_name, "id": user_id, "group-id": group_id}
  359. data_raw = self.connection.raw_put(URL_ADMIN_USER_GROUP.format(**params_path), data=None)
  360. return raise_error_from_response(data_raw, KeycloakGetError, expected_code=204)
  361. def group_user_remove(self, user_id, group_id):
  362. """
  363. Remove user from group (user_id and group_id)
  364. :param group_id: id of group
  365. :param user_id: id of user
  366. :param group_id: id of group to add to
  367. :return: Keycloak server response
  368. """
  369. params_path = {"realm-name": self.realm_name, "id": user_id, "group-id": group_id}
  370. data_raw = self.connection.raw_delete(URL_ADMIN_USER_GROUP.format(**params_path))
  371. return raise_error_from_response(data_raw, KeycloakGetError, expected_code=204)
  372. def delete_group(self, group_id):
  373. """
  374. Deletes a group in the Realm
  375. :param group_id: id of group to delete
  376. :return: Keycloak server response
  377. """
  378. params_path = {"realm-name": self.realm_name, "id": group_id}
  379. data_raw = self.connection.raw_delete(URL_ADMIN_GROUP.format(**params_path))
  380. return raise_error_from_response(data_raw, KeycloakGetError, expected_code=204)
  381. def get_clients(self):
  382. """
  383. Get clients belonging to the realm Returns a list of clients belonging to the realm
  384. ClientRepresentation
  385. http://www.keycloak.org/docs-api/3.3/rest-api/index.html#_clientrepresentation
  386. :return: Keycloak server response (ClientRepresentation)
  387. """
  388. params_path = {"realm-name": self.realm_name}
  389. data_raw = self.connection.raw_get(URL_ADMIN_CLIENTS.format(**params_path))
  390. return raise_error_from_response(data_raw, KeycloakGetError)
  391. def get_client(self, client_id):
  392. """
  393. Get representation of the client
  394. ClientRepresentation
  395. http://www.keycloak.org/docs-api/3.3/rest-api/index.html#_clientrepresentation
  396. :param client_id: id of client (not client-id)
  397. :return: Keycloak server response (ClientRepresentation)
  398. """
  399. params_path = {"realm-name": self.realm_name, "id": client_id}
  400. data_raw = self.connection.raw_get(URL_ADMIN_CLIENT.format(**params_path))
  401. return raise_error_from_response(data_raw, KeycloakGetError)
  402. def get_client_id(self, client_name):
  403. """
  404. Get internal keycloak client id from client-id.
  405. This is required for further actions against this client.
  406. :param client_name: name in ClientRepresentation
  407. http://www.keycloak.org/docs-api/3.3/rest-api/index.html#_clientrepresentation
  408. :return: client_id (uuid as string)
  409. """
  410. clients = self.get_clients()
  411. for client in clients:
  412. if client_name == client['name']:
  413. return client["id"]
  414. return None
  415. def create_client(self, payload, skip_exists=False):
  416. """
  417. Create a client
  418. ClientRepresentation: http://www.keycloak.org/docs-api/3.3/rest-api/index.html#_clientrepresentation
  419. :param payload: ClientRepresentation
  420. :return: Keycloak server response (UserRepresentation)
  421. """
  422. params_path = {"realm-name": self.realm_name}
  423. data_raw = self.connection.raw_post(URL_ADMIN_CLIENTS.format(**params_path),
  424. data=json.dumps(payload))
  425. return raise_error_from_response(data_raw, KeycloakGetError, expected_code=201, skip_exists=skip_exists)
  426. def delete_client(self, client_id):
  427. """
  428. Get representation of the client
  429. ClientRepresentation
  430. http://www.keycloak.org/docs-api/3.3/rest-api/index.html#_clientrepresentation
  431. :param client_id: keycloak client id (not oauth client-id)
  432. :return: Keycloak server response (ClientRepresentation)
  433. """
  434. params_path = {"realm-name": self.realm_name, "id": client_id}
  435. data_raw = self.connection.raw_delete(URL_ADMIN_CLIENT.format(**params_path))
  436. return raise_error_from_response(data_raw, KeycloakGetError, expected_code=204)
  437. def get_realm_roles(self):
  438. """
  439. Get all roles for the realm or client
  440. RoleRepresentation
  441. http://www.keycloak.org/docs-api/3.3/rest-api/index.html#_rolerepresentation
  442. :return: Keycloak server response (RoleRepresentation)
  443. """
  444. params_path = {"realm-name": self.realm_name}
  445. data_raw = self.connection.raw_get(URL_ADMIN_REALM_ROLES.format(**params_path))
  446. return raise_error_from_response(data_raw, KeycloakGetError)
  447. def get_client_roles(self, client_id):
  448. """
  449. Get all roles for the client
  450. :param client_id: id of client (not client-id)
  451. RoleRepresentation
  452. http://www.keycloak.org/docs-api/3.3/rest-api/index.html#_rolerepresentation
  453. :return: Keycloak server response (RoleRepresentation)
  454. """
  455. params_path = {"realm-name": self.realm_name, "id": client_id}
  456. data_raw = self.connection.raw_get(URL_ADMIN_CLIENT_ROLES.format(**params_path))
  457. return raise_error_from_response(data_raw, KeycloakGetError)
  458. def get_client_role(self, client_id, role_name):
  459. """
  460. Get client role id by name
  461. This is required for further actions with this role.
  462. :param client_id: id of client (not client-id)
  463. :param role_name: roles name (not id!)
  464. RoleRepresentation
  465. http://www.keycloak.org/docs-api/3.3/rest-api/index.html#_rolerepresentation
  466. :return: role_id
  467. """
  468. params_path = {"realm-name": self.realm_name, "id": client_id, "role-name": role_name}
  469. data_raw = self.connection.raw_get(URL_ADMIN_CLIENT_ROLE.format(**params_path))
  470. return raise_error_from_response(data_raw, KeycloakGetError)
  471. def get_client_role_id(self, client_id, role_name):
  472. """
  473. Warning: Deprecated
  474. Get client role id by name
  475. This is required for further actions with this role.
  476. :param client_id: id of client (not client-id)
  477. :param role_name: roles name (not id!)
  478. RoleRepresentation
  479. http://www.keycloak.org/docs-api/3.3/rest-api/index.html#_rolerepresentation
  480. :return: role_id
  481. """
  482. role = self.get_client_role(client_id, role_name)
  483. return role.get("id")
  484. def create_client_role(self, payload, skip_exists=False):
  485. """
  486. Create a client role
  487. RoleRepresentation
  488. http://www.keycloak.org/docs-api/3.3/rest-api/index.html#_rolerepresentation
  489. :param payload: id of client (not client-id), role_name: name of role
  490. :return: Keycloak server response (RoleRepresentation)
  491. """
  492. params_path = {"realm-name": self.realm_name, "id": self.client_id}
  493. data_raw = self.connection.raw_post(URL_ADMIN_CLIENT_ROLES.format(**params_path),
  494. data=json.dumps(payload))
  495. return raise_error_from_response(data_raw, KeycloakGetError, expected_code=201, skip_exists=skip_exists)
  496. def delete_client_role(self, role_name):
  497. """
  498. Create a client role
  499. RoleRepresentation
  500. http://www.keycloak.org/docs-api/3.3/rest-api/index.html#_rolerepresentation
  501. :param role_name: roles name (not id!)
  502. """
  503. params_path = {"realm-name": self.realm_name, "id": self.client_id, "role-name": role_name}
  504. data_raw = self.connection.raw_delete(URL_ADMIN_CLIENT_ROLE.format(**params_path))
  505. return raise_error_from_response(data_raw, KeycloakGetError, expected_code=204)
  506. def assign_client_role(self, user_id, client_id, roles):
  507. """
  508. Assign a client role to a user
  509. :param client_id: id of client (not client-id)
  510. :param user_id: id of user
  511. :param client_id: id of client containing role,
  512. :param roles: roles list or role (use RoleRepresentation)
  513. :return Keycloak server response
  514. """
  515. payload = roles if isinstance(roles, list) else [roles]
  516. params_path = {"realm-name": self.realm_name, "id": user_id, "client-id": client_id}
  517. data_raw = self.connection.raw_post(URL_ADMIN_USER_CLIENT_ROLES.format(**params_path),
  518. data=json.dumps(payload))
  519. return raise_error_from_response(data_raw, KeycloakGetError, expected_code=204)
  520. def sync_users(self, storage_id, action):
  521. """
  522. Function to trigger user sync from provider
  523. :param storage_id:
  524. :param action:
  525. :return:
  526. """
  527. data = {'action': action}
  528. params_query = {"action": action}
  529. params_path = {"realm-name": self.realm_name, "id": storage_id}
  530. data_raw = self.connection.raw_post(URL_ADMIN_USER_STORAGE.format(**params_path),
  531. data=json.dumps(data), **params_query)
  532. return raise_error_from_response(data_raw, KeycloakGetError)