aboutsummaryrefslogtreecommitdiffstats
path: root/src/auth.rs
diff options
context:
space:
mode:
authorjikstra <jikstra@disroot.org>2021-04-25 15:48:09 +0000
committerjikstra <jikstra@disroot.org>2021-09-01 19:08:00 +0000
commit06db56e40820ec0067d18840648ee786163a3862 (patch)
tree638eb60a484ba0b17a8373703141e19812dd0859 /src/auth.rs
parentAdd CHANGELOG entry for printing QR codes on terminal (diff)
downloadminiserve-06db56e40820ec0067d18840648ee786163a3862.tar.gz
miniserve-06db56e40820ec0067d18840648ee786163a3862.zip
Implement a raw rendering mode for recursive folder download
- Raw mode only displays file/folders and is more focused on computer processing - Display a banner in footer to recursively download the current folder with wget
Diffstat (limited to 'src/auth.rs')
-rw-r--r--src/auth.rs20
1 files changed, 14 insertions, 6 deletions
diff --git a/src/auth.rs b/src/auth.rs
index 0d97f11..43aca17 100644
--- a/src/auth.rs
+++ b/src/auth.rs
@@ -42,7 +42,7 @@ pub struct RequiredAuth {
}
/// Return `true` if `basic_auth` is matches any of `required_auth`
-pub fn match_auth(basic_auth: BasicAuthParams, required_auth: &[RequiredAuth]) -> bool {
+pub fn match_auth(basic_auth: &BasicAuthParams, required_auth: &[RequiredAuth]) -> bool {
required_auth
.iter()
.any(|RequiredAuth { username, password }| {
@@ -74,6 +74,9 @@ pub fn get_hash<T: Digest>(text: &str) -> Vec<u8> {
hasher.update(text);
hasher.finalize().to_vec()
}
+pub struct CurrentUser {
+ pub name: String,
+}
fn handle_auth(req: &HttpRequest) -> Result<(), ContextualError> {
let required_auth = &req.app_data::<crate::MiniserveConfig>().unwrap().auth;
@@ -85,7 +88,12 @@ fn handle_auth(req: &HttpRequest) -> Result<(), ContextualError> {
match BasicAuthParams::try_from_request(req) {
Ok(cred) => match match_auth(cred, required_auth) {
- true => Ok(()),
+ true => {
+ req.extensions_mut().insert(CurrentUser {
+ name: cred_params.username,
+ });
+ Ok(())
+ },
false => Err(ContextualError::InvalidHttpCredentials),
},
Err(_) => Err(ContextualError::RequireHttpCredentials),
@@ -173,7 +181,7 @@ mod tests {
) {
assert_eq!(
match_auth(
- BasicAuthParams {
+ &BasicAuthParams {
username: param_username.to_owned(),
password: param_password.to_owned(),
},
@@ -214,7 +222,7 @@ mod tests {
password: &str,
) {
assert!(match_auth(
- BasicAuthParams {
+ &BasicAuthParams {
username: username.to_owned(),
password: password.to_owned(),
},
@@ -225,7 +233,7 @@ mod tests {
#[rstest]
fn test_multiple_auth_wrong_username(account_sample: Vec<RequiredAuth>) {
assert_eq!(match_auth(
- BasicAuthParams {
+ &BasicAuthParams {
username: "unregistered user".to_owned(),
password: "pwd0".to_owned(),
},
@@ -248,7 +256,7 @@ mod tests {
password: &str,
) {
assert_eq!(match_auth(
- BasicAuthParams {
+ &BasicAuthParams {
username: username.to_owned(),
password: password.to_owned(),
},