commit d6fbeaa9b1e55c2d793289b18c69146b4a4ff8da
parent 0e69082cbd484c1a03dc1cede04f5f3654a8176f
Author: lash <dev@holbrook.no>
Date: Mon, 13 Jun 2022 16:59:59 +0000
Add mock auth and test
Diffstat:
2 files changed, 52 insertions(+), 6 deletions(-)
diff --git a/src/auth/mock.rs b/src/auth/mock.rs
@@ -1,6 +1,46 @@
+use std::fmt;
+use std::error::Error;
+
use crate::auth::AuthSpec;
-pub fn auth_check(auth: AuthSpec) {
- let auth_fields = s.split(":").collect()
+#[derive(Debug)]
+pub struct AuthError;
+
+impl fmt::Display for AuthError {
+ fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
+ fmt.write_str(self.description())
+ }
+}
+
+impl Error for AuthError {
+ fn description(&self) -> &str{
+ "auth key signature mismatch"
+ }
+}
+
+pub fn auth_check(auth: AuthSpec) -> bool {
+ if auth.method != "mock" {
+ return false;
+ }
+ if auth.key != auth.signature {
+ return false;
+ }
+ true
+}
+
+
+#[test]
+fn test_mock_auth_check() {
+ use super::mock::auth_check;
+ use super::AuthSpec;
+ use std::str::FromStr;
+
+ let mut auth_spec = AuthSpec::from_str("foo:bar:baz").unwrap();
+ assert!(!auth_check(auth_spec));
+
+ auth_spec = AuthSpec::from_str("mock:bar:baz").unwrap();
+ assert!(!auth_check(auth_spec));
+ auth_spec = AuthSpec::from_str("mock:bar:bar").unwrap();
+ assert!(auth_check(auth_spec));
}
diff --git a/src/auth/mod.rs b/src/auth/mod.rs
@@ -4,8 +4,8 @@ use std::fmt;
pub struct AuthSpec {
pub method: String,
- key: Vec<u8>,
- signature: Vec<u8>,
+ pub key: String,
+ pub signature: String,
}
#[derive(Debug)]
@@ -32,10 +32,13 @@ impl FromStr for AuthSpec {
return Err(AuthSpecError{})
}
let auth_type: String = auth_fields.next().unwrap().to_string();
+ let auth_key: String = auth_fields.next().unwrap().to_string();
+ let auth_signature: String = auth_fields.next().unwrap().to_string();
+
let r = AuthSpec{
method: auth_type,
- key: vec!(),
- signature: vec!(),
+ key: auth_key,
+ signature: auth_signature,
};
Ok(r)
}
@@ -52,3 +55,6 @@ impl fmt::Debug for AuthSpec {
)
}
}
+
+#[cfg(test)]
+mod mock;