wala-rust

Content-adressed HTTP file server
Info | Log | Files | Refs | README | LICENSE

mock.rs (1971B)


      1 //! The `mock` auth module is provided to facilitate testing.
      2 //!
      3 //! If active, it will be executed for the `mock` authentication scheme.
      4 //!
      5 //! Under the `mock` scheme, a valid signature is simply the same value as the identity key.
      6 use std::io::{
      7     Read,
      8 };
      9 use crate::auth::{
     10     AuthSpec,
     11     AuthError,
     12     AuthResult,
     13 };
     14 
     15 
     16 /// Verifies the given [auth::AuthSpec](crate::auth::AuthSpec) structure against the `mock` scheme.
     17 ///
     18 /// # Arguments
     19 ///
     20 /// * `auth` - Authentication data submitted by client.
     21 /// * `data` - Content body submitted by client, to match signature against.
     22 /// * `data_length` - Length of content body
     23 pub fn auth_check(auth: &AuthSpec, data: impl Read, data_length: usize) -> Result<AuthResult, AuthError> {
     24     if auth.method != "mock" {
     25         return Err(AuthError{});
     26     }
     27     if auth.key != auth.signature {
     28         return Err(AuthError{});
     29     }
     30     let res = AuthResult{
     31         identity: auth.key.as_bytes().to_vec(),
     32         error: false,
     33     };
     34     Ok(res)
     35 }
     36 
     37 
     38 #[cfg(test)]
     39 mod tests {
     40     use super::auth_check;
     41     use super::{AuthSpec, AuthResult};
     42     use std::str::FromStr;
     43     use std::io::empty;
     44 
     45     #[test]
     46     fn test_mock_auth_check() {
     47         let mut auth_spec = AuthSpec::from_str("PUBSIG foo:bar:baz").unwrap();
     48         match auth_check(&auth_spec, empty(), 0) {
     49             Ok(v) => {
     50                 panic!("expected invalid auth");
     51             },
     52             Err(e) => {
     53             },
     54         }
     55 
     56         auth_spec = AuthSpec::from_str("PUBSIG mock:bar:baz").unwrap();
     57         match auth_check(&auth_spec, empty(), 0) {
     58             Ok(v) => {
     59                 panic!("expected invalid auth");
     60             },
     61             Err(e) => {
     62             },
     63         }
     64 
     65         auth_spec = AuthSpec::from_str("PUBSIG mock:bar:bar").unwrap();
     66         match auth_check(&auth_spec, empty(), 0) {
     67             Ok(v) => {
     68             },
     69             Err(e) => {
     70                 panic!("{}", e);
     71             },
     72         }
     73     }
     74 }