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.

246 lines
8.6 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. ## Usage
  38. ```python
  39. from keycloak import KeycloakOpenID
  40. # Configure client
  41. keycloak_openid = KeycloakOpenID(server_url="http://localhost:8080/auth/",
  42. client_id="example_client",
  43. realm_name="example_realm",
  44. client_secret_key="secret")
  45. # Get WellKnow
  46. config_well_know = keycloak_openid.well_know()
  47. # Get Token
  48. token = keycloak_openid.token("user", "password")
  49. token = keycloak_openid.token("user", "password", totp="012345")
  50. # Get Userinfo
  51. userinfo = keycloak_openid.userinfo(token['access_token'])
  52. # Refresh token
  53. token = keycloak_openid.refresh_token(token['refresh_token'])
  54. # Logout
  55. keycloak_openid.logout(token['refresh_token'])
  56. # Get Certs
  57. certs = keycloak_openid.certs()
  58. # Get RPT (Entitlement)
  59. token = keycloak_openid.token("user", "password")
  60. rpt = keycloak_openid.entitlement(token['access_token'], "resource_id")
  61. # Instropect RPT
  62. token_rpt_info = keycloak_openid.introspect(keycloak_openid.introspect(token['access_token'], rpt=rpt['rpt'],
  63. token_type_hint="requesting_party_token"))
  64. # Introspect Token
  65. token_info = keycloak_openid.introspect(token['access_token'])
  66. # Decode Token
  67. KEYCLOAK_PUBLIC_KEY = keycloak_openid.public_key()
  68. options = {"verify_signature": True, "verify_aud": True, "verify_exp": True}
  69. token_info = keycloak_openid.decode_token(token['access_token'], key=KEYCLOAK_PUBLIC_KEY, options=options)
  70. # Get permissions by token
  71. token = keycloak_openid.token("user", "password")
  72. keycloak_openid.load_authorization_config("example-authz-config.json")
  73. policies = keycloak_openid.get_policies(token['access_token'], method_token_info='decode', key=KEYCLOAK_PUBLIC_KEY)
  74. permissions = keycloak_openid.get_permissions(token['access_token'], method_token_info='introspect')
  75. # KEYCLOAK ADMIN
  76. from keycloak import KeycloakAdmin
  77. keycloak_admin = KeycloakAdmin(server_url="http://localhost:8080/auth/",
  78. username='example-admin',
  79. password='secret',
  80. realm_name="master",
  81. user_realm_name="only_if_other_realm_than_master",
  82. client_secret_key="client-secret",
  83. verify=True)
  84. # Add user
  85. new_user = keycloak_admin.create_user({"email": "example@example.com",
  86. "username": "example@example.com",
  87. "enabled": True,
  88. "firstName": "Example",
  89. "lastName": "Example"})
  90. # Add user and set password
  91. new_user = keycloak_admin.create_user({"email": "example@example.com",
  92. "username": "example@example.com",
  93. "enabled": True,
  94. "firstName": "Example",
  95. "lastName": "Example",
  96. "credentials": [{"value": "secret","type": "password",}]})
  97. # User counter
  98. count_users = keycloak_admin.users_count()
  99. # Get users Returns a list of users, filtered according to query parameters
  100. users = keycloak_admin.get_users({})
  101. # Get user ID from name
  102. user_id_keycloak = keycloak_admin.get_user_id("example@example.com")
  103. # Get User
  104. user = keycloak_admin.get_user("user-id-keycloak")
  105. # Update User
  106. response = keycloak_admin.update_user(user_id="user-id-keycloak",
  107. payload={'firstName': 'Example Update'})
  108. # Update User Password
  109. response = keycloak_admin.set_user_password(user_id="user-id-keycloak", password="secret", temporary=True)
  110. # Delete User
  111. response = keycloak_admin.delete_user(user_id="user-id-keycloak")
  112. # Get consents granted by the user
  113. consents = keycloak_admin.consents_user(user_id="user-id-keycloak")
  114. # Send User Action
  115. response = keycloak_admin.send_update_account(user_id="user-id-keycloak",
  116. payload=json.dumps(['UPDATE_PASSWORD']))
  117. # Send Verify Email
  118. response = keycloak_admin.send_verify_email(user_id="user-id-keycloak")
  119. # Get sessions associated with the user
  120. sessions = keycloak_admin.get_sessions(user_id="user-id-keycloak")
  121. # Get themes, social providers, auth providers, and event listeners available on this server
  122. server_info = keycloak_admin.get_server_info()
  123. # Get clients belonging to the realm Returns a list of clients belonging to the realm
  124. clients = keycloak_admin.get_clients()
  125. # Get client - id (not client-id) from client by name
  126. client_id = keycloak_admin.get_client_id("my-client")
  127. # Get representation of the client - id of client (not client-id)
  128. client = keycloak_admin.get_client(client_id="client_id")
  129. # Get all roles for the realm or client
  130. realm_roles = keycloak_admin.get_realm_roles()
  131. # Get all roles for the client
  132. client_roles = keycloak_admin.get_client_roles(client_id="client_id")
  133. # Get client role
  134. role = keycloak_admin.get_client_role(client_id="client_id", role_name="role_name")
  135. # Warning: Deprecated
  136. # Get client role id from name
  137. role_id = keycloak_admin.get_client_role_id(client_id="client_id", role_name="test")
  138. # Create client role
  139. keycloak_admin.create_client_role(client_id='client_id', {'name': 'roleName', 'clientRole': True})
  140. # Assign client role to user. Note that BOTH role_name and role_id appear to be required.
  141. keycloak_admin.assign_client_role(client_id="client_id", user_id="user_id", role_id="role_id", role_name="test")
  142. # Retrieve client roles of a user.
  143. keycloak_admin.get_client_roles_of_user(user_id="user_id", client_id="client_id")
  144. # Retrieve available client roles of a user.
  145. keycloak_admin.get_available_client_roles_of_user(user_id="user_id", client_id="client_id")
  146. # Retrieve composite client roles of a user.
  147. keycloak_admin.get_composite_client_roles_of_user(user_id="user_id", client_id="client_id")
  148. # Delete client roles of a user.
  149. keycloak_admin.delete_client_roles_of_user(client_id="client_id", user_id="user_id", roles={"id": "role-id"})
  150. keycloak_admin.delete_client_roles_of_user(client_id="client_id", user_id="user_id", roles=[{"id": "role-id_1"}, {"id": "role-id_2"}])
  151. # Create new group
  152. group = keycloak_admin.create_group(name="Example Group")
  153. # Get all groups
  154. groups = keycloak_admin.get_groups()
  155. # Get group
  156. group = keycloak_admin.get_group(group_id='group_id')
  157. # Get group by name
  158. group = keycloak_admin.get_group_by_path(path='/group/subgroup', search_in_subgroups=True)
  159. # Function to trigger user sync from provider
  160. sync_users(storage_id="storage_di", action="action")
  161. # Get client role id from name
  162. role_id = keycloak_admin.get_client_role_id(client_id=client_id, role_name="test")
  163. # Get all roles for the realm or client
  164. realm_roles = keycloak_admin.get_roles()
  165. # Assign client role to user. Note that BOTH role_name and role_id appear to be required.
  166. keycloak_admin.assign_client_role(client_id=client_id, user_id=user_id, role_id=role_id, role_name="test")
  167. # Get all ID Providers
  168. idps = keycloak_admin.get_idps()
  169. # Create a new Realm
  170. keycloak_admin.create_realm(payload={"realm": "demo"}, skip_exists=False)
  171. ```