Skip to content

Commit

Permalink
add authentication subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfv committed Jun 20, 2023
1 parent 26119fe commit b829437
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
60 changes: 60 additions & 0 deletions src/cli/auth.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use clap::Parser;
use rattler_networking::{Authentication, AuthenticationStorage};

// use crate::auth::{default_authentication_storage, listen_and_open_browser};

fn default_authentication_storage() -> AuthenticationStorage {
AuthenticationStorage::new("rattler", &dirs::home_dir().unwrap().join(".rattler"))
}

/// Adds a dependency to the project
#[derive(Parser, Debug)]
pub struct Args {
/// The host to authenticate with
host: String,

/// The token to use (for bearer authentication)
#[clap(long)]
token: Option<String>,

/// The username to use (for basic authentication)
#[clap(long)]
username: Option<String>,

/// The password to use (for basic authentication)
#[clap(long)]
password: Option<String>,

/// The token to use on anaconda.org style authentication
#[clap(long)]
conda_token: Option<String>,
}

pub async fn execute(args: Args) -> anyhow::Result<()> {
let storage = default_authentication_storage();


if let Some(conda_token) = args.conda_token {
let auth = Authentication::CondaToken(conda_token);
storage.store(&args.host, &auth)?;
return Ok(())
}
else if let Some(username) = args.username {
if args.password.is_none() {
anyhow::bail!("Password must be provided when using basic authentication");
}

let password = args.password.unwrap();
let auth = Authentication::BasicHTTP{username, password};
storage.store(&args.host, &auth)?;
return Ok(())
}
else if let Some(token) = args.token {
let auth = Authentication::BearerToken(token);
storage.store(&args.host, &auth)?;
return Ok(())
}
else {
anyhow::bail!("No authentication method provided");
}
}
3 changes: 3 additions & 0 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use anyhow::Error;
use tracing_subscriber::{filter::LevelFilter, util::SubscriberInitExt, EnvFilter};

mod add;
mod auth;
mod global;
mod init;
mod run;
Expand Down Expand Up @@ -43,6 +44,7 @@ enum Command {
Run(run::Args),
#[clap(alias = "g")]
Global(global::Args),
Auth(auth::Args),
}

fn completion(args: CompletionCommand) -> Result<(), Error> {
Expand Down Expand Up @@ -102,6 +104,7 @@ pub async fn execute() -> anyhow::Result<()> {
Some(Command::Add(cmd)) => add::execute(cmd).await,
Some(Command::Run(cmd)) => run::execute(cmd).await,
Some(Command::Global(cmd)) => global::execute(cmd).await,
Some(Command::Auth(cmd)) => auth::execute(cmd).await,
None => default().await,
}
}

0 comments on commit b829437

Please sign in to comment.