1 //! The auth contract of the web: one bearer token, one set of peer uids.2 3 use inf3r_server::Headers;4 use inf3r_server::auth::{Auth, AuthError, Principal};5 6 /// A token that is 64 hex characters, the only shape the deploy file allows.7 const GOOD: &str = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";8 9 /// The Authorization header for `token`.10 fn bearer(token: &str) -> Headers {11 let mut headers = Headers::new();12 headers.push("authorization", &format!("Bearer {token}"));13 headers14 }15 16 #[test]17 fn a_good_token_passes() {18 let auth = Auth::from_token(GOOD).unwrap();19 assert!(auth.is_configured());20 assert_eq!(auth.check_token(&bearer(GOOD)), Ok(Principal::Token));21 }22 23 #[test]24 fn one_flipped_character_fails() {25 let auth = Auth::from_token(GOOD).unwrap();26 let mut flipped: Vec<char> = GOOD.chars().collect();27 flipped[10] = if flipped[10] == '0' { '1' } else { '0' };28 let flipped: String = flipped.into_iter().collect();29 assert_eq!(auth.check_token(&bearer(&flipped)), Err(AuthError::Bad));30 }31 32 #[test]33 fn the_scheme_is_exact() {34 let auth = Auth::from_token(GOOD).unwrap();35 36 let mut lower = Headers::new();37 lower.push("authorization", &format!("bearer {GOOD}"));38 assert_eq!(auth.check_token(&lower), Err(AuthError::Scheme));39 40 // The scheme is `Bearer ` with one space, so a second space leaves a41 // leading space in the token and the token is not the configured one.42 let mut two = Headers::new();43 two.push("authorization", &format!("Bearer {GOOD}"));44 assert_eq!(auth.check_token(&two), Err(AuthError::Bad));45 }46 47 #[test]48 fn the_header_name_is_case_insensitive() {49 let auth = Auth::from_token(GOOD).unwrap();50 let mut headers = Headers::new();51 headers.push("AuThOrIzAtIoN", &format!("Bearer {GOOD}"));52 assert_eq!(auth.check_token(&headers), Ok(Principal::Token));53 }54 55 #[test]56 fn a_short_token_file_is_bad_token() {57 let dir = std::env::temp_dir().join(format!("inf3r-auth-short-{}", std::process::id()));58 let _ = std::fs::remove_dir_all(&dir);59 std::fs::create_dir_all(&dir).unwrap();60 let path = dir.join("token");61 62 std::fs::write(&path, format!("{}\n", &GOOD[..63])).unwrap();63 assert!(matches!(64 Auth::from_token_file(&path),65 Err(AuthError::BadToken(_))66 ));67 68 // The same file with one more character is the token it names.69 std::fs::write(&path, format!("{GOOD}\n")).unwrap();70 let auth = Auth::from_token_file(&path).unwrap();71 assert_eq!(auth.check_token(&bearer(GOOD)), Ok(Principal::Token));72 73 let _ = std::fs::remove_dir_all(&dir);74 }75 76 #[test]77 fn the_allow_list_starts_empty_and_admits_only_what_it_holds() {78 let mut auth = Auth::new();79 assert_eq!(auth.check_peer(0), Err(AuthError::Bad));80 assert_eq!(auth.check_peer(1000), Err(AuthError::Bad));81 82 auth.allow_uid(1000);83 assert_eq!(auth.check_peer(1000), Ok(Principal::PeerUid(1000)));84 assert_eq!(auth.check_peer(0), Err(AuthError::Bad));85 }86 87 #[test]88 fn an_unconfigured_token_is_the_callers_decision() {89 // No token is "auth off". The module reports `Missing` and does not90 // guess; the caller decides whether that means open or closed.91 let auth = Auth::new();92 assert!(!auth.is_configured());93 assert_eq!(auth.check_token(&bearer(GOOD)), Err(AuthError::Missing));94 assert_eq!(auth.check_token(&Headers::new()), Err(AuthError::Missing));95 }96 97 #[test]98 fn a_constant_time_compare_reaches_the_last_byte() {99 // A shape test, not a timing test. A token that differs in the first byte100 // and one that differs in the last byte must both be `Bad`, so the101 // comparison does not return early on the first mismatch.102 let auth = Auth::from_token(GOOD).unwrap();103 104 let mut first: Vec<char> = GOOD.chars().collect();105 first[0] = if first[0] == '0' { '1' } else { '0' };106 let first: String = first.into_iter().collect();107 108 let mut last: Vec<char> = GOOD.chars().collect();109 let end = last.len() - 1;110 last[end] = if last[end] == '0' { '1' } else { '0' };111 let last: String = last.into_iter().collect();112 113 assert_eq!(auth.check_token(&bearer(&first)), Err(AuthError::Bad));114 assert_eq!(auth.check_token(&bearer(&last)), Err(AuthError::Bad));115 }