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.

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