From aac70e6607d3e8172f15df91bc16dc3244473ea9 Mon Sep 17 00:00:00 2001 From: khai96_ Date: Fri, 26 Apr 2019 17:08:23 +0700 Subject: Comply to change requests - Added doc comments - Added an additional test case --- src/auth.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'src/auth.rs') 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), @@ -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(password: String, hash: &Vec) -> bool { get_hash::(password) == *hash } +/// Get hash of a `text` pub fn get_hash(text: String) -> Vec { 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 { match name { "sha256" => get_hash::, @@ -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::*; -- cgit v1.2.3