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.

293 lines
10 KiB

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
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
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
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
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
7 years ago
7 years ago
7 years ago
4 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
4 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
3 years ago
6 years ago
6 years ago
6 years ago
6 years ago
7 years ago
  1. [![CircleCI](https://github.com/marcospereirampj/python-keycloak/actions/workflows/daily.yaml/badge.svg)](https://github.com/marcospereirampj/python-keycloak/)
  2. [![Documentation Status](https://readthedocs.org/projects/python-keycloak/badge/?version=latest)](http://python-keycloak.readthedocs.io/en/latest/?badge=latest)
  3. # Python Keycloak
  4. For review- see https://github.com/marcospereirampj/python-keycloak
  5. **python-keycloak** is a Python package providing access to the Keycloak API.
  6. ## Installation
  7. ### Via Pypi Package:
  8. `$ pip install python-keycloak`
  9. ### Manually
  10. `$ python setup.py install`
  11. ## Dependencies
  12. python-keycloak depends on:
  13. - Python 3
  14. - [requests](https://requests.readthedocs.io)
  15. - [python-jose](http://python-jose.readthedocs.io/en/latest/)
  16. - [urllib3](https://urllib3.readthedocs.io/en/stable/)
  17. ### Tests Dependencies
  18. - [tox](https://tox.readthedocs.io/)
  19. - [pytest](https://docs.pytest.org/en/latest/)
  20. - [pytest-cov](https://github.com/pytest-dev/pytest-cov)
  21. - [wheel](https://github.com/pypa/wheel)
  22. ## Bug reports
  23. Please report bugs and feature requests at
  24. https://github.com/marcospereirampj/python-keycloak/issues
  25. ## Documentation
  26. The documentation for python-keycloak is available on [readthedocs](http://python-keycloak.readthedocs.io).
  27. ## Contributors
  28. - [Agriness Team](http://www.agriness.com/pt/)
  29. - [Marcos Pereira](marcospereira.mpj@gmail.com)
  30. - [Martin Devlin](https://bitbucket.org/devlinmpearson/)
  31. - [Shon T. Urbas](https://bitbucket.org/surbas/)
  32. - [Markus Spanier](https://bitbucket.org/spanierm/)
  33. - [Remco Kranenburg](https://bitbucket.org/Remco47/)
  34. - [Armin](https://bitbucket.org/arminfelder/)
  35. - [njordr](https://bitbucket.org/njordr/)
  36. - [Josha Inglis](https://bitbucket.org/joshainglis/)
  37. - [Alex](https://bitbucket.org/alex_zel/)
  38. - [Ewan Jone](https://bitbucket.org/kisamoto/)
  39. - [Lukas Martini](https://github.com/lutoma)
  40. - [Adamatics](https://www.adamatics.com)
  41. ## Usage
  42. ```python
  43. from keycloak import KeycloakOpenID
  44. # Configure client
  45. keycloak_openid = KeycloakOpenID(server_url="http://localhost:8080/auth/",
  46. client_id="example_client",
  47. realm_name="example_realm",
  48. client_secret_key="secret")
  49. # Get WellKnow
  50. config_well_know = keycloak_openid.well_know()
  51. # Get Token
  52. token = keycloak_openid.token("user", "password")
  53. token = keycloak_openid.token("user", "password", totp="012345")
  54. # Get Userinfo
  55. userinfo = keycloak_openid.userinfo(token['access_token'])
  56. # Refresh token
  57. token = keycloak_openid.refresh_token(token['refresh_token'])
  58. # Logout
  59. keycloak_openid.logout(token['refresh_token'])
  60. # Get Certs
  61. certs = keycloak_openid.certs()
  62. # Get RPT (Entitlement)
  63. token = keycloak_openid.token("user", "password")
  64. rpt = keycloak_openid.entitlement(token['access_token'], "resource_id")
  65. # Instropect RPT
  66. token_rpt_info = keycloak_openid.introspect(keycloak_openid.introspect(token['access_token'], rpt=rpt['rpt'],
  67. token_type_hint="requesting_party_token"))
  68. # Introspect Token
  69. token_info = keycloak_openid.introspect(token['access_token'])
  70. # Decode Token
  71. KEYCLOAK_PUBLIC_KEY = "-----BEGIN PUBLIC KEY-----\n" + keycloak_openid.public_key() + "\n-----END PUBLIC KEY-----"
  72. options = {"verify_signature": True, "verify_aud": True, "verify_exp": True}
  73. token_info = keycloak_openid.decode_token(token['access_token'], key=KEYCLOAK_PUBLIC_KEY, options=options)
  74. # Get permissions by token
  75. token = keycloak_openid.token("user", "password")
  76. keycloak_openid.load_authorization_config("example-authz-config.json")
  77. policies = keycloak_openid.get_policies(token['access_token'], method_token_info='decode', key=KEYCLOAK_PUBLIC_KEY)
  78. permissions = keycloak_openid.get_permissions(token['access_token'], method_token_info='introspect')
  79. # KEYCLOAK ADMIN
  80. from keycloak import KeycloakAdmin
  81. keycloak_admin = KeycloakAdmin(server_url="http://localhost:8080/auth/",
  82. username='example-admin',
  83. password='secret',
  84. realm_name="master",
  85. user_realm_name="only_if_other_realm_than_master",
  86. client_secret_key="client-secret",
  87. verify=True)
  88. # Add user
  89. new_user = keycloak_admin.create_user({"email": "example@example.com",
  90. "username": "example@example.com",
  91. "enabled": True,
  92. "firstName": "Example",
  93. "lastName": "Example"})
  94. # Add user and raise exception if username already exists
  95. # exist_ok currently defaults to True for backwards compatibility reasons
  96. new_user = keycloak_admin.create_user({"email": "example@example.com",
  97. "username": "example@example.com",
  98. "enabled": True,
  99. "firstName": "Example",
  100. "lastName": "Example"},
  101. exist_ok=False)
  102. # Add user and set password
  103. new_user = keycloak_admin.create_user({"email": "example@example.com",
  104. "username": "example@example.com",
  105. "enabled": True,
  106. "firstName": "Example",
  107. "lastName": "Example",
  108. "credentials": [{"value": "secret","type": "password",}]})
  109. # Add user and specify a locale
  110. new_user = keycloak_admin.create_user({"email": "example@example.fr",
  111. "username": "example@example.fr",
  112. "enabled": True,
  113. "firstName": "Example",
  114. "lastName": "Example",
  115. "attributes": {
  116. "locale": ["fr"]
  117. })
  118. # User counter
  119. count_users = keycloak_admin.users_count()
  120. # Get users Returns a list of users, filtered according to query parameters
  121. users = keycloak_admin.get_users({})
  122. # Get user ID from name
  123. user_id_keycloak = keycloak_admin.get_user_id("example@example.com")
  124. # Get User
  125. user = keycloak_admin.get_user("user-id-keycloak")
  126. # Update User
  127. response = keycloak_admin.update_user(user_id="user-id-keycloak",
  128. payload={'firstName': 'Example Update'})
  129. # Update User Password
  130. response = keycloak_admin.set_user_password(user_id="user-id-keycloak", password="secret", temporary=True)
  131. # Get User Credentials
  132. credentials = keycloak_admin.get_credentials(user_id='user_id')
  133. # Get User Credential by ID
  134. credential = keycloak_admin.get_credential(user_id='user_id', credential_id='credential_id')
  135. # Delete User Credential
  136. response = keycloak_admin.delete_credential(user_id='user_id', credential_id='credential_id')
  137. # Delete User
  138. response = keycloak_admin.delete_user(user_id="user-id-keycloak")
  139. # Get consents granted by the user
  140. consents = keycloak_admin.consents_user(user_id="user-id-keycloak")
  141. # Send User Action
  142. response = keycloak_admin.send_update_account(user_id="user-id-keycloak",
  143. payload=json.dumps(['UPDATE_PASSWORD']))
  144. # Send Verify Email
  145. response = keycloak_admin.send_verify_email(user_id="user-id-keycloak")
  146. # Get sessions associated with the user
  147. sessions = keycloak_admin.get_sessions(user_id="user-id-keycloak")
  148. # Get themes, social providers, auth providers, and event listeners available on this server
  149. server_info = keycloak_admin.get_server_info()
  150. # Get clients belonging to the realm Returns a list of clients belonging to the realm
  151. clients = keycloak_admin.get_clients()
  152. # Get client - id (not client-id) from client by name
  153. client_id = keycloak_admin.get_client_id("my-client")
  154. # Get representation of the client - id of client (not client-id)
  155. client = keycloak_admin.get_client(client_id="client_id")
  156. # Get all roles for the realm or client
  157. realm_roles = keycloak_admin.get_realm_roles()
  158. # Get all roles for the client
  159. client_roles = keycloak_admin.get_client_roles(client_id="client_id")
  160. # Get client role
  161. role = keycloak_admin.get_client_role(client_id="client_id", role_name="role_name")
  162. # Warning: Deprecated
  163. # Get client role id from name
  164. role_id = keycloak_admin.get_client_role_id(client_id="client_id", role_name="test")
  165. # Create client role
  166. keycloak_admin.create_client_role(client_role_id='client_id', {'name': 'roleName', 'clientRole': True})
  167. # Assign client role to user. Note that BOTH role_name and role_id appear to be required.
  168. keycloak_admin.assign_client_role(client_id="client_id", user_id="user_id", role_id="role_id", role_name="test")
  169. # Retrieve client roles of a user.
  170. keycloak_admin.get_client_roles_of_user(user_id="user_id", client_id="client_id")
  171. # Retrieve available client roles of a user.
  172. keycloak_admin.get_available_client_roles_of_user(user_id="user_id", client_id="client_id")
  173. # Retrieve composite client roles of a user.
  174. keycloak_admin.get_composite_client_roles_of_user(user_id="user_id", client_id="client_id")
  175. # Delete client roles of a user.
  176. keycloak_admin.delete_client_roles_of_user(client_id="client_id", user_id="user_id", roles={"id": "role-id"})
  177. keycloak_admin.delete_client_roles_of_user(client_id="client_id", user_id="user_id", roles=[{"id": "role-id_1"}, {"id": "role-id_2"}])
  178. # Get all client authorization resources
  179. client_resources = get_client_authz_resources(client_id="client_id")
  180. # Get all client authorization scopes
  181. client_scopes = get_client_authz_scopes(client_id="client_id")
  182. # Get all client authorization permissions
  183. client_permissions = get_client_authz_permissions(client_id="client_id")
  184. # Get all client authorization policies
  185. client_policies = get_client_authz_policies(client_id="client_id")
  186. # Create new group
  187. group = keycloak_admin.create_group({"name": "Example Group"})
  188. # Get all groups
  189. groups = keycloak_admin.get_groups()
  190. # Get group
  191. group = keycloak_admin.get_group(group_id='group_id')
  192. # Get group by name
  193. group = keycloak_admin.get_group_by_path(path='/group/subgroup', search_in_subgroups=True)
  194. # Function to trigger user sync from provider
  195. sync_users(storage_id="storage_di", action="action")
  196. # Get client role id from name
  197. role_id = keycloak_admin.get_client_role_id(client_id=client_id, role_name="test")
  198. # Get all roles for the realm or client
  199. realm_roles = keycloak_admin.get_roles()
  200. # Assign client role to user. Note that BOTH role_name and role_id appear to be required.
  201. keycloak_admin.assign_client_role(client_id=client_id, user_id=user_id, role_id=role_id, role_name="test")
  202. # Assign realm roles to user
  203. keycloak_admin.assign_realm_roles(user_id=user_id, roles=realm_roles)
  204. # Get all ID Providers
  205. idps = keycloak_admin.get_idps()
  206. # Create a new Realm
  207. keycloak_admin.create_realm(payload={"realm": "demo"}, skip_exists=False)
  208. ```