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.

172 lines
5.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
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. * [Martin Devlin](martin.devlin@pearson.com)
  26. * [Shon T. Urbas](shon.urbas@gmail.com>)
  27. ## Usage
  28. ```python
  29. from keycloak import KeycloakOpenID
  30. # Configure client
  31. keycloak_openid = KeycloakOpenID(server_url="http://localhost:8080/auth/",
  32. client_id="example_client",
  33. realm_name="example_realm",
  34. client_secret_key="secret",
  35. verify=True)
  36. # Get WellKnow
  37. config_well_know = keycloak_openid.well_know()
  38. # Get Token
  39. token = keycloak_openid.token("user", "password")
  40. # Get Userinfo
  41. userinfo = keycloak_openid.userinfo(token['access_token'])
  42. # Logout
  43. keycloak_openid.logout(token['refresh_token'])
  44. # Get Certs
  45. certs = keycloak_openid.certs()
  46. # Get RPT (Entitlement)
  47. token = keycloak_openid.token("user", "password")
  48. rpt = keycloak_openid.entitlement(token['access_token'], "resource_id")
  49. # Instropect RPT
  50. token_rpt_info = keycloak_openid.introspect(keycloak_openid.introspect(token['access_token'], rpt=rpt['rpt'],
  51. token_type_hint="requesting_party_token"))
  52. # Introspect Token
  53. token_info = keycloak_openid.introspect(token['access_token']))
  54. # Decode Token
  55. KEYCLOAK_PUBLIC_KEY = "secret"
  56. options = {"verify_signature": True, "verify_aud": True, "exp": True}
  57. token_info = keycloak_openid.decode_token(token['access_token'], key=KEYCLOAK_PUBLIC_KEY, options=options)
  58. # Get permissions by token
  59. token = keycloak_openid.token("user", "password")
  60. keycloak_openid.load_authorization_config("example-authz-config.json")
  61. policies = keycloak_openid.get_policies(token['access_token'], method_token_info='decode', key=KEYCLOAK_PUBLIC_KEY)
  62. permissions = keycloak_openid.get_permissions(token['access_token'], method_token_info='introspect')
  63. # KEYCLOAK ADMIN
  64. from keycloak import KeycloakAdmin
  65. keycloak_admin = KeycloakAdmin(server_url="http://localhost:8080/auth/",
  66. username='example-admin',
  67. password='secret',
  68. realm_name="example_realm",
  69. verify=True)
  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. "realmRoles": ["user_default", ],
  77. "attributes": {"example": "1,2,3,3,"}})
  78. # User counter
  79. count_users = keycloak_admin.users_count()
  80. # Get users Returns a list of users, filtered according to query parameters
  81. users = keycloak_admin.get_users({})
  82. # Get user ID from name
  83. user-id-keycloak = keycloak_admin.get_user_id("example@example.com")
  84. # Get User
  85. user = keycloak_admin.get_user("user-id-keycloak")
  86. # Update User
  87. response = keycloak_admin.update_user(user_id="user-id-keycloak",
  88. payload={'firstName': 'Example Update'})
  89. # Delete User
  90. response = keycloak_admin.delete_user(user_id="user-id-keycloak")
  91. # Get consents granted by the user
  92. consents = keycloak_admin.consents_user(user_id="user-id-keycloak")
  93. # Send User Action
  94. response = keycloak_admin.send_update_account(user_id="user-id-keycloak",
  95. payload=json.dumps(['UPDATE_PASSWORD']))
  96. # Send Verify Email
  97. response = keycloak_admin.send_verify_email(user_id="user-id-keycloak")
  98. # Get sessions associated with the user
  99. sessions = keycloak_admin.get_sessions(user_id="user-id-keycloak")
  100. # Get themes, social providers, auth providers, and event listeners available on this server
  101. server_info = keycloak_admin.get_server_info()
  102. # Get clients belonging to the realm Returns a list of clients belonging to the realm
  103. clients = keycloak_admin.get_clients()
  104. # Get client - id (not client-id) from client by name
  105. client_id=keycloak_admin.get_client_id("my-client")
  106. # Get representation of the client - id of client (not client-id)
  107. client = keycloak_admin.get_client(client_id=client_id)
  108. # Get all roles for the client
  109. client_roles = keycloak_admin.get_client_role(client_id=client_id)
  110. # Create client role
  111. keycloak_admin.create_client_role(client_id, "test")
  112. # Get client role id from name
  113. role_id = keycloak_admin.get_client_role_id(client_id=client_id, role_name="test")
  114. # Get all roles for the realm or client
  115. realm_roles = keycloak_admin.get_roles()
  116. # Assign client role to user. Note that BOTH role_name and role_id appear to be required.
  117. keycloak_admin.assign_client_role(client_id=client_id, user_id=user_id, role_id=role_id, role_name="test")
  118. ```