Skip to content

Commit

Permalink
get access token via client_id and secret
Browse files Browse the repository at this point in the history
  • Loading branch information
Threated committed Nov 2, 2023
1 parent 5fb95f5 commit 8cebc0d
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions central/src/keycloak.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,29 @@ pub struct KeyCloakConfig {
}

async fn get_access_token(conf: &KeyCloakConfig) -> Result<String> {
#[derive(serde::Deserialize)]
struct Token {
access_token: String,
}
dbg!(CLIENT
.post(&format!(
"{}/realms/{}/protocol/openid-connect/token",
conf.keycloak_url, conf.keycloak_realm
))
.form(&json!({
"client_id": conf.keycloak_id,
"client_secret": conf.keycloak_secret,
"grant_type": "client_credentials"
}))
.send()
.await?)
.json::<Token>()
.await
.map(|t| t.access_token)
}

#[cfg(test)]
async fn get_access_token_via_admin_login(conf: &KeyCloakConfig) -> Result<String> {
#[derive(serde::Deserialize)]
struct Token {
access_token: String,
Expand All @@ -30,10 +53,8 @@ async fn get_access_token(conf: &KeyCloakConfig) -> Result<String> {
"{}/realms/{}/protocol/openid-connect/token",
conf.keycloak_url, conf.keycloak_realm
))
// TODO: Change to conf.keycloak_id and conf.keyclaok_secret
.form(&json!({
"client_id": "admin-cli",
// "client_secret": "admin",
"username": "admin",
"password": "admin",
"grant_type": "password"
Expand All @@ -53,7 +74,7 @@ async fn test_create_client() -> Result<()> {
keycloak_secret: "".to_owned(),
keycloak_realm: "master".to_owned(),
};
let token = get_access_token(&conf).await?;
let token = get_access_token_via_admin_login(&conf).await?;
dbg!(post_client(&token, "test", vec!["http://test.bk".into()], &conf).await?);
Ok(())
}
Expand Down

0 comments on commit 8cebc0d

Please sign in to comment.