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.

243 lines
7.9 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. * `Martin Devlin <martin.devlin@pearson.com>`_
  46. * `Shon T. Urbas <shon.urbas@gmail.com>`_
  47. Usage
  48. =====
  49. Main methods::
  50. # KEYCLOAK OPENID
  51. from keycloak import KeycloakOpenID
  52. # Configure client
  53. keycloak_openid = KeycloakOpenID(server_url="http://localhost:8080/auth/",
  54. client_id="example_client",
  55. realm_name="example_realm",
  56. client_secret_key="secret",
  57. verify=True)
  58. # Get WellKnow
  59. config_well_know = keycloak_openid.well_know()
  60. # Get Token
  61. token = keycloak_openid.token("user", "password")
  62. # Get Userinfo
  63. userinfo = keycloak_openid.userinfo(token['access_token'])
  64. # Refresh token
  65. token = keycloak_openid.refresh_token(token['refresh_token'])
  66. # Logout
  67. keycloak_openid.logout(token['refresh_token'])
  68. # Get Certs
  69. certs = keycloak_openid.certs()
  70. # Get RPT (Entitlement)
  71. token = keycloak_openid.token("user", "password")
  72. rpt = keycloak_openid.entitlement(token['access_token'], "resource_id")
  73. # Instropect RPT
  74. token_rpt_info = keycloak_openid.introspect(keycloak_openid.introspect(token['access_token'], rpt=rpt['rpt'],
  75. token_type_hint="requesting_party_token"))
  76. # Introspect Token
  77. token_info = keycloak_openid.introspect(token['access_token']))
  78. # Decode Token
  79. KEYCLOAK_PUBLIC_KEY = "secret"
  80. options = {"verify_signature": True, "verify_aud": True, "exp": True}
  81. token_info = keycloak_openid.decode_token(token['access_token'], key=KEYCLOAK_PUBLIC_KEY, options=options)
  82. # Get permissions by token
  83. token = keycloak_openid.token("user", "password")
  84. keycloak_openid.load_authorization_config("example-authz-config.json")
  85. policies = keycloak_openid.get_policies(token['access_token'], method_token_info='decode', key=KEYCLOAK_PUBLIC_KEY)
  86. permissions = keycloak_openid.get_permissions(token['access_token'], method_token_info='introspect')
  87. # KEYCLOAK ADMIN
  88. from keycloak import KeycloakAdmin
  89. keycloak_admin = KeycloakAdmin(server_url="http://localhost:8080/auth/",
  90. username='example-admin',
  91. password='secret',
  92. realm_name="example_realm",
  93. verify=True)
  94. # Add user
  95. new_user = keycloak_admin.create_user({"email": "example@example.com",
  96. "username": "example@example.com",
  97. "enabled": True,
  98. "firstName": "Example",
  99. "lastName": "Example",
  100. "realmRoles": ["user_default", ],
  101. "attributes": {"example": "1,2,3,3,"}})
  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. "realmRoles": ["user_default", ],
  110. "attributes": {"example": "1,2,3,3,"}})
  111. # User counter
  112. count_users = keycloak_admin.users_count()
  113. # Get users Returns a list of users, filtered according to query parameters
  114. users = keycloak_admin.get_users({})
  115. # Get user ID from name
  116. user-id-keycloak = keycloak_admin.get_user_id("example@example.com")
  117. # Get User
  118. user = keycloak_admin.get_user("user-id-keycloak")
  119. # Update User
  120. response = keycloak_admin.update_user(user_id="user-id-keycloak",
  121. payload={'firstName': 'Example Update'})
  122. # Update User Password
  123. response = set_user_password(user_id="user-id-keycloak", password="secret", temporary=True)
  124. # Delete User
  125. response = keycloak_admin.delete_user(user_id="user-id-keycloak")
  126. # Get consents granted by the user
  127. consents = keycloak_admin.consents_user(user_id="user-id-keycloak")
  128. # Send User Action
  129. response = keycloak_admin.send_update_account(user_id="user-id-keycloak",
  130. payload=json.dumps(['UPDATE_PASSWORD']))
  131. # Send Verify Email
  132. response = keycloak_admin.send_verify_email(user_id="user-id-keycloak")
  133. # Get sessions associated with the user
  134. sessions = keycloak_admin.get_sessions(user_id="user-id-keycloak")
  135. # Get themes, social providers, auth providers, and event listeners available on this server
  136. server_info = keycloak_admin.get_server_info()
  137. # Get clients belonging to the realm Returns a list of clients belonging to the realm
  138. clients = keycloak_admin.get_clients()
  139. # Get client - id (not client-id) from client by name
  140. client_id=keycloak_admin.get_client_id("my-client")
  141. # Get representation of the client - id of client (not client-id)
  142. client = keycloak_admin.get_client(client_id="client_id")
  143. # Get all roles for the realm or client
  144. realm_roles = keycloak_admin.get_realm_roles()
  145. # Get all roles for the client
  146. client_roles = keycloak_admin.get_client_roles(client_id="client_id")
  147. # Get client role
  148. role = keycloak_admin.get_client_role(client_id="client_id", role_name="role_name")
  149. # Warning: Deprecated
  150. # Get client role id from name
  151. role_id = keycloak_admin.get_client_role_id(client_id="client_id", role_name="test")
  152. # Create client role
  153. keycloak_admin.create_client_role(client_id, "test")
  154. # Get client role id from name
  155. role_id = keycloak_admin.get_client_role_id(client_id=client_id, role_name="test")
  156. # Get all roles for the realm or client
  157. realm_roles = keycloak_admin.get_roles()
  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. # Create new group
  161. group = keycloak_admin.create_group(name="Example Group")
  162. # Get all groups
  163. groups = keycloak_admin.get_groups()
  164. # Get group
  165. group = keycloak_admin.get_group(group_id='group_id')
  166. # Get group by path
  167. group = keycloak_admin.get_group_by_path(path='/group/subgroup', search_in_subgroups=True)
  168. # Function to trigger user sync from provider
  169. sync_users(storage_id="storage_di", action="action")