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.

112 lines
4.0 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
10 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
10 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. ## Example of Using Keycloak OpenID
  14. ```python
  15. from keycloak import KeycloakOpenID
  16. # Configure client
  17. keycloak_openid = KeycloakOpenID(server_url="http://localhost:8080/auth/",
  18. client_id="example_client",
  19. realm_name="example_realm",
  20. client_secret_key="secret")
  21. # Get WellKnown
  22. config_well_known = keycloak_openid.well_known()
  23. # Get Code With Oauth Authorization Request
  24. auth_url = keycloak_openid.auth_url(
  25. redirect_uri="your_call_back_url",
  26. scope="email",
  27. state="your_state_info")
  28. # Get Access Token With Code
  29. access_token = keycloak_openid.token(
  30. grant_type='authorization_code',
  31. code='the_code_you_get_from_auth_url_callback',
  32. redirect_uri="your_call_back_url")
  33. # Get Token
  34. token = keycloak_openid.token("user", "password")
  35. token = keycloak_openid.token("user", "password", totp="012345")
  36. # Get token using Token Exchange
  37. token = keycloak_openid.exchange_token(token['access_token'], "my_client", "other_client", "some_user")
  38. # Get Userinfo
  39. userinfo = keycloak_openid.userinfo(token['access_token'])
  40. # Refresh token
  41. token = keycloak_openid.refresh_token(token['refresh_token'])
  42. # Logout
  43. keycloak_openid.logout(token['refresh_token'])
  44. ```
  45. ## Example of Using Keycloak Admin API
  46. ```python
  47. from keycloak import KeycloakAdmin
  48. from keycloak import KeycloakOpenIDConnection
  49. keycloak_connection = KeycloakOpenIDConnection(
  50. server_url="http://localhost:8080/",
  51. username='example-admin',
  52. password='secret',
  53. realm_name="master",
  54. user_realm_name="only_if_other_realm_than_master",
  55. client_id="my_client",
  56. client_secret_key="client-secret",
  57. verify=True)
  58. keycloak_admin = KeycloakAdmin(connection=keycloak_connection)
  59. # Add user
  60. new_user = keycloak_admin.create_user({"email": "example@example.com",
  61. "username": "example@example.com",
  62. "enabled": True,
  63. "firstName": "Example",
  64. "lastName": "Example"})
  65. # Add user and raise exception if username already exists
  66. # exist_ok currently defaults to True for backwards compatibility reasons
  67. new_user = keycloak_admin.create_user({"email": "example@example.com",
  68. "username": "example@example.com",
  69. "enabled": True,
  70. "firstName": "Example",
  71. "lastName": "Example"},
  72. exist_ok=False)
  73. # Add user and set password
  74. new_user = keycloak_admin.create_user({"email": "example@example.com",
  75. "username": "example@example.com",
  76. "enabled": True,
  77. "firstName": "Example",
  78. "lastName": "Example",
  79. "credentials": [{"value": "secret","type": "password",}]})
  80. ```
  81. For more details, see the documentation available on [readthedocs](http://python-keycloak.readthedocs.io).