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.

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