diff options
author | Sven-Hendrik Haase <svenstaro@gmail.com> | 2018-04-29 23:48:01 +0000 |
---|---|---|
committer | Sven-Hendrik Haase <svenstaro@gmail.com> | 2018-04-29 23:48:01 +0000 |
commit | 9d972a43b32dfdd620d8f710f6c2db9a327c5103 (patch) | |
tree | f723ad25669c35d62b6d00e56f320273bf700e2c /src/main.rs | |
parent | Some more progress (diff) | |
download | miniserve-9d972a43b32dfdd620d8f710f6c2db9a327c5103.tar.gz miniserve-9d972a43b32dfdd620d8f710f6c2db9a327c5103.zip |
Basic auth stuff
Diffstat (limited to '')
-rw-r--r-- | src/main.rs | 23 |
1 files changed, 19 insertions, 4 deletions
diff --git a/src/main.rs b/src/main.rs index 2258fac..d291aa5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ extern crate actix; extern crate actix_web; extern crate simplelog; +extern crate base64; #[macro_use] extern crate clap; @@ -11,6 +12,19 @@ use actix_web::middleware::{Middleware, Response}; use simplelog::{TermLogger, LevelFilter, Config}; use std::path::PathBuf; use std::net::{IpAddr, Ipv4Addr}; +use std::error::Error; + +/// Decode a HTTP basic auth string into a tuple of username and password. +fn parse_basic_auth(auth: String) -> Result<(String, String), String> { + let decoded = base64::decode(&auth).map_err(|e| e.description().to_owned())?; + let decoded_str = String::from_utf8_lossy(&decoded); + let strings: Vec<&str> = decoded_str.splitn(2, ':').collect(); + if strings.len() != 2 { + return Err("Invalid username/password format".to_owned()); + } + let (user, password) = (strings[0], strings[1]); + Ok((user.to_owned(), password.to_owned())) +} #[derive(Clone)] pub struct MiniserveConfig { @@ -38,7 +52,7 @@ fn is_valid_interface(interface: String) -> Result<(), String> { } fn is_valid_auth(auth: String) -> Result<(), String> { - auth.find(':').ok_or("Auth is not in form user:password".to_owned()).map(|_| ()) + auth.find(':').ok_or("Correct format is user:password".to_owned()).map(|_| ()) } pub fn parse_args() -> MiniserveConfig { @@ -131,9 +145,10 @@ struct Auth; impl Middleware<MiniserveConfig> for Auth { fn response(&self, req: &mut HttpRequest<MiniserveConfig>, mut resp: HttpResponse) -> Result<Response> { - let passphrase = &req.state().auth; - if passphrase.is_some() { - println!("{:?}", passphrase); + let required_auth = &req.state().auth; + if required_auth.is_some() { + // parse_basic_auth(pass) + println!("{:?}", required_auth); println!("{:?}", req.headers().get(header::AUTHORIZATION)); } resp.headers_mut().insert(header::WWW_AUTHENTICATE, header::HeaderValue::from_static("Basic realm=\"lol\"")); |