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.

168 lines
5.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
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
  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. **python-keycloak** is a Python package providing access to the Keycloak API.
  5. ## Installation
  6. ### Via Pypi Package:
  7. ``` $ pip install python-keycloak ```
  8. ### Manually
  9. ``` $ python setup.py install ```
  10. ## Dependencies
  11. python-keycloak depends on:
  12. * Python 3
  13. * [requests](http://docs.python-requests.org/en/master/)
  14. * [python-jose](http://python-jose.readthedocs.io/en/latest/)
  15. ### Tests Dependencies
  16. * unittest
  17. * [httmock](https://github.com/patrys/httmock)
  18. ## Bug reports
  19. Please report bugs and feature requests at
  20. https://bitbucket.org/agriness/python-keycloak/issues
  21. ## Documentation
  22. The documentation for python-keycloak is available on [readthedocs](http://python-keycloak.readthedocs.io).
  23. ## Contributors
  24. * [Agriness Team](http://www.agriness.com/pt/)
  25. ## Usage
  26. ```python
  27. from keycloak import KeycloakOpenID
  28. # Configure client
  29. keycloak_openid = KeycloakOpenID(server_url="http://localhost:8080/auth/",
  30. client_id="example_client",
  31. realm_name="example_realm",
  32. client_secret_key="secret")
  33. # Get WellKnow
  34. config_well_know = keycloak_openid.well_know()
  35. # Get Token
  36. token = keycloak_openid.token("user", "password")
  37. # Get Userinfo
  38. userinfo = keycloak_openid.userinfo(token['access_token'])
  39. # Logout
  40. keycloak_openid.logout(token['refresh_token'])
  41. # Get Certs
  42. certs = keycloak_openid.certs()
  43. # Get RPT (Entitlement)
  44. token = keycloak_openid.token("user", "password")
  45. rpt = keycloak_openid.entitlement(token['access_token'], "resource_id")
  46. # Instropect RPT
  47. token_rpt_info = keycloak_openid.instropect(keycloak_openid.instropect(token['access_token'], rpt=rpt['rpt'],
  48. token_type_hint="requesting_party_token"))
  49. # Introspect Token
  50. token_info = keycloak_openid.introspect(token['access_token']))
  51. # Decode Token
  52. KEYCLOAK_PUBLIC_KEY = "secret"
  53. options = {"verify_signature": True, "verify_aud": True, "exp": True}
  54. token_info = keycloak_openid.decode_token(token['access_token'], key=KEYCLOAK_PUBLIC_KEY, options=options)
  55. # Get permissions by token
  56. token = keycloak_openid.token("user", "password")
  57. keycloak_openid.load_authorization_config("example-authz-config.json")
  58. policies = keycloak_openid.get_policies(token['access_token'], method_token_info='decode', key=KEYCLOAK_PUBLIC_KEY)
  59. permissions = keycloak_openid.get_permissions(token['access_token'], method_token_info='introspect')
  60. # KEYCLOAK ADMIN
  61. from keycloak import KeycloakAdmin
  62. keycloak_admin = KeycloakAdmin(server_url="http://localhost:8080/auth/",
  63. username='example-admin',
  64. password='secret',
  65. realm_name="example_realm")
  66. # Add user
  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. "realmRoles": ["user_default", ],
  73. "attributes": {"example": "1,2,3,3,"}})
  74. # User counter
  75. count_users = keycloak_admin.users_count()
  76. # Get users Returns a list of users, filtered according to query parameters
  77. users = keycloak_admin.get_users({})
  78. # Get user ID from name
  79. user-id-keycloak = keycloak_admin.get_user_id("example@example.com")
  80. # Get User
  81. user = keycloak_admin.get_user("user-id-keycloak")
  82. # Update User
  83. response = keycloak_admin.update_user(user_id="user-id-keycloak",
  84. payload={'firstName': 'Example Update'})
  85. # Delete User
  86. response = keycloak_admin.delete_user(user_id="user-id-keycloak")
  87. # Get consents granted by the user
  88. consents = keycloak_admin.consents_user(user_id="user-id-keycloak")
  89. # Send User Action
  90. response = keycloak_admin.send_update_account(user_id="user-id-keycloak",
  91. payload=json.dumps(['UPDATE_PASSWORD']))
  92. # Send Verify Email
  93. response = keycloak_admin.send_verify_email(user_id="user-id-keycloak")
  94. # Get sessions associated with the user
  95. sessions = keycloak_admin.get_sessions(user_id="user-id-keycloak")
  96. # Get themes, social providers, auth providers, and event listeners available on this server
  97. server_info = keycloak_admin.get_server_info()
  98. # Get clients belonging to the realm Returns a list of clients belonging to the realm
  99. clients = keycloak_admin.get_clients()
  100. # Get client - id (not client-id) from client by name
  101. client_id=keycloak_admin.get_client_id("my-client")
  102. # Get representation of the client - id of client (not client-id)
  103. client = keycloak_admin.get_client(client_id=client_id)
  104. # Get all roles for the client
  105. client_roles = keycloak_admin.get_client_role(client_id=client_id)
  106. # Create client role
  107. keycloak_admin.create_client_role(client_id, "test")
  108. # Get client role id from name
  109. role_id = keycloak_admin.get_client_role_id(client_id=client_id, role_name="test")
  110. # Get all roles for the realm or client
  111. realm_roles = keycloak_admin.get_roles()
  112. # Assign client role to user. Note that BOTH role_name and role_id appear to be required.
  113. keycloak_admin.assign_client_role(client_id=client_id, user_id=user_id, role_id=role_id, role_name="test")
  114. ```