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.

408 lines
12 KiB

  1. .. admin:
  2. Use Python Keycloak Asynchronously
  3. ==================================
  4. Asynchronous admin client
  5. -------------------------
  6. Configure admin client
  7. ------------------------
  8. .. code-block:: python
  9. admin = KeycloakAdmin(
  10. server_url="http://localhost:8080/",
  11. username='example-admin',
  12. password='secret',
  13. realm_name="master",
  14. user_realm_name="only_if_other_realm_than_master")
  15. Configure admin client with connection
  16. -----------------------------------------
  17. .. code-block:: python
  18. from keycloak import KeycloakAdmin
  19. from keycloak import KeycloakOpenIDConnection
  20. keycloak_connection = KeycloakOpenIDConnection(
  21. server_url="http://localhost:8080/",
  22. username='example-admin',
  23. password='secret',
  24. realm_name="master",
  25. user_realm_name="only_if_other_realm_than_master",
  26. client_id="my_client",
  27. client_secret_key="client-secret",
  28. verify=True)
  29. keycloak_admin = KeycloakAdmin(connection=keycloak_connection)
  30. Create user asynchronously
  31. ----------------------------
  32. .. code-block:: python
  33. new_user = await keycloak_admin.a_create_user({"email": "example@example.com",
  34. "username": "example@example.com",
  35. "enabled": True,
  36. "firstName": "Example",
  37. "lastName": "Example"})
  38. Add user asynchronously and raise exception if username already exists
  39. -----------------------------------------------------------------------
  40. The exist_ok currently defaults to True for backwards compatibility reasons.
  41. .. code-block:: python
  42. new_user = await keycloak_admin.a_create_user({"email": "example@example.com",
  43. "username": "example@example.com",
  44. "enabled": True,
  45. "firstName": "Example",
  46. "lastName": "Example"},
  47. exist_ok=False)
  48. Add user asynchronously and set password
  49. ----------------------------------------
  50. .. code-block:: python
  51. new_user = await keycloak_admin.a_create_user({"email": "example@example.com",
  52. "username": "example@example.com",
  53. "enabled": True,
  54. "firstName": "Example",
  55. "lastName": "Example",
  56. "credentials": [{"value": "secret","type": "password",}]})
  57. Add user asynchronous and specify a locale
  58. -------------------------------------------
  59. .. code-block:: python
  60. new_user = await keycloak_admin.a_create_user({"email": "example@example.fr",
  61. "username": "example@example.fr",
  62. "enabled": True,
  63. "firstName": "Example",
  64. "lastName": "Example",
  65. "attributes": {
  66. "locale": ["fr"]
  67. }})
  68. Asynchronous User counter
  69. ------------------------------
  70. .. code-block:: python
  71. count_users = await keycloak_admin.a_users_count()
  72. Get users Returns a list of users asynchronously, filtered according to query parameters
  73. -----------------------------------------------------------------------------------------
  74. .. code-block:: python
  75. users = await keycloak_admin.a_get_users({})
  76. Get user ID asynchronously from username
  77. -----------------------------------------
  78. .. code-block:: python
  79. user_id_keycloak = await keycloak_admin.a_get_user_id("username-keycloak")
  80. Get user asynchronously
  81. ------------------------------
  82. .. code-block:: python
  83. user = await keycloak_admin.a_get_user("user-id-keycloak")
  84. Update user asynchronously
  85. ------------------------------
  86. .. code-block:: python
  87. response = await keycloak_admin.a_update_user(user_id="user-id-keycloak",
  88. payload={'firstName': 'Example Update'})
  89. Update user password asynchronously
  90. ------------------------------------
  91. .. code-block:: python
  92. response = await keycloak_admin.a_set_user_password(user_id="user-id-keycloak", password="secret", temporary=True)
  93. Get user credentials asynchronously
  94. ------------------------------------
  95. .. code-block:: python
  96. credentials = await keycloak_admin.a_get_credentials(user_id='user_id')
  97. Get user credential asynchronously by ID
  98. -----------------------------------------
  99. .. code-block:: python
  100. credential = await keycloak_admin.a_get_credential(user_id='user_id', credential_id='credential_id')
  101. Delete user credential asynchronously
  102. ---------------------------------------
  103. .. code-block:: python
  104. response = await keycloak_admin.a_delete_credential(user_id='user_id', credential_id='credential_id')
  105. Delete User asynchronously
  106. ------------------------------
  107. .. code-block:: python
  108. response = await keycloak_admin.a_delete_user(user_id="user-id-keycloak")
  109. Get consents granted asynchronously by the user
  110. ------------------------------------------------
  111. .. code-block:: python
  112. consents = await keycloak_admin.a_consents_user(user_id="user-id-keycloak")
  113. Send user action asynchronously
  114. ---------------------------------
  115. .. code-block:: python
  116. response = await keycloak_admin.a_send_update_account(user_id="user-id-keycloak",
  117. payload=['UPDATE_PASSWORD'])
  118. Send verify email asynchronously
  119. ----------------------------------
  120. .. code-block:: python
  121. response = await keycloak_admin.a_send_verify_email(user_id="user-id-keycloak")
  122. Get sessions associated asynchronously with the user
  123. -----------------------------------------------------
  124. .. code-block:: python
  125. sessions = await keycloak_admin.a_get_sessions(user_id="user-id-keycloak")
  126. Asynchronous OpenID Client
  127. ===========================
  128. Asynchronous Configure client OpenID
  129. -------------------------------------
  130. .. code-block:: python
  131. from keycloak import KeycloakOpenID
  132. # Configure client
  133. # For versions older than 18 /auth/ must be added at the end of the server_url.
  134. keycloak_openid = KeycloakOpenID(server_url="http://localhost:8080/",
  135. client_id="example_client",
  136. realm_name="example_realm",
  137. client_secret_key="secret")
  138. Get .well_know asynchronously
  139. ------------------------------
  140. .. code-block:: python
  141. config_well_known = await keycloak_openid.a_well_known()
  142. Get code asynchronously with OAuth authorization request
  143. ---------------------------------------------------------
  144. .. code-block:: python
  145. auth_url = await keycloak_openid.a_auth_url(
  146. redirect_uri="your_call_back_url",
  147. scope="email",
  148. state="your_state_info")
  149. Get access token asynchronously with code
  150. ----------------------------------------------
  151. .. code-block:: python
  152. access_token = await keycloak_openid.a_token(
  153. grant_type='authorization_code',
  154. code='the_code_you_get_from_auth_url_callback',
  155. redirect_uri="your_call_back_url")
  156. Get access asynchronously token with user and password
  157. -------------------------------------------------------
  158. .. code-block:: python
  159. token = await keycloak_openid.a_token("user", "password")
  160. token = await keycloak_openid.a_token("user", "password", totp="012345")
  161. Get token asynchronously using Token Exchange
  162. ----------------------------------------------
  163. .. code-block:: python
  164. token = await keycloak_openid.a_exchange_token(token['access_token'],
  165. "my_client", "other_client", "some_user")
  166. Refresh token asynchronously
  167. ----------------------------------------------
  168. .. code-block:: python
  169. token = await keycloak_openid.a_refresh_token(token['refresh_token'])
  170. Get UserInfo asynchronously
  171. ----------------------------------------------
  172. .. code-block:: python
  173. userinfo = await keycloak_openid.a_userinfo(token['access_token'])
  174. Logout asynchronously
  175. ----------------------------------------------
  176. .. code-block:: python
  177. await keycloak_openid.a_logout(token['refresh_token'])
  178. Get certs asynchronously
  179. ----------------------------------------------
  180. .. code-block:: python
  181. certs = await keycloak_openid.a_certs()
  182. Introspect RPT asynchronously
  183. ----------------------------------------------
  184. .. code-block:: python
  185. token_rpt_info = await keycloak_openid.a_introspect(await keycloak_openid.a_introspect(token['access_token'],
  186. rpt=rpt['rpt'],
  187. token_type_hint="requesting_party_token"))
  188. Introspect token asynchronously
  189. ----------------------------------------------
  190. .. code-block:: python
  191. token_info = await keycloak_openid.a_introspect(token['access_token'])
  192. Decode token asynchronously
  193. ----------------------------------------------
  194. .. code-block:: python
  195. token_info = await keycloak_openid.a_decode_token(token['access_token'])
  196. # Without validation
  197. token_info = await keycloak_openid.a_decode_token(token['access_token'], validate=False)
  198. Get UMA-permissions asynchronously by token
  199. ----------------------------------------------
  200. .. code-block:: python
  201. token = await keycloak_openid.a_token("user", "password")
  202. permissions = await keycloak_openid.a_uma_permissions(token['access_token'])
  203. Get UMA-permissions asynchronously by token with specific resource and scope requested
  204. ---------------------------------------------------------------------------------------
  205. .. code-block:: python
  206. token = await keycloak_openid.a_token("user", "password")
  207. permissions = await keycloak_openid.a_uma_permissions(token['access_token'], permissions="Resource#Scope")
  208. Get auth status asynchronously for a specific resource and scope by token
  209. --------------------------------------------------------------------------
  210. .. code-block:: python
  211. token = await keycloak_openid.a_token("user", "password")
  212. auth_status = await keycloak_openid.a_has_uma_access(token['access_token'], "Resource#Scope")
  213. Asynchronous UMA
  214. ========================
  215. Asynchronous Configure client UMA
  216. ----------------------------------
  217. .. code-block:: python
  218. from keycloak import KeycloakOpenIDConnection
  219. from keycloak import KeycloakUMA
  220. keycloak_connection = KeycloakOpenIDConnection(
  221. server_url="http://localhost:8080/",
  222. realm_name="master",
  223. client_id="my_client",
  224. client_secret_key="client-secret")
  225. keycloak_uma = KeycloakUMA(connection=keycloak_connection)
  226. Create a resource set asynchronously
  227. ---------------------------------------
  228. .. code-block:: python
  229. resource_set = await keycloak_uma.a_resource_set_create({
  230. "name": "example_resource",
  231. "scopes": ["example:read", "example:write"],
  232. "type": "urn:example"})
  233. List resource sets asynchronously
  234. ----------------------------------
  235. .. code-block:: python
  236. resource_sets = await uma.a_resource_set_list()
  237. Get resource set asynchronously
  238. --------------------------------
  239. .. code-block:: python
  240. latest_resource = await uma.a_resource_set_read(resource_set["_id"])
  241. Update resource set asynchronously
  242. -------------------------------------
  243. .. code-block:: python
  244. latest_resource["name"] = "New Resource Name"
  245. await uma.a_resource_set_update(resource_set["_id"], latest_resource)
  246. Delete resource set asynchronously
  247. ------------------------------------
  248. .. code-block:: python
  249. await uma.a_resource_set_delete(resource_id=resource_set["_id"])