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.

266 lines
9.5 KiB

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