aboutsummaryrefslogtreecommitdiffstats
path: root/src/auth.rs
diff options
context:
space:
mode:
authorkhai96_ <hvksmr1996@gmail.com>2019-04-26 10:08:23 +0000
committerkhai96_ <hvksmr1996@gmail.com>2019-04-26 10:08:23 +0000
commitaac70e6607d3e8172f15df91bc16dc3244473ea9 (patch)
treeebed28e4e6913f617bfc27dc8d0656955e55689d /src/auth.rs
parentMerge remote-tracking branch 'mainrepo/master' into pullrequest.hashed-password (diff)
downloadminiserve-aac70e6607d3e8172f15df91bc16dc3244473ea9.tar.gz
miniserve-aac70e6607d3e8172f15df91bc16dc3244473ea9.zip
Comply to change requests
- Added doc comments - Added an additional test case
Diffstat (limited to '')
-rw-r--r--src/auth.rs8
1 files changed, 7 insertions, 1 deletions
diff --git a/src/auth.rs b/src/auth.rs
index 432f6ce..e75f498 100644
--- a/src/auth.rs
+++ b/src/auth.rs
@@ -15,6 +15,7 @@ pub struct BasicAuthParams {
}
#[derive(Clone, Debug, PartialEq)]
+/// `password` field of `RequiredAuth`
pub enum RequiredAuthPassword {
Plain(String),
Sha256(Vec<u8>),
@@ -22,7 +23,7 @@ pub enum RequiredAuthPassword {
}
#[derive(Clone, Debug, PartialEq)]
-/// Authentication structure to match BasicAuthParams against
+/// Authentication structure to match `BasicAuthParams` against
pub struct RequiredAuth {
pub username: String,
pub password: RequiredAuthPassword,
@@ -54,6 +55,7 @@ pub fn parse_basic_auth(
})
}
+/// Verify authentication
pub fn match_auth(basic_auth: BasicAuthParams, required_auth: &RequiredAuth) -> bool {
if basic_auth.username != required_auth.username {
return false;
@@ -72,10 +74,12 @@ pub fn match_auth(basic_auth: BasicAuthParams, required_auth: &RequiredAuth) ->
}
}
+/// Return `true` if hashing of `password` by `T` algorithm equals to `hash`
pub fn compare_hash<T: Digest>(password: String, hash: &Vec<u8>) -> bool {
get_hash::<T>(password) == *hash
}
+/// Get hash of a `text`
pub fn get_hash<T: Digest>(text: String) -> Vec<u8> {
let mut hasher = T::new();
hasher.input(text);
@@ -124,6 +128,7 @@ mod tests {
use super::*;
use rstest::rstest_parametrize;
+ /// Return a hashing function corresponds to given name
fn get_hash_func(name: &str) -> impl FnOnce(String) -> Vec<u8> {
match name {
"sha256" => get_hash::<Sha256>,
@@ -144,6 +149,7 @@ mod tests {
assert_eq!(received, expected);
}
+ /// Helper function that creates a `RequiredAuth` structure and encrypt `password` if necessary
fn create_required_auth(username: &str, password: &str, encrypt: &str) -> RequiredAuth {
use RequiredAuthPassword::*;