Implement basic HTTP authentication argument and environment variable

This commit is contained in:
timvisee 2019-03-19 22:09:10 +01:00
parent 0052a60503
commit 556eed4a71
No known key found for this signature in database
GPG key ID: B8DB720BC383E172
6 changed files with 60 additions and 9 deletions

View file

@ -435,13 +435,14 @@ The following environment variables may be used to configure the following
defaults. The CLI flag is shown along with it, to better describe the relation
to command line arguments:
| Variable | CLI flag | Description |
| :------------------------ | :----------------------------: | :-------------------------------- |
| `FFSEND_HISTORY` | `--history <FILE>` | History file path |
| `FFSEND_HOST` | `--host <URL>` | Upload host |
| `FFSEND_TIMEOUT` | `--timeout <SECONDS>` | Request timeout (0 to disable) |
| `FFSEND_TRANSFER_TIMEOUT` | `--transfer-timeout <SECONDS>` | Transfer timeout (0 to disable) |
| `FFSEND_API` | `--api <VERSION>` | Server API version, `-` to lookup |
| Variable | CLI flag | Description |
| :------------------------ | :----------------------------: | :-------------------------------------------- |
| `FFSEND_HISTORY` | `--history <FILE>` | History file path |
| `FFSEND_HOST` | `--host <URL>` | Upload host |
| `FFSEND_TIMEOUT` | `--timeout <SECONDS>` | Request timeout (0 to disable) |
| `FFSEND_TRANSFER_TIMEOUT` | `--transfer-timeout <SECONDS>` | Transfer timeout (0 to disable) |
| `FFSEND_API` | `--api <VERSION>` | Server API version, `-` to lookup |
| `FFSEND_BASIC_AUTH` | `--basic-auth <USER:PASSWORD>` | Basic HTTP authentication credentials to use. |
These environment variables may be used to toggle a flag, simply by making them
available. The actual value of these variables is ignored, and variables may be

View file

@ -14,6 +14,7 @@ pub fn create_config(matcher_main: &MainMatcher) -> ClientConfig {
ClientConfigBuilder::default()
.timeout(to_duration(matcher_main.timeout()))
.transfer_timeout(to_duration(matcher_main.transfer_timeout()))
.basic_auth(matcher_main.basic_auth())
.build()
.expect("failed to create network client configuration")
}

41
src/cmd/arg/basic_auth.rs Normal file
View file

@ -0,0 +1,41 @@
use clap::{Arg, ArgMatches};
use super::{CmdArg, CmdArgOption};
/// The basicauth argument.
pub struct ArgBasicAuth {}
impl CmdArg for ArgBasicAuth {
fn name() -> &'static str {
"basic-auth"
}
fn build<'b, 'c>() -> Arg<'b, 'c> {
Arg::with_name("basic-auth")
.long("basic-auth")
.value_name("USER:PASSWORD")
.env("FFSEND_BASIC_AUTH")
.hide_env_values(true)
.global(true)
.help("HTTP basic authentication credentials")
}
}
impl<'a> CmdArgOption<'a> for ArgBasicAuth {
type Value = Option<(String, Option<String>)>;
fn value<'b: 'a>(matches: &'a ArgMatches<'b>) -> Self::Value {
// Get the authentication credentials
let raw = match Self::value_raw(matches) {
Some(raw) => raw,
None => return None,
};
// Split the properties
let mut iter = raw.splitn(2, ':');
Some((
iter.next().unwrap_or("").to_owned(),
iter.next().map(|pass| pass.to_owned()),
))
}
}

View file

@ -1,4 +1,5 @@
pub mod api;
pub mod basic_auth;
pub mod download_limit;
pub mod gen_passphrase;
pub mod host;
@ -8,6 +9,7 @@ pub mod url;
// Re-eexport to arg module
pub use self::api::ArgApi;
pub use self::basic_auth::ArgBasicAuth;
pub use self::download_limit::ArgDownloadLimit;
pub use self::gen_passphrase::ArgGenPassphrase;
pub use self::host::ArgHost;

View file

@ -5,7 +5,7 @@ use std::ffi::OsString;
use clap::{App, AppSettings, Arg, ArgMatches};
use super::arg::{ArgApi, CmdArg};
use super::arg::{ArgApi, ArgBasicAuth, CmdArg};
#[cfg(feature = "history")]
use super::matcher::HistoryMatcher;
use super::matcher::{
@ -149,6 +149,7 @@ impl<'a: 'b, 'b> Handler<'a> {
.help("Enable verbose information and logging"),
)
.arg(ArgApi::build())
.arg(ArgBasicAuth::build())
.subcommand(CmdDebug::build())
.subcommand(CmdDelete::build())
.subcommand(CmdDownload::build().display_order(2))

View file

@ -5,7 +5,7 @@ use clap::ArgMatches;
use ffsend_api::api::DesiredVersion;
use super::Matcher;
use crate::cmd::arg::{ArgApi, CmdArgOption};
use crate::cmd::arg::{ArgApi, ArgBasicAuth, CmdArgOption};
use crate::util::env_var_present;
#[cfg(feature = "history")]
use crate::util::{quit_error_msg, ErrorHintsBuilder};
@ -36,6 +36,11 @@ impl<'a: 'b, 'b> MainMatcher<'a> {
ArgApi::value(self.matches)
}
/// Get basic HTTP authentication credentials to use.
pub fn basic_auth(&'a self) -> Option<(String, Option<String>)> {
ArgBasicAuth::value(self.matches)
}
/// Get the history file to use.
#[cfg(feature = "history")]
pub fn history(&self) -> PathBuf {