Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

login sets default url with arg and creates default key if absent #255

Merged
merged 1 commit into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions src/commands/login.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use anyhow::{bail, Context, Result};
use clap::Args;
use dialoguer::{theme::ColorfulTheme, Password};
use warg_client::RegistryUrl;
use indexmap::IndexSet;
use p256::ecdsa::SigningKey;
use rand_core::OsRng;
use warg_client::{Config, RegistryUrl};

use crate::keyring::set_auth_token;
use crate::keyring::{set_auth_token, set_signing_key};

use super::CommonOptions;

Expand Down Expand Up @@ -41,6 +44,43 @@ impl KeyringEntryArgs {
impl LoginCommand {
/// Executes the command.
pub async fn exec(self) -> Result<()> {
let home_url = &self
.common
.registry
.clone()
.map(RegistryUrl::new)
.transpose()?
.map(|u| u.to_string());
let mut config = self.common.read_config()?;
if home_url.is_some() {
config.home_url = home_url.clone();
config.write_to_file(&Config::default_config_path()?)?;
}
if config.keys.is_none() {
let mut keys = IndexSet::new();
keys.insert("default".to_string());
config.keys = Some(keys);
let key = SigningKey::random(&mut OsRng).into();
set_signing_key(
&None,
&key,
config.keys.as_mut().unwrap(),
config.home_url.clone(),
)?;
let public_key = key.public_key();
let token = Password::with_theme(&ColorfulTheme::default())
.with_prompt("Enter auth token")
.interact()
.context("failed to read token")?;
self.keyring_entry
.set_entry(self.common.read_config()?.home_url, &token)?;
config.write_to_file(&Config::default_config_path()?)?;
println!("auth token was set successfully, and generated default key",);
println!("Public Key: {public_key}");
return Ok(());
}
config.write_to_file(&Config::default_config_path()?)?;

let token = Password::with_theme(&ColorfulTheme::default())
.with_prompt("Enter auth token")
.interact()
Expand Down
6 changes: 4 additions & 2 deletions src/keyring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,10 @@ pub fn get_signing_key_entry(
}
} else {
if let Some(url) = &home_url {
return Entry::new("warg-signing-key", &RegistryUrl::new(url)?.safe_label())
.context("failed to get keyring entry");
if keys.contains(url) {
return Entry::new("warg-signing-key", &RegistryUrl::new(url)?.safe_label())
.context("failed to get keyring entry");
}
}
if keys.contains("default") {
Entry::new("warg-signing-key", "default").context("failed to get keyring entry")
Expand Down
Loading