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