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.

127 lines
4.5 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
9 months ago
7 months ago
9 months 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
9 months ago
  1. [![CircleCI](https://github.com/marcospereirampj/python-keycloak/actions/workflows/daily.yaml/badge.svg)](https://github.com/marcospereirampj/python-keycloak/)
  2. [![Documentation Status](https://readthedocs.org/projects/python-keycloak/badge/?version=latest)](http://python-keycloak.readthedocs.io/en/latest/?badge=latest)
  3. # Python Keycloak
  4. **python-keycloak** is a Python package providing access to the Keycloak API.
  5. ## Installation
  6. Install via PyPI:
  7. `$ pip install python-keycloak`
  8. ## Bug reports
  9. Please report bugs and feature requests at
  10. https://github.com/marcospereirampj/python-keycloak/issues
  11. ## Documentation
  12. The documentation for python-keycloak is available on [readthedocs](http://python-keycloak.readthedocs.io).
  13. ## Keycloak version support
  14. The library strives to always support Keycloak's latest version. Additionally to that, we also support 5 latest major versions of Keycloak,
  15. in order to give our user base more time for smoother upgrades.
  16. Current list of supported Keycloak versions:
  17. - 25.X
  18. - 24.X
  19. - 23.X
  20. - 22.X
  21. - 21.X
  22. ## Python version support
  23. We only support Python versions that have active or security support by the Python Software Foundation. You can find the list of active Python versions [here](https://endoflife.date/python).
  24. ## Example of Using Keycloak OpenID
  25. ```python
  26. from keycloak import KeycloakOpenID
  27. # Configure client
  28. keycloak_openid = KeycloakOpenID(server_url="http://localhost:8080/auth/",
  29. client_id="example_client",
  30. realm_name="example_realm",
  31. client_secret_key="secret")
  32. # Get WellKnown
  33. config_well_known = keycloak_openid.well_known()
  34. # Get Code With Oauth Authorization Request
  35. auth_url = keycloak_openid.auth_url(
  36. redirect_uri="your_call_back_url",
  37. scope="email",
  38. state="your_state_info")
  39. # Get Access Token With Code
  40. access_token = keycloak_openid.token(
  41. grant_type='authorization_code',
  42. code='the_code_you_get_from_auth_url_callback',
  43. redirect_uri="your_call_back_url")
  44. # Get Token
  45. token = keycloak_openid.token("user", "password")
  46. token = keycloak_openid.token("user", "password", totp="012345")
  47. # Get token using Token Exchange
  48. token = keycloak_openid.exchange_token(token['access_token'], "my_client", "other_client", "some_user")
  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. ```
  56. ## Example of Using Keycloak Admin API
  57. ```python
  58. from keycloak import KeycloakAdmin
  59. from keycloak import KeycloakOpenIDConnection
  60. keycloak_connection = KeycloakOpenIDConnection(
  61. server_url="http://localhost:8080/",
  62. username='example-admin',
  63. password='secret',
  64. realm_name="master",
  65. user_realm_name="only_if_other_realm_than_master",
  66. client_id="my_client",
  67. client_secret_key="client-secret",
  68. verify=True)
  69. keycloak_admin = KeycloakAdmin(connection=keycloak_connection)
  70. # Add user
  71. new_user = keycloak_admin.create_user({"email": "example@example.com",
  72. "username": "example@example.com",
  73. "enabled": True,
  74. "firstName": "Example",
  75. "lastName": "Example"})
  76. # Add user and raise exception if username already exists
  77. # exist_ok currently defaults to True for backwards compatibility reasons
  78. new_user = keycloak_admin.create_user({"email": "example@example.com",
  79. "username": "example@example.com",
  80. "enabled": True,
  81. "firstName": "Example",
  82. "lastName": "Example"},
  83. exist_ok=False)
  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. ```
  92. For more details, see the documentation available on [readthedocs](http://python-keycloak.readthedocs.io).