Make keyring support non-optional

Now that the dbus library isn't needed..
This commit is contained in:
FliegendeWurst 2022-01-06 15:57:50 +01:00
parent bd9a777bb9
commit 126f8500e4
3 changed files with 527 additions and 306 deletions

772
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
[package] [package]
name = "KIT-ILIAS-downloader" name = "KIT-ILIAS-downloader"
version = "0.2.24" version = "0.3.0"
authors = ["FliegendeWurst <2012gdwu@posteo.de>"] authors = ["FliegendeWurst <2012gdwu@posteo.de>"]
license = "GPL-3.0-or-later" license = "GPL-3.0-or-later"
edition = "2018" edition = "2018"
@ -24,18 +24,14 @@ rprompt = "1.0.5"
ignore = "0.4.14" ignore = "0.4.14"
anyhow = "1.0.28" anyhow = "1.0.28"
colored = "2.0.0" colored = "2.0.0"
keyring = { version = "0.10.1", optional = true } keyring = "1.0.0"
cfg-if = "1.0.0" cfg-if = "1.0.0"
indicatif = "0.16.0" indicatif = "0.16.0"
once_cell = "1.7.2" once_cell = "1.7.2"
atty = "0.2.14" atty = "0.2.14"
h2 = "0.3.3" h2 = "0.3.3"
cookie_store = "0.14.0" cookie_store = "0.15.1"
reqwest_cookie_store = "0.1.5" reqwest_cookie_store = "0.2.0"
bytes = "1.0.1" bytes = "1.0.1"
toml = "0.5.8" toml = "0.5.8"
tempfile = "3.2.0" tempfile = "3.2.0"
[features]
default = []
keyring-auth = ["keyring"]

View File

@ -3,7 +3,6 @@
use std::path::PathBuf; use std::path::PathBuf;
use std::sync::atomic::{AtomicBool, AtomicUsize}; use std::sync::atomic::{AtomicBool, AtomicUsize};
#[cfg(feature = "keyring-auth")]
use anyhow::anyhow; use anyhow::anyhow;
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use indicatif::ProgressBar; use indicatif::ProgressBar;
@ -63,7 +62,6 @@ pub struct Opt {
/// Use the system keyring /// Use the system keyring
#[structopt(long)] #[structopt(long)]
#[cfg(feature = "keyring-auth")]
pub keyring: bool, pub keyring: bool,
/// KIT account username /// KIT account username
@ -151,42 +149,27 @@ pub fn ask_user_pass(opt: &Opt) -> Result<(String, String)> {
} else { } else {
rprompt::prompt_reply_stdout("Username: ").context("username prompt")? rprompt::prompt_reply_stdout("Username: ").context("username prompt")?
}; };
#[cfg(feature = "keyring-auth")]
let (pass, should_store); let (pass, should_store);
#[cfg(feature = "keyring-auth")] let keyring = Lazy::new(|| keyring::Entry::new(env!("CARGO_PKG_NAME"), &user));
let keyring = Lazy::new(|| keyring::Keyring::new(env!("CARGO_PKG_NAME"), &user)); if let Some(password) = opt.password.as_ref() {
#[cfg(not(feature = "keyring-auth"))] pass = password.clone();
let pass; should_store = true;
cfg_if::cfg_if! { // TODO: deduplicate the logic below } else if opt.keyring {
if #[cfg(feature = "keyring-auth")] { match keyring.get_password() {
if let Some(password) = opt.password.as_ref() { Ok(password) => {
pass = password.clone(); pass = password;
should_store = true; should_store = false;
} else if opt.keyring { },
match keyring.get_password() { Err(e) => {
Ok(password) => { error!(e);
pass = password;
should_store = false;
},
Err(e) => {
error!(e);
pass = rpassword::read_password_from_tty(Some("Password: ")).context("password prompt")?;
should_store = true;
}
}
} else {
pass = rpassword::read_password_from_tty(Some("Password: ")).context("password prompt")?; pass = rpassword::read_password_from_tty(Some("Password: ")).context("password prompt")?;
should_store = true; should_store = true;
} }
} else {
if let Some(password) = opt.password.as_ref() {
pass = password.clone();
} else {
pass = rpassword::read_password_from_tty(Some("Password: ")).context("password prompt")?;
}
} }
}; } else {
#[cfg(feature = "keyring-auth")] pass = rpassword::read_password_from_tty(Some("Password: ")).context("password prompt")?;
should_store = true;
}
if should_store && opt.keyring { if should_store && opt.keyring {
keyring.set_password(&pass).map_err(|x| anyhow!(x.to_string()))?; keyring.set_password(&pass).map_err(|x| anyhow!(x.to_string()))?;
} }