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.

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