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.

195 lines
6.3 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
  1. .. python-keycloak documentation master file, created by
  2. sphinx-quickstart on Tue Aug 15 11:02:59 2017.
  3. You can adapt this file completely to your liking, but it should at least
  4. contain the root `toctree` directive.
  5. .. toctree::
  6. :maxdepth: 2
  7. :caption: Contents:
  8. Indices and tables
  9. ==================
  10. * :ref:`genindex`
  11. * :ref:`modindex`
  12. * :ref:`search`
  13. .. image:: https://readthedocs.org/projects/python-keycloak/badge/?version=latest
  14. :target: http://python-keycloak.readthedocs.io/en/latest/?badge=latest
  15. Welcome to python-keycloak's documentation!
  16. ===========================================
  17. **python-keycloak** is a Python package providing access to the Keycloak API.
  18. Installation
  19. ==================
  20. Via Pypi Package::
  21. $ pip install python-keycloak
  22. Manually::
  23. $ python setup.py install
  24. Dependencies
  25. ==================
  26. python-keycloak depends on:
  27. * Python 3
  28. * `requests <http://docs.python-requests.org/en/master/>`_
  29. * `python-jose <http://python-jose.readthedocs.io/en/latest/>`_
  30. Tests Dependencies
  31. ------------------
  32. * unittest
  33. * `httmock <https://github.com/patrys/httmock>`_
  34. Bug reports
  35. ==================
  36. Please report bugs and feature requests at
  37. `https://bitbucket.org/agriness/python-keycloak/issues <https://bitbucket.org/agriness/python-keycloak/issues>`_
  38. Documentation
  39. ==================
  40. The documentation for python-keycloak is available on `readthedocs <http://python-keycloak.readthedocs.io>`_.
  41. Contributors
  42. ==================
  43. * `Agriness Team <http://www.agriness.com/pt/>`_
  44. * `Martin Devlin <martin.devlin@pearson.com>`_
  45. * `Shon T. Urbas <shon.urbas@gmail.com>`_
  46. Usage
  47. =====
  48. Main methods::
  49. from keycloak import KeycloakOpenID
  50. # Configure client
  51. keycloak_openid = KeycloakOpenID(server_url="http://localhost:8080/auth/",
  52. client_id="example_client",
  53. realm_name="example_realm",
  54. client_secret_key="secret",
  55. verify=True)
  56. # Get WellKnow
  57. config_well_know = keycloak_openid.well_know()
  58. # Get Token
  59. token = keycloak_openid.token("user", "password")
  60. # Get Userinfo
  61. userinfo = keycloak_openid.userinfo(token['access_token'])
  62. # Logout
  63. keycloak_openid.logout(token['refresh_token'])
  64. # Get Certs
  65. certs = keycloak_openid.certs()
  66. # Get RPT (Entitlement)
  67. token = keycloak_openid.token("user", "password")
  68. rpt = keycloak_openid.entitlement(token['access_token'], "resource_id")
  69. # Instropect RPT
  70. token_rpt_info = keycloak_openid.introspect(keycloak_openid.introspect(token['access_token'], rpt=rpt['rpt'],
  71. token_type_hint="requesting_party_token"))
  72. # Introspect Token
  73. token_info = keycloak_openid.introspect(token['access_token']))
  74. # Decode Token
  75. KEYCLOAK_PUBLIC_KEY = "secret"
  76. options = {"verify_signature": True, "verify_aud": True, "exp": True}
  77. token_info = keycloak_openid.decode_token(token['access_token'], key=KEYCLOAK_PUBLIC_KEY, options=options)
  78. # Get permissions by token
  79. token = keycloak_openid.token("user", "password")
  80. keycloak_openid.load_authorization_config("example-authz-config.json")
  81. policies = keycloak_openid.get_policies(token['access_token'], method_token_info='decode', key=KEYCLOAK_PUBLIC_KEY)
  82. permissions = keycloak_openid.get_permissions(token['access_token'], method_token_info='introspect')
  83. # KEYCLOAK ADMIN
  84. from keycloak import KeycloakAdmin
  85. keycloak_admin = KeycloakAdmin(server_url="http://localhost:8080/auth/",
  86. username='example-admin',
  87. password='secret',
  88. realm_name="example_realm",
  89. verify=True)
  90. # Add user
  91. new_user = keycloak_admin.create_user({"email": "example@example.com",
  92. "username": "example@example.com",
  93. "enabled": True,
  94. "firstName": "Example",
  95. "lastName": "Example",
  96. "realmRoles": ["user_default", ],
  97. "attributes": {"example": "1,2,3,3,"}})
  98. # User counter
  99. count_users = keycloak_admin.users_count()
  100. # Get users Returns a list of users, filtered according to query parameters
  101. users = keycloak_admin.get_users({})
  102. # Get User
  103. user = keycloak_admin.get_user("user-id-keycloak")
  104. # Update User
  105. response = keycloak_admin.update_user(user_id="user-id-keycloak",
  106. payload={'firstName': 'Example Update'})
  107. # Delete User
  108. response = keycloak_admin.delete_user(user_id="user-id-keycloak")
  109. # Get consents granted by the user
  110. consents = keycloak_admin.consents_user(user_id="user-id-keycloak")
  111. # Send User Action
  112. response = keycloak_admin.send_update_account(user_id="user-id-keycloak",
  113. payload=json.dumps(['UPDATE_PASSWORD']))
  114. # Send Verify Email
  115. response = keycloak_admin.send_verify_email(user_id="user-id-keycloak")
  116. # Get sessions associated with the user
  117. sessions = keycloak_admin.get_sessions(user_id="user-id-keycloak")
  118. # Get themes, social providers, auth providers, and event listeners available on this server
  119. server_info = keycloak_admin.get_server_info()
  120. # Get clients belonging to the realm Returns a list of clients belonging to the realm
  121. clients = keycloak_admin.get_clients()
  122. # Get client - id (not client-id) from client by name
  123. client_id=keycloak_admin.get_client_id("my-client")
  124. # Get representation of the client - id of client (not client-id)
  125. client_roles = keycloak_admin.get_client_role(client_id=client_id)
  126. # Get all roles for the client
  127. client_roles = keycloak_admin.get_client_role(client_id=client_id)
  128. # Create client role
  129. keycloak_admin.create_client_role(client_id, "test")
  130. # Get client role id from name
  131. role_id = keycloak_admin.get_client_role_id(client_id=client_id, role_name="test")
  132. # Get all roles for the realm or client
  133. realm_roles = keycloak_admin.get_roles()
  134. # Assign client role to user. Note that BOTH role_name and role_id appear to be required.
  135. keycloak_admin.assign_client_role(client_id=client_id, user_id=user_id, role_id=role_id, role_name="test")