Skip to main content

python-keycloak is a Python package providing access to the Keycloak API.

Project description

CircleCI Documentation Status

Python Keycloak

python-keycloak is a Python package providing access to the Keycloak API.

Installation

Install via PyPI:

$ pip install python-keycloak

Bug reports

Please report bugs and feature requests at https://github.com/marcospereirampj/python-keycloak/issues

Documentation

The documentation for python-keycloak is available on readthedocs.

Example of Using Keycloak OpenID

from keycloak import KeycloakOpenID

# Configure client
keycloak_openid = KeycloakOpenID(server_url="http://localhost:8080/auth/",
                                 client_id="example_client",
                                 realm_name="example_realm",
                                 client_secret_key="secret")

# Get WellKnown
config_well_known = keycloak_openid.well_known()

# Get Code With Oauth Authorization Request
auth_url = keycloak_openid.auth_url(
    redirect_uri="your_call_back_url",
    scope="email",
    state="your_state_info")

# Get Access Token With Code
access_token = keycloak_openid.token(
    grant_type='authorization_code',
    code='the_code_you_get_from_auth_url_callback',
    redirect_uri="your_call_back_url")


# Get Token
token = keycloak_openid.token("user", "password")
token = keycloak_openid.token("user", "password", totp="012345")

# Get token using Token Exchange
token = keycloak_openid.exchange_token(token['access_token'], "my_client", "other_client", "some_user")

# Get Userinfo
userinfo = keycloak_openid.userinfo(token['access_token'])

# Refresh token
token = keycloak_openid.refresh_token(token['refresh_token'])

# Logout
keycloak_openid.logout(token['refresh_token'])

Example of Using Keycloak Admin API

from keycloak import KeycloakAdmin
from keycloak import KeycloakOpenIDConnection

keycloak_connection = KeycloakOpenIDConnection(
                        server_url="http://localhost:8080/",
                        username='example-admin',
                        password='secret',
                        realm_name="master",
                        user_realm_name="only_if_other_realm_than_master",
                        client_id="my_client",
                        client_secret_key="client-secret",
                        verify=True)

keycloak_admin = KeycloakAdmin(connection=keycloak_connection)

# Add user
new_user = keycloak_admin.create_user({"email": "example@example.com",
                                       "username": "example@example.com",
                                       "enabled": True,
                                       "firstName": "Example",
                                       "lastName": "Example"})

# Add user and raise exception if username already exists
# exist_ok currently defaults to True for backwards compatibility reasons
new_user = keycloak_admin.create_user({"email": "example@example.com",
                                       "username": "example@example.com",
                                       "enabled": True,
                                       "firstName": "Example",
                                       "lastName": "Example"},
                                      exist_ok=False)

# Add user and set password
new_user = keycloak_admin.create_user({"email": "example@example.com",
                                       "username": "example@example.com",
                                       "enabled": True,
                                       "firstName": "Example",
                                       "lastName": "Example",
                    "credentials": [{"value": "secret","type": "password",}]})

For more details, see the documentation available on readthedocs.

Project details


Release history Release notifications | RSS feed

This version

3.9.3

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python_keycloak-3.9.3.tar.gz (44.3 kB view details)

Uploaded Source

Built Distribution

python_keycloak-3.9.3-py3-none-any.whl (58.8 kB view details)

Uploaded Python 3

File details

Details for the file python_keycloak-3.9.3.tar.gz.

File metadata

  • Download URL: python_keycloak-3.9.3.tar.gz
  • Upload date:
  • Size: 44.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.2

File hashes

Hashes for python_keycloak-3.9.3.tar.gz
Algorithm Hash digest
SHA256 9aacd7d64d565f9f9d38e7fe0a25f610e44a29c22b28faaa602f2c9269e5ba1c
MD5 1350050f670387f5c5757b799a13397f
BLAKE2b-256 0d1c9c16c53eac3772437ddfc920c76b833af0a0e9f147befd41d1fab1dc13cd

See more details on using hashes here.

File details

Details for the file python_keycloak-3.9.3-py3-none-any.whl.

File metadata

File hashes

Hashes for python_keycloak-3.9.3-py3-none-any.whl
Algorithm Hash digest
SHA256 613abb29d68a02086fde33779e43741b5a30a8203ee7cfef7fba81181ae754ab
MD5 440d90b15f2cb9afe5c26dac659ade5a
BLAKE2b-256 fc907876ba646479da895f0de95038b47c9e8b389e441c81263b26be2be2d32e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page