How kilork/keycloak works
Purpose and Public Entry Points
This is a Rust client library for Keycloak Admin REST API v26.6.0. Consumers construct KeycloakAdmin, acquire or supply an admin token, then navigate typed resource scopes and invoke async REST operations. Public API assembly and exports are centered in src/lib.rs and src/prelude.rs; shared API representations live in src/types.rs.
Client Construction and Authentication
KeycloakAdmin is the root client, initialized with a Keycloak base URL, an access token, and a caller-owned reqwest client. Token acquisition is part of the library’s public flow via KeycloakAdminToken; callers can also manage token lifecycle externally. Client construction and request setup are defined in src/builder.rs, src/lib.rs, and src/rest/mod.rs.
Resource-Oriented API Surface
Resource modules expose ergonomic, scoped methods corresponding to Keycloak Admin API tag groups. They derive sub-resource URLs from the root or parent scope—for example realms, users, clients, roles, and identity providers. Key paths include src/resource/mod.rs, src/resource/realms_admin.rs, and src/resource/users.rs. Feature flags can compile all resource groups by default or restrict the exposed groups.
Generated REST Operation Layer
Generated operation implementations map typed methods to Keycloak endpoints, request parameters, bodies, and response types. This is the principal API-contract layer generated from the OpenAPI specification, with one module per resource group, such as src/rest/generated_rest/mod.rs, src/rest/generated_rest/users.rs, and src/rest/generated_rest/clients.rs. Resource-facing methods delegate into this layer rather than hand-writing endpoint logic.
Request/Response Boundary
The REST layer serializes src/types.rs representations, builds URLs and query parameters, authenticates requests, and converts HTTP results into typed responses or crate errors. Shared response handling and URL escaping are isolated in src/rest/default_response.rs, src/rest/manual_rest.rs, and src/rest/url_enc.rs. Error normalization is defined in src/error.rs.
Code Generation and API Contract Updates
api/openapi.json is the checked-in Keycloak OpenAPI contract. update.ts downloads/updates the contract and regenerates typed models, generated REST methods, and resource modules; generation templates are in templates/src/lib.rs. This makes the OpenAPI specification the source boundary for much of src/types.rs, src/rest/generated_rest/, and src/resource/.
Runtime and Integration Environment
The crate requires Rust 1.87+ and async execution supplied by the consumer (the README example uses Tokio). HTTP transport is reqwest, with selectable 0.12/0.13 feature variants and optional multipart support. Docker-based Keycloak integration infrastructure is provided through docker-compose.yml and docker/Dockerfile; CI integration execution is configured at .github/workflows/integration.yml.
Main entry points: generated_rest/mod.rs Generated operations · update.ts Contract updater · lib.rs Crate exports · rest/mod.rs Request setup
Components 19
Public Rust API
- Crate exports
src/lib.rs - Prelude
src/prelude.rs - KeycloakAdmin · Owns base URL and access token state.
src/builder.rs - KeycloakAdminToken · Acquires admin tokens or supports caller-managed lifecycle.
src/lib.rs - Typed API models
src/types.rs
Resource Scopes
- Resource navigation · Derives scoped URLs from the root client.
src/resource/mod.rs - Realm scopes
src/resource/realms_admin.rs - User scopes
src/resource/users.rs
REST Execution
- Generated operations · Typed endpoint methods generated per Keycloak resource group.
src/rest/generated_rest/mod.rs - User operations
src/rest/generated_rest/users.rs - Client operations
src/rest/generated_rest/clients.rs - Request setup · Builds authenticated requests using reqwest.
src/rest/mod.rs - Response handling · Normalizes HTTP results into typed responses or crate errors.
src/rest/default_response.rs - Crate errors
src/error.rs
Contract Generation
- Keycloak OpenAPI contract · Checked-in source contract for generated API surface.
api/openapi.json - Contract updater · Downloads the contract and regenerates models, operations, and resources.
update.ts - Generation templates
templates/src/lib.rs
Other components
- Consumer async runtime · Supplies async execution and a reqwest client.
- Keycloak Admin API
Connections 24
- Consumer async runtime → KeycloakAdmin: constructs
- Crate exports → Prelude: exports
- Crate exports → KeycloakAdmin: exports
- Crate exports → KeycloakAdminToken: exports
- Crate exports → Typed API models: exports
- KeycloakAdminToken → KeycloakAdmin: provides token
- KeycloakAdmin → Resource navigation: navigates
- Resource navigation → Realm scopes: scopes
- Resource navigation → User scopes: scopes
- Realm scopes → Generated operations: delegates operations
- User scopes → User operations: delegates operations
- Generated operations → User operations: includes
- Generated operations → Client operations: includes
- Generated operations → Request setup: builds requests
- Request setup → Typed API models: serializes models
- Request setup → Response handling: sends HTTP results
- Response handling → Typed API models: deserializes responses
- Response handling → Crate errors: normalizes failures
- Request setup → Keycloak Admin API: Admin REST calls
- Keycloak OpenAPI contract → Contract updater: source contract
- Generation templates → Contract updater: templates
- Contract updater → Typed API models: regenerates
- Contract updater → Generated operations: regenerates
- Contract updater → Resource navigation: regenerates