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.

250 lines
8.3 KiB

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
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
6 years ago
7 years ago
6 years ago
  1. .. python-keycloak documentation master file, created by
  2. sphinx-quickstart on Tue Aug 15 11:02:59 2017.
  3. You can adapt this file completely to your liking, but it should at least
  4. contain the root `toctree` directive.
  5. .. toctree::
  6. :maxdepth: 2
  7. :caption: Contents:
  8. Indices and tables
  9. ==================
  10. * :ref:`genindex`
  11. * :ref:`modindex`
  12. * :ref:`search`
  13. .. image:: https://readthedocs.org/projects/python-keycloak/badge/?version=latest
  14. :target: http://python-keycloak.readthedocs.io/en/latest/?badge=latest
  15. Welcome to python-keycloak's documentation!
  16. ===========================================
  17. **python-keycloak** is a Python package providing access to the Keycloak API.
  18. Installation
  19. ==================
  20. Via Pypi Package::
  21. $ pip install python-keycloak
  22. Manually::
  23. $ python setup.py install
  24. Dependencies
  25. ==================
  26. python-keycloak depends on:
  27. * Python 3
  28. * `requests <http://docs.python-requests.org/en/master/>`_
  29. * `python-jose <http://python-jose.readthedocs.io/en/latest/>`_
  30. * `simplejson <https://simplejson.readthedocs.io/en/latest/>`_
  31. Tests Dependencies
  32. ------------------
  33. * unittest
  34. * `httmock <https://github.com/patrys/httmock>`_
  35. Bug reports
  36. ==================
  37. Please report bugs and feature requests at
  38. `https://bitbucket.org/agriness/python-keycloak/issues <https://bitbucket.org/agriness/python-keycloak/issues>`_
  39. Documentation
  40. ==================
  41. The documentation for python-keycloak is available on `readthedocs <http://python-keycloak.readthedocs.io>`_.
  42. Contributors
  43. ==================
  44. * `Agriness Team <http://www.agriness.com/pt/>`_
  45. * `Marcos Pereira <marcospereira.mpj@gmail.com>`_
  46. * `Martin Devlin <martin.devlin@pearson.com>`_
  47. * `Shon T. Urbas <shon.urbas@gmail.com>`_
  48. * `Markus Spanier <https://bitbucket.org/spanierm/>`_
  49. * `Remco Kranenburg <https://bitbucket.org/Remco47/>`_
  50. * `Armin <https://bitbucket.org/arminfelder/>`_
  51. * `Njordr <https://bitbucket.org/njordr/>`_
  52. * `Josha Inglis <https://bitbucket.org/joshainglis/>`_
  53. * `Alex] <https://bitbucket.org/alex_zel/>`_
  54. Usage
  55. =====
  56. Main methods::
  57. # KEYCLOAK OPENID
  58. from keycloak import KeycloakOpenID
  59. # Configure client
  60. keycloak_openid = KeycloakOpenID(server_url="http://localhost:8080/auth/",
  61. client_id="example_client",
  62. realm_name="example_realm",
  63. client_secret_key="secret",
  64. verify=True)
  65. # Get WellKnow
  66. config_well_know = keycloak_openid.well_know()
  67. # Get Token
  68. token = keycloak_openid.token("user", "password")
  69. # Get Userinfo
  70. userinfo = keycloak_openid.userinfo(token['access_token'])
  71. # Refresh token
  72. token = keycloak_openid.refresh_token(token['refresh_token'])
  73. # Logout
  74. keycloak_openid.logout(token['refresh_token'])
  75. # Get Certs
  76. certs = keycloak_openid.certs()
  77. # Get RPT (Entitlement)
  78. token = keycloak_openid.token("user", "password")
  79. rpt = keycloak_openid.entitlement(token['access_token'], "resource_id")
  80. # Instropect RPT
  81. token_rpt_info = keycloak_openid.introspect(keycloak_openid.introspect(token['access_token'], rpt=rpt['rpt'],
  82. token_type_hint="requesting_party_token"))
  83. # Introspect Token
  84. token_info = keycloak_openid.introspect(token['access_token']))
  85. # Decode Token
  86. KEYCLOAK_PUBLIC_KEY = "secret"
  87. options = {"verify_signature": True, "verify_aud": True, "exp": True}
  88. token_info = keycloak_openid.decode_token(token['access_token'], key=KEYCLOAK_PUBLIC_KEY, options=options)
  89. # Get permissions by token
  90. token = keycloak_openid.token("user", "password")
  91. keycloak_openid.load_authorization_config("example-authz-config.json")
  92. policies = keycloak_openid.get_policies(token['access_token'], method_token_info='decode', key=KEYCLOAK_PUBLIC_KEY)
  93. permissions = keycloak_openid.get_permissions(token['access_token'], method_token_info='introspect')
  94. # KEYCLOAK ADMIN
  95. from keycloak import KeycloakAdmin
  96. keycloak_admin = KeycloakAdmin(server_url="http://localhost:8080/auth/",
  97. username='example-admin',
  98. password='secret',
  99. realm_name="example_realm",
  100. verify=True)
  101. # Add user
  102. new_user = keycloak_admin.create_user({"email": "example@example.com",
  103. "username": "example@example.com",
  104. "enabled": True,
  105. "firstName": "Example",
  106. "lastName": "Example",
  107. "realmRoles": ["user_default", ],
  108. "attributes": {"example": "1,2,3,3,"}})
  109. # Add user and set password
  110. new_user = keycloak_admin.create_user({"email": "example@example.com",
  111. "username": "example@example.com",
  112. "enabled": True,
  113. "firstName": "Example",
  114. "lastName": "Example",
  115. "credentials": [{"value": "secret","type": "password",}],
  116. "realmRoles": ["user_default", ],
  117. "attributes": {"example": "1,2,3,3,"}})
  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 = set_user_password(user_id="user-id-keycloak", password="secret", temporary=True)
  131. # Delete User
  132. response = keycloak_admin.delete_user(user_id="user-id-keycloak")
  133. # Get consents granted by the user
  134. consents = keycloak_admin.consents_user(user_id="user-id-keycloak")
  135. # Send User Action
  136. response = keycloak_admin.send_update_account(user_id="user-id-keycloak",
  137. payload=json.dumps(['UPDATE_PASSWORD']))
  138. # Send Verify Email
  139. response = keycloak_admin.send_verify_email(user_id="user-id-keycloak")
  140. # Get sessions associated with the user
  141. sessions = keycloak_admin.get_sessions(user_id="user-id-keycloak")
  142. # Get themes, social providers, auth providers, and event listeners available on this server
  143. server_info = keycloak_admin.get_server_info()
  144. # Get clients belonging to the realm Returns a list of clients belonging to the realm
  145. clients = keycloak_admin.get_clients()
  146. # Get client - id (not client-id) from client by name
  147. client_id=keycloak_admin.get_client_id("my-client")
  148. # Get representation of the client - id of client (not client-id)
  149. client = keycloak_admin.get_client(client_id="client_id")
  150. # Get all roles for the realm or client
  151. realm_roles = keycloak_admin.get_realm_roles()
  152. # Get all roles for the client
  153. client_roles = keycloak_admin.get_client_roles(client_id="client_id")
  154. # Get client role
  155. role = keycloak_admin.get_client_role(client_id="client_id", role_name="role_name")
  156. # Warning: Deprecated
  157. # Get client role id from name
  158. role_id = keycloak_admin.get_client_role_id(client_id="client_id", role_name="test")
  159. # Create client role
  160. keycloak_admin.create_client_role(client_id, "test")
  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. # Create new group
  168. group = keycloak_admin.create_group(name="Example Group")
  169. # Get all groups
  170. groups = keycloak_admin.get_groups()
  171. # Get group
  172. group = keycloak_admin.get_group(group_id='group_id')
  173. # Get group by path
  174. group = keycloak_admin.get_group_by_path(path='/group/subgroup', search_in_subgroups=True)
  175. # Function to trigger user sync from provider
  176. sync_users(storage_id="storage_di", action="action")