37 stable releases (16 major)

new 24.0.300 Apr 22, 2024
24.0.200 Mar 30, 2024
23.0.400 Mar 29, 2024
22.0.501 Mar 29, 2024
0.2.2 Mar 12, 2020

#2 in #rest-client

Download history 1237/week @ 2024-01-03 1119/week @ 2024-01-10 1382/week @ 2024-01-17 945/week @ 2024-01-24 1401/week @ 2024-01-31 1305/week @ 2024-02-07 1121/week @ 2024-02-14 1116/week @ 2024-02-21 1004/week @ 2024-02-28 1444/week @ 2024-03-06 1375/week @ 2024-03-13 1136/week @ 2024-03-20 1897/week @ 2024-03-27 1330/week @ 2024-04-03 897/week @ 2024-04-10 840/week @ 2024-04-17

5,227 downloads per month
Used in 5 crates (2 directly)

Unlicense OR MIT

495KB
9K SLoC

Rust 7.5K SLoC // 0.0% comments TypeScript 1K SLoC

Keycloak Admin REST API

Dual-licensed under MIT or the UNLICENSE.

Features

Implements Keycloak Admin REST API version 24.0.3.

Feature flags

Default flags: tags-all.

  • rc: use Arc for deserialization.
  • schemars: add schemars support.
  • tags-all: activate all tags (resource groups) in REST API, it is default behavior. Disable default features and use individual tag-xxx features to activate only required resource groups. For a full list reference the Cargo.toml.

Usage

Requires Rust version >= 1.74.0.

Add dependency to Cargo.toml:

[dependencies]
keycloak = "24.0"
use keycloak::{
    types::*,
    {KeycloakAdmin, KeycloakAdminToken},
};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let url = std::env::var("KEYCLOAK_ADDR").unwrap_or_else(|_| "http://localhost:8080".into());
    let user = std::env::var("KEYCLOAK_USER").unwrap_or_else(|_| "admin".into());
    let password = std::env::var("KEYCLOAK_PASSWORD").unwrap_or_else(|_| "password".into());

    let client = reqwest::Client::new();
    let admin_token = KeycloakAdminToken::acquire(&url, &user, &password, &client).await?;

    eprintln!("{:?}", admin_token);

    let admin = KeycloakAdmin::new(&url, admin_token, client);

    admin
        .post(RealmRepresentation {
            realm: Some("test".into()),
            ..Default::default()
        })
        .await?;

    admin
        .realm_users_post(
            "test",
            UserRepresentation {
                username: Some("user".into()),
                ..Default::default()
            },
        )
        .await?;

    let users = admin
        .realm_users_get(
            "test", None, None, None, None, None, None, None, None, None, None, None, None, None,
            None,
        )
        .await?;

    eprintln!("{:?}", users);

    let id = users
        .iter()
        .find(|u| u.username == Some("user".into()))
        .unwrap()
        .id
        .as_ref()
        .unwrap()
        .to_string();

    admin
        .realm_users_with_user_id_delete("test", id.as_str())
        .await?;

    admin.realm_delete("test").await?;

    Ok(())
}

Version agreement

If we have x.y.z version of keycloak, our package version would be x.y.(z * 100 + v) there v is a minor fix version to official x.y.z version.

Example: official version 13.0.1 is 13.0.100 for crate version. 13.0.102 means keycloak version 13.0.1 and minor fix version 2.

Update

To update current version use provided update.ts deno script:

deno run --allow-env=KEYCLOAK_RUST_VERSION,KEYCLOAK_VERSION,KEYCLOAK_RUST_MAJOR_VERSION --allow-read=Cargo.toml --allow-write=Cargo.toml,api/openapi.json,src/types.rs,src/rest/generated_rest.rs --allow-net=keycloak.org,www.keycloak.org --allow-run=cargo,gh,git,handlebars-magic update.ts

Dependencies

~5–18MB
~251K SLoC