# kilork/keycloak architecture

> Architecture diagram of the GitHub repository kilork/keycloak, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-22). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/kilork/keycloak
- Repository: https://github.com/kilork/keycloak

## Overview

#### 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: [`src/rest/generated_rest/mod.rs`](https://github.com/kilork/keycloak/blob/master/src/rest/generated_rest/mod.rs) (Generated operations), [`update.ts`](https://github.com/kilork/keycloak/blob/master/update.ts) (Contract updater), [`src/lib.rs`](https://github.com/kilork/keycloak/blob/master/src/lib.rs) (Crate exports), [`src/rest/mod.rs`](https://github.com/kilork/keycloak/blob/master/src/rest/mod.rs) (Request setup)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_public["Public Rust API"]
  node_lib["Crate exports<br/>Rust API assembly<br/>[lib.rs]"]
  node_prelude["Prelude<br/>Rust exports<br/>[prelude.rs]"]
  node_client["KeycloakAdmin<br/>root client<br/>[builder.rs]"]
  node_token["KeycloakAdminToken<br/>authentication API<br/>[lib.rs]"]
  node_types["Typed API models<br/>Rust representations<br/>[types.rs]"]
end

subgraph group_resources["Resource Scopes"]
  node_resource_root["Resource navigation<br/>resource scopes<br/>[mod.rs]"]
  node_realms["Realm scopes<br/>resource group<br/>[realms_admin.rs]"]
  node_users_scope["User scopes<br/>resource group<br/>[users.rs]"]
end

subgraph group_rest["REST Execution"]
  node_generated_rest["Generated operations<br/>OpenAPI operation layer<br/>[mod.rs]"]
  node_user_operations["User operations<br/>generated endpoints<br/>[users.rs]"]
  node_client_operations["Client operations<br/>generated endpoints<br/>[clients.rs]"]
  node_request_setup["Request setup<br/>REST core<br/>[mod.rs]"]
  node_response_boundary["Response handling<br/>HTTP boundary"]
  node_errors["Crate errors<br/>error model<br/>[error.rs]"]
end

subgraph group_generation["Contract Generation"]
  node_openapi["Keycloak OpenAPI contract<br/>API specification<br/>[openapi.json]"]
  node_generator{{"Contract updater<br/>generation pipeline<br/>[update.ts]"}}
  node_templates["Generation templates<br/>code templates<br/>[lib.rs]"]
end

node_consumer(("Consumer async runtime"))
node_keycloak{{"Keycloak Admin API<br/>external service"}}

node_consumer -->|"constructs"| node_client
node_lib -->|"exports"| node_prelude
node_lib -->|"exports"| node_client
node_lib -->|"exports"| node_token
node_lib -->|"exports"| node_types
node_token -->|"provides token"| node_client
node_client -->|"navigates"| node_resource_root
node_resource_root -->|"scopes"| node_realms
node_resource_root -->|"scopes"| node_users_scope
node_realms -->|"delegates operations"| node_generated_rest
node_users_scope -->|"delegates operations"| node_user_operations
node_generated_rest -->|"includes"| node_user_operations
node_generated_rest -->|"includes"| node_client_operations
node_generated_rest -->|"builds requests"| node_request_setup
node_request_setup -->|"serializes models"| node_types
node_request_setup -->|"sends HTTP results"| node_response_boundary
node_response_boundary -->|"deserializes responses"| node_types
node_response_boundary -->|"normalizes failures"| node_errors
node_request_setup -->|"Admin REST calls"| node_keycloak
node_openapi -->|"source contract"| node_generator
node_templates -->|"templates"| node_generator
node_generator -.->|"regenerates"| node_types
node_generator -.->|"regenerates"| node_generated_rest
node_generator -.->|"regenerates"| node_resource_root

click node_lib "https://github.com/kilork/keycloak/blob/master/src/lib.rs"
click node_prelude "https://github.com/kilork/keycloak/blob/master/src/prelude.rs"
click node_client "https://github.com/kilork/keycloak/blob/master/src/builder.rs"
click node_token "https://github.com/kilork/keycloak/blob/master/src/lib.rs"
click node_types "https://github.com/kilork/keycloak/blob/master/src/types.rs"
click node_resource_root "https://github.com/kilork/keycloak/blob/master/src/resource/mod.rs"
click node_realms "https://github.com/kilork/keycloak/blob/master/src/resource/realms_admin.rs"
click node_users_scope "https://github.com/kilork/keycloak/blob/master/src/resource/users.rs"
click node_generated_rest "https://github.com/kilork/keycloak/blob/master/src/rest/generated_rest/mod.rs"
click node_user_operations "https://github.com/kilork/keycloak/blob/master/src/rest/generated_rest/users.rs"
click node_client_operations "https://github.com/kilork/keycloak/blob/master/src/rest/generated_rest/clients.rs"
click node_request_setup "https://github.com/kilork/keycloak/blob/master/src/rest/mod.rs"
click node_response_boundary "https://github.com/kilork/keycloak/blob/master/src/rest/default_response.rs"
click node_errors "https://github.com/kilork/keycloak/blob/master/src/error.rs"
click node_openapi "https://github.com/kilork/keycloak/blob/master/api/openapi.json"
click node_generator "https://github.com/kilork/keycloak/blob/master/update.ts"
click node_templates "https://github.com/kilork/keycloak/blob/master/templates/src/lib.rs"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_lib,node_prelude,node_client,node_token,node_types toneBlue
class node_resource_root,node_realms,node_users_scope toneAmber
class node_generated_rest,node_user_operations,node_client_operations,node_request_setup,node_response_boundary,node_errors toneMint
class node_openapi,node_generator,node_templates toneRose
class node_consumer,node_keycloak toneNeutral
```

## Components

### Public Rust API

- [Crate exports](https://github.com/kilork/keycloak/blob/master/src/lib.rs): Rust API assembly · `src/lib.rs`
- [Prelude](https://github.com/kilork/keycloak/blob/master/src/prelude.rs): Rust exports · `src/prelude.rs`
- [KeycloakAdmin](https://github.com/kilork/keycloak/blob/master/src/builder.rs): root client · Owns base URL and access token state. · `src/builder.rs`
- [KeycloakAdminToken](https://github.com/kilork/keycloak/blob/master/src/lib.rs): authentication API · Acquires admin tokens or supports caller-managed lifecycle. · `src/lib.rs`
- [Typed API models](https://github.com/kilork/keycloak/blob/master/src/types.rs): Rust representations · `src/types.rs`

### Resource Scopes

- [Resource navigation](https://github.com/kilork/keycloak/blob/master/src/resource/mod.rs): resource scopes · Derives scoped URLs from the root client. · `src/resource/mod.rs`
- [Realm scopes](https://github.com/kilork/keycloak/blob/master/src/resource/realms_admin.rs): resource group · `src/resource/realms_admin.rs`
- [User scopes](https://github.com/kilork/keycloak/blob/master/src/resource/users.rs): resource group · `src/resource/users.rs`

### REST Execution

- [Generated operations](https://github.com/kilork/keycloak/blob/master/src/rest/generated_rest/mod.rs): OpenAPI operation layer · Typed endpoint methods generated per Keycloak resource group. · `src/rest/generated_rest/mod.rs`
- [User operations](https://github.com/kilork/keycloak/blob/master/src/rest/generated_rest/users.rs): generated endpoints · `src/rest/generated_rest/users.rs`
- [Client operations](https://github.com/kilork/keycloak/blob/master/src/rest/generated_rest/clients.rs): generated endpoints · `src/rest/generated_rest/clients.rs`
- [Request setup](https://github.com/kilork/keycloak/blob/master/src/rest/mod.rs): REST core · Builds authenticated requests using reqwest. · `src/rest/mod.rs`
- [Response handling](https://github.com/kilork/keycloak/blob/master/src/rest/default_response.rs): HTTP boundary · Normalizes HTTP results into typed responses or crate errors. · `src/rest/default_response.rs`
- [Crate errors](https://github.com/kilork/keycloak/blob/master/src/error.rs): error model · `src/error.rs`

### Contract Generation

- [Keycloak OpenAPI contract](https://github.com/kilork/keycloak/blob/master/api/openapi.json): API specification · Checked-in source contract for generated API surface. · `api/openapi.json`
- [Contract updater](https://github.com/kilork/keycloak/blob/master/update.ts): generation pipeline · Downloads the contract and regenerates models, operations, and resources. · `update.ts`
- [Generation templates](https://github.com/kilork/keycloak/blob/master/templates/src/lib.rs): code templates · `templates/src/lib.rs`

### Other components

- **Consumer async runtime**: application · Supplies async execution and a reqwest client.
- **Keycloak Admin API**: external service

## Connections

- 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
