1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
/*!
# Keycloak Admin REST API
## Legal
Dual-licensed under `MIT` or the [UNLICENSE](http://unlicense.org/).
## Features
Implements [Keycloak Admin REST API version 17.0](https://www.keycloak.org/docs-api/17.0/rest-api/index.html).
## Usage
Requires Rust version >= `1.58.0`.
Add dependency to Cargo.toml:
```toml
[dependencies]
keycloak = "17.0"
```
If you are using new [Quarkus distribution of Keycloak](https://www.keycloak.org/migration/migrating-to-quarkus) enable `quarkus` feature:
```toml
[dependencies]
keycloak = { version = "17.0", features = [ "quarkus" ] }
```
```rust#ignore
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:9080".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_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`.
*/
pub mod types;
mod error;
mod rest;
pub use error::KeycloakError;
pub use rest::{
KeycloakAdmin, KeycloakAdminToken, KeycloakServiceAccountAdminTokenRetriever,
KeycloakTokenSupplier,
};