use crate::cli::output::print_header;
use crate::cli::{args::AuthArgs, auth_params::AuthParams};
use crate::config::cli_config::Profile;
use crate::services::authentication::authenticator::execute_flow;
use crate::services::authentication::client_credentials::ClientCredentialsFlow;
use dialoguer::{Password, theme::ColorfulTheme};
use miette::{IntoDiagnostic, Result};
pub async fn run(profile: Profile, args: AuthArgs, arg_secret: Option<String>) -> Result<()> {
let theme = ColorfulTheme::default();
print_header("OAuth2 Token Generator (M2M)");
let auth = AuthParams::new(&profile, args, "api://<client_id>/.default")?;
let client_secret = match arg_secret {
Some(s) => s,
None => Password::with_theme(&theme)
.with_prompt("Enter Client Secret")
.interact()
.into_diagnostic()?,
};
let flow = ClientCredentialsFlow {
provider: auth.provider,
client_id: auth.client_id,
client_secret,
scopes: auth.scopes,
};
execute_flow(&flow).await?;
Ok(())
}