Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/cortex-rs/blob/main/src/auth.rs # cortex-rs Pure compiled native Rust memory engine with vector search and SQLite WAL Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `cortex-rs / src / auth.rs` 112 lines ยท 3595 bytes Raw Blame History 1 use axum::{ 2 extract::Request, 3 http::StatusCode, 4 middleware::Next, 5 response::{IntoResponse, Response}, 6 Json, 7 }; 8 use serde_json::json; 9 use std::fs; 10 use std::path::Path; 11 12 const TOKEN_PATH: &str = "/home/drakestapleton/.config/cortex/token"; 13 14 pub fn load_cortex_token() -> String { 15 if let Ok(env_tok) = std::env::var("CORTEX_TOKEN") { 16 if !env_tok.trim().is_empty() { 17 return env_tok.trim().to_string(); 18 } 19 } 20 let p = Path::new(TOKEN_PATH); 21 if p.exists() { 22 fs::read_to_string(p).unwrap_or_default().trim().to_string() 23 } else { 24 String::new() 25 } 26 } 27 28 pub fn validate_auth_header(auth_header: Option<&str>, expected_token: &str) -> Result<(), StatusCode> { 29 if expected_token.is_empty() { 30 return Err(StatusCode::SERVICE_UNAVAILABLE); 31 } 32 match auth_header { 33 Some(header) if header.starts_with("Bearer ") => { 34 let token = header.strip_prefix("Bearer ").unwrap().trim(); 35 if token == expected_token { 36 Ok(()) 37 } else { 38 Err(StatusCode::UNAUTHORIZED) 39 } 40 } 41 _ => Err(StatusCode::UNAUTHORIZED), 42 } 43 } 44 45 pub async fn auth_middleware(req: Request, next: Next) -> Result { 46 let expected = load_cortex_token(); 47 let auth_header = req.headers().get("Authorization").and_then(|h| h.to_str().ok()); 48 49 match validate_auth_header(auth_header, &expected) { 50 Ok(()) => Ok(next.run(req).await), 51 Err(StatusCode::SERVICE_UNAVAILABLE) => { 52 let err_resp = ( 53 StatusCode::SERVICE_UNAVAILABLE, 54 Json(json!({"error": "Service unavailable: Cortex authentication token unconfigured on host"})), 55 ).into_response(); 56 Err(err_resp) 57 } 58 Err(_) => { 59 let err_resp = ( 60 StatusCode::UNAUTHORIZED, 61 Json(json!({"error": "Unauthorized: invalid or missing Cortex bearer token"})), 62 ).into_response(); 63 Err(err_resp) 64 } 65 } 66 } 67 68 #[cfg(test)] 69 mod tests { 70 use super::*; 71 72 #[test] 73 fn test_load_cortex_token_env_override() { 74 std::env::set_var("CORTEX_TOKEN", "test_token_12345"); 75 let token = load_cortex_token(); 76 assert_eq!(token, "test_token_12345"); 77 std::env::remove_var("CORTEX_TOKEN"); 78 } 79 80 #[test] 81 fn test_validate_auth_header_success() { 82 let expected = "secret_vault_token_42"; 83 let res = validate_auth_header(Some("Bearer secret_vault_token_42"), expected); 84 assert_eq!(res, Ok(())); 85 } 86 87 #[test] 88 fn test_validate_auth_header_unauthenticated() { 89 let expected = "secret_vault_token_42"; 90 let res_none = validate_auth_header(None, expected); 91 assert_eq!(res_none, Err(StatusCode::UNAUTHORIZED)); 92 93 let res_empty = validate_auth_header(Some(""), expected); 94 assert_eq!(res_empty, Err(StatusCode::UNAUTHORIZED)); 95 96 let res_no_bearer = validate_auth_header(Some("Basic 12345"), expected); 97 assert_eq!(res_no_bearer, Err(StatusCode::UNAUTHORIZED)); 98 } 99 100 #[test] 101 fn test_validate_auth_header_invalid_token() { 102 let expected = "secret_vault_token_42"; 103 let res = validate_auth_header(Some("Bearer wrong_secret"), expected); 104 assert_eq!(res, Err(StatusCode::UNAUTHORIZED)); 105 } 106 107 #[test] 108 fn test_validate_auth_header_unconfigured_host() { 109 let res = validate_auth_header(Some("Bearer any_token"), ""); 110 assert_eq!(res, Err(StatusCode::SERVICE_UNAVAILABLE)); 111 } 112 }