Observed since 20 Oct, 18:15 UTC
No superseded metadata states collected. Unchanged observations do not create new revisions.
ackc
API Client for Keycloak
A comprehensive Python client library for Keycloak Admin REST API, providing a clean and typed interface for managing Keycloak resources.
The only dependencies are niquests for HTTP and attrs for data models, making it lightweight and easy to integrate.
ACKC is a fully-typed Python library that wraps Keycloak's Admin REST API.
It provides both synchronous and asynchronous interfaces for all major Keycloak administrative operations, with a focus on developer experience, type safety, and efficiency.
The author of this package was also a little fed up with the usual daily slog of CLI login and token acquisition before getting to work, so this library aims to make that process as painless as possible.
attrs models for all requests and responsesniquests library)openapi-python-clientuv is recommended, but you can also use other package managers like pip.
uv add ackc
Standalone tool installation is also available:
uv tool install --python 3.13 ackc
from ackc import KeycloakClient
client = KeycloakClient(
server_url="https://keycloak.example.com",
client_id="admin-cli",
client_secret="your-secret",
realm="my-realm", # Default realm for API calls
auth_realm="master", # Default realm for client authentication
)
with client:
users = client.users.get_all()
realms = client.realms.get_all()
async def main():
async with client:
await client.users.aget_all()
await client.realms.aget_all()
ACKC supports multiple authentication flows:
client = KeycloakClient(
server_url="https://keycloak.example.com",
client_id="admin-cli",
client_secret="secret"
)
users = client.users.get_all()
client = KeycloakClient(
server_url="https://keycloak.example.com",
client_id="my-client",
client_secret="secret"
)
token = client.get_token_password(
username="admin",
password="admin",
scopes=["openid", "profile", "email"]
)
client = KeycloakClient(
server_url="https://keycloak.example.com",
client_id="cli-client"
)
def device_callback(*, verification_uri, user_code, expires_in):
print(f"Please visit: {verification_uri}")
print(f"User code: {user_code}")
print(f"You have {expires_in} seconds to authorize")
token = client.get_token_device(
scopes=["openid", "offline_access"],
callback=device_callback
)
ACKC provides methods for validating and working with JWTs:
claims = KeycloakClient.jwt_decode(jwt="your-jwt-token")
print(f"User: {claims.get('preferred_username')}")
print(f"Expires: {claims.get('exp')}")
needs_refresh = KeycloakClient.jwt_needs_refresh(jwt="your-jwt-token", buffer_seconds=300)
client = KeycloakClient(...)
user_info = client.jwt_userinfo(jwt="your-jwt-token")
token_info = client.jwt_introspect(jwt="your-jwt-token")
if token_info.get("active"):
print(f"Token is valid for user: {token_info.get('username')}")
new_token = client.jwt_refresh(refresh_token="your-refresh-token")
All API methods have async equivalents with the a prefix, allowing for non-blocking operations:
import asyncio
from ackc import KeycloakClient
async def main():
client = KeycloakClient(
server_url="https://keycloak.example.com",
client_id="admin-cli",
client_secret="secret"
)
async with client:
users = await client.users.aget_all()
realms = await client.realms.aget_all()
roles = await client.roles.aget_all()
asyncio.run(main())
ACKC includes helpful CLI tools:
Acquire an access token for Keycloak using client, password, or device code flows.
Also supports 2FA for password grant using the --otp/--otp-code options.
auth-token --server https://keycloak.example.com --client admin-cli
Export a realm and associated data to JSON.
auth-realm-export my-realm
Get health status or dump Keycloak prometheus metrics.
Requires KC_HEALTH_ENABLED or KC_METRICS_ENABLED to be set in Keycloak.
auth-mc --url http://localhost:9000 --json metrics
Creates Keycloak Docker compose.yaml and .env files in the current directory for development.
ackc-init
# Use with Cloudflare Access (+ Tunnel = HTTPS for local development or secure remote management)
# Note: This gets you past Cloudflare, but you still need to authenticate with Keycloak.
client = KeycloakClient(
server_url="https://keycloak.example.com",
cf_client_id='<your-cf-client-id>.access', # or CF_ACCESS_CLIENT_ID
cf_client_secret='your-cf-secret', # or CF_ACCESS_CLIENT_SECRET
)
# Initialize client for custom realm
client = KeycloakClient(server_url="...", realm="my-realm")
# Override realm for specific calls
users = client.users.get_all(realm="other-realm")
# Use a different realm for API client authentication (master by default).
# Recommended for backend production clients to maintain least privilege - the admin client should not have access to all realms.
company_realm = "my-company-realm"
client = KeycloakClient(server_url="...", auth_realm=company_realm, realm=company_realm)
(Just don't do this)
from ackc import KeycloakClient, AuthError
try:
with KeycloakClient(...) as client:
users = client.users.get_all()
except AuthError as e:
print(f"Authentication failed: {e}")
except Exception as e:
print(f"API error: {e}")
To update the generated code when Keycloak API changes:
python gen/generate_client.py --download
This project is licensed under the Apache License 2.0. See the license file for details.
Contributions are welcome! Please read the contributing guidelines for details on how to contribute to this project.
ACKC organizes Keycloak's functionality into logical API modules:
client.users)Manage users, credentials, roles, and user sessions.
Keycloak Documentation: User Management
client.realms)Configure realms, realm settings, and realm-level operations.
Keycloak Documentation: Realms
client.clients)Manage OAuth2/OIDC clients and their configurations.
Keycloak Documentation: Clients
client.roles)Define and manage realm and client roles.
client.groups)Organize users into groups with hierarchical structures.
Keycloak Documentation: Groups
client.identity_providers)Configure external identity providers for federation.
Keycloak Documentation: Identity Providers
client.authentication)Customize authentication flows and requirements.
Keycloak Documentation: Authentication
client.authorization)Fine-grained authorization using Keycloak Authorization Services.
Keycloak Documentation: Authorization Services
client.client_scopes)Manage reusable scope configurations for clients.
Keycloak Documentation: Client Scopes
client.protocol_mappers)Configure how tokens and assertions are populated.
Keycloak Documentation: Protocol Mappers
client.components)Manage pluggable components like user storage providers.
Keycloak Documentation: User Storage
client.sessions)Monitor and manage active user and client sessions.
Keycloak Documentation: Sessions
client.events)Access and configure audit and admin events.
Keycloak Documentation: Events
client.keys)Manage realm cryptographic keys.
client.organizations)Manage organizations (Keycloak 25+).
Keycloak Documentation: Organizations
client.scope_mappings)Manage client and realm scope mappings for users and groups.
Keycloak Documentation: Role Mappings
client.client_role_mappings)Manage client-specific role assignments.
Keycloak Documentation: Client Roles
client.role_mapper)Manage realm-level role assignments.
Keycloak Documentation: Realm Roles
client.roles_by_id)Manage roles using their unique IDs.
Keycloak Documentation: Role Management
client.attack_detection)Manage brute force attack detection.
Keycloak Documentation: Attack Detection
client.client_initial_access)Manage initial access tokens for dynamic client registration.
Keycloak Documentation: Client Registration
client.client_attribute_certificate)Manage client certificates and keystores.
Keycloak Documentation: Client Certificates
client.client_registration_policy)Manage policies for dynamic client registration.
Keycloak Documentation: Client Registration Policies
| API Module | Endpoints | Coverage | Status |
|---|---|---|---|
| Users | 33 | 100% | Full CRUD, groups, sessions, credentials, consents, federated identity, profile management |
| Realms | 44 | 100% | Full CRUD, events, admin events, default groups, client scopes, partial import/export |
| Clients | 34 | 100% | Full CRUD, sessions, scopes, revocation, registration tokens |
| Roles | 27 | 100% | Full CRUD, composites, client roles, users/groups with role |
| Groups | 11 | 100% | Full CRUD, members, children, count |
| Identity Providers | 17 | 100% | Full CRUD, mappers, import/export, mapper types |
| Authentication | 39 | 100% | Flows, executions, required actions, configurations |
| Authorization | 31 | 100% | Resource server, resources, scopes, policies, permissions |
| Client Scopes | 5 | 100% | Full CRUD operations (excluding 5 deprecated template endpoints) |
| Protocol Mappers | 14 | 100% | Full mapper operations (excluding 7 deprecated template endpoints) |
| Components | 6 | 100% | Component management and sub-types |
| Sessions | 5 | 100% | Session management for realms, clients, users |
| Events | 6 | 100% | User events, admin events, configuration |
| Keys | 1 | 100% | Realm key management |
| Organizations | 19 | 100% | Full organization management (Keycloak 25+) |
| Scope Mappings | 22 | 100% | Realm and client scope mappings for users/groups (excluding 11 deprecated templates) |
| Client Role Mappings | 10 | 100% | User and group client role assignments and available roles |
| Role Mapper | 12 | 100% | User and group realm role assignments and effective roles |
| Roles by ID | 10 | 100% | Role operations by ID, composite management, cross-realm operations |
| Attack Detection | 3 | 100% | Brute force detection status and flag management |
| Client Initial Access | 3 | 100% | Initial access tokens for dynamic client registration |
| Client Attribute Certificate | 6 | 100% | Certificate generation, upload, keystore management |
| Client Registration Policy | 1 | 100% | Registration policy provider configuration |
Download counts and popularity will appear here as the collector receives them.
| Package / target | Requirement | Relationship |
|---|---|---|
attrsEnvironment{
"extras": "",
"marker": null,
"requires_python": ">=3.13",
"evaluation": "NOT_EVALUATED"
} | attrs>=25.3.0 | runtime |
niquestsEnvironment{
"extras": "",
"marker": null,
"requires_python": ">=3.13",
"evaluation": "NOT_EVALUATED"
} | niquests>=3.14.1 | runtime |
bdist_wheel · 775.22 KB · Python py3
sdist · 266.73 KB · Python source
Observed since 20 Oct, 18:15 UTC
No superseded metadata states collected. Unchanged observations do not create new revisions.
package_version:863e6fae237003d4c0b28b50f9f03afbc3cd8b4a02fdbffe752805b16e9e31dd