use clap::Parser;
use cosmian_kms_client::{KmsClientConfig, reexport::cosmian_http_client::LoginState};
use crate::error::{KmsCliError, result::KmsCliResult};
#[derive(Parser, Debug)]
#[clap(verbatim_doc_comment)]
pub struct LoginAction;
impl LoginAction {
#[expect(clippy::print_stdout)]
pub async fn process(&self, config: KmsClientConfig) -> KmsCliResult<String> {
let login_config = config.http_config.oauth2_conf.as_ref().ok_or_else(|| {
KmsCliError::Default(format!(
"The `login` command (only used for JWT authentication) requires an Identity \
Provider (IdP) that MUST be configured in the oauth2_conf object in {config:?}",
))
})?;
let state = LoginState::try_from(login_config.clone())?;
println!("Browse to: {}", state.auth_url);
let access_token = state.finalize().await?;
println!("\nSuccess! The access token was saved in the KMS configuration (in memory)");
Ok(access_token)
}
}