Skip to main content

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.

Keycloak version support

The library strives to always support Keycloak's latest version. Additionally to that, we also support 5 latest major versions of Keycloak, in order to give our user base more time for smoother upgrades.

Current list of supported Keycloak versions:

  • 26.X
  • 25.X
  • 24.X
  • 23.X
  • 22.X

Python version support

We only support Python versions that have active or security support by the Python Software Foundation. You can find the list of active Python versions here.

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.

Note about uploading manually

This package is not managed through the pipeline so, building manually and uploading via twine is required.

First ensure poetry is installed

python -m pip install poetry

or via system package manager

Second install dependencies

  poetry install

Next run the build

poetry run tox -e build

Finally upload to pypi via twine

poetry run twine upload -u __token -p $API_TOKEN dist/*

Metadata

Release files for ct-keycloak 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ct-keycloak 1.0.1
File Size Uploaded
ct_keycloak-1.0.1.tar.gz 79.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ct-keycloak 1.0.1
File Interpreter ABI Platform
ct_keycloak-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 166.9 kB

Release files / ct_keycloak-1.0.1.tar.gz

Download URL ct_keycloak-1.0.1.tar.gz
Size 79.2 kB
Tags Source
SHA-256 checksum
How to use checksums
3ac50be4d1c43e6ff82e5c92efaf95dd8bebcd03567e1308f4bcf073f31a2a26
BLAKE2b-256 checksum
How to use checksums
76d41da79dd9e29f3fda348df9f44a47f609c98d9125139d08c26c264a2a1f8e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.7

Release files / ct_keycloak-1.0.1-py3-none-any.whl

Download URL ct_keycloak-1.0.1-py3-none-any.whl
Size 87.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1d015f587e809a86da928f151461be93a618ad193c0c8319ebcb8b449d61e30b
BLAKE2b-256 checksum
How to use checksums
954c6e27cc126cbd954cca4962e909c9f9ef4c4578049bb6f678d2f16c61dc3a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.7

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page