aboutsummaryrefslogtreecommitdiffstats
path: root/src/args.rs
diff options
context:
space:
mode:
authorkhai96_ <hvksmr1996@gmail.com>2019-05-05 07:30:13 +0000
committerkhai96_ <hvksmr1996@gmail.com>2019-05-05 07:30:13 +0000
commit70d82c3faad9204e47ed514b58c7bd7c5e7cb61a (patch)
tree63e2e5457a93f85789c3e59c31e958f89fe943b1 /src/args.rs
parentMerge pull request #99 from KSXGitHub/new-clippy (diff)
downloadminiserve-70d82c3faad9204e47ed514b58c7bd7c5e7cb61a.tar.gz
miniserve-70d82c3faad9204e47ed514b58c7bd7c5e7cb61a.zip
Begin to support multiple auths
Diffstat (limited to 'src/args.rs')
-rw-r--r--src/args.rs30
1 files changed, 21 insertions, 9 deletions
diff --git a/src/args.rs b/src/args.rs
index 63799a0..3972995 100644
--- a/src/args.rs
+++ b/src/args.rs
@@ -77,8 +77,20 @@ fn parse_interface(src: &str) -> Result<IpAddr, std::net::AddrParseError> {
src.parse::<IpAddr>()
}
-/// Checks wether the auth string is valid, i.e. it follows the syntax username:password
+/// Parse a string of multiple authentication requirements
fn parse_auth(src: &str) -> Result<auth::RequiredAuth, ContextualError> {
+ let mut required_auth = auth::RequiredAuth::new();
+
+ for pair in src.split_whitespace().map(parse_single_auth) {
+ let (username, password) = pair?;
+ required_auth.insert(username.to_owned(), password);
+ }
+
+ Ok(required_auth)
+}
+
+/// Parse a single authentication requirement
+fn parse_single_auth(src: &str) -> Result<(String, auth::RequiredAuthPassword), ContextualError> {
let mut split = src.splitn(3, ':');
let invalid_auth_format = Err(ContextualError::InvalidAuthFormat);
@@ -119,10 +131,7 @@ fn parse_auth(src: &str) -> Result<auth::RequiredAuth, ContextualError> {
auth::RequiredAuthPassword::Plain(second_part.to_owned())
};
- Ok(auth::RequiredAuth {
- username: username.to_owned(),
- password,
- })
+ Ok((username.to_owned(), password))
}
/// Parses the command line arguments
@@ -172,16 +181,19 @@ mod tests {
fn create_required_auth(username: &str, password: &str, encrypt: &str) -> auth::RequiredAuth {
use auth::*;
use RequiredAuthPassword::*;
+ let mut required_auth = RequiredAuth::new();
- RequiredAuth {
- username: username.to_owned(),
- password: match encrypt {
+ required_auth.insert(
+ username.to_owned(),
+ match encrypt {
"plain" => Plain(password.to_owned()),
"sha256" => Sha256(hex::decode(password.to_owned()).unwrap()),
"sha512" => Sha512(hex::decode(password.to_owned()).unwrap()),
_ => panic!("Unknown encryption type"),
},
- }
+ );
+
+ required_auth
}
#[rstest_parametrize(