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.

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