aien-sovereign-core-mv
AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.
Brought by drakestapleton
aien-sovereign-core-mv / crates / cortex-rs / src / auth.rs
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | use axum::{ |
| 17 | extract::Request, |
| 18 | http::StatusCode, |
| 19 | middleware::Next, |
| 20 | response::{IntoResponse, Response}, |
| 21 | Json, |
| 22 | }; |
| 23 | use serde_json::json; |
| 24 | use std::fs; |
| 25 | |
| 26 | fn default_token_path() -> std::path::PathBuf { |
| 27 | let home = std::env::var("HOME") |
| 28 | .or_else(|_| std::env::var("USERPROFILE")) |
| 29 | .unwrap_or_else(|_| ".".to_string()); |
| 30 | std::path::PathBuf::from(home).join(".config/cortex/token") |
| 31 | } |
| 32 | |
| 33 | pub fn load_cortex_token() -> String { |
| 34 | if let Ok(env_tok) = std::env::var("CORTEX_TOKEN") { |
| 35 | if !env_tok.trim().is_empty() { |
| 36 | return env_tok.trim().to_string(); |
| 37 | } |
| 38 | } |
| 39 | let p = default_token_path(); |
| 40 | if p.exists() { |
| 41 | fs::read_to_string(p).unwrap_or_default().trim().to_string() |
| 42 | } else { |
| 43 | String::new() |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | pub fn validate_auth_header( |
| 48 | auth_header: Option<&str>, |
| 49 | expected_token: &str, |
| 50 | ) -> Result<(), StatusCode> { |
| 51 | if expected_token.is_empty() { |
| 52 | return Err(StatusCode::SERVICE_UNAVAILABLE); |
| 53 | } |
| 54 | match auth_header { |
| 55 | Some(header) if header.starts_with("Bearer ") => { |
| 56 | let token = header.strip_prefix("Bearer ").unwrap().trim(); |
| 57 | if token == expected_token { |
| 58 | Ok(()) |
| 59 | } else { |
| 60 | Err(StatusCode::UNAUTHORIZED) |
| 61 | } |
| 62 | } |
| 63 | _ => Err(StatusCode::UNAUTHORIZED), |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | #[allow(clippy::result_large_err)] |
| 68 | pub async fn auth_middleware(req: Request, next: Next) -> Result<Response, Response> { |
| 69 | let expected = load_cortex_token(); |
| 70 | let auth_header = req |
| 71 | .headers() |
| 72 | .get("Authorization") |
| 73 | .and_then(|h| h.to_str().ok()); |
| 74 | |
| 75 | match validate_auth_header(auth_header, &expected) { |
| 76 | Ok(()) => Ok(next.run(req).await), |
| 77 | Err(StatusCode::SERVICE_UNAVAILABLE) => { |
| 78 | let err_resp = ( |
| 79 | StatusCode::SERVICE_UNAVAILABLE, |
| 80 | Json(json!({"error": "Service unavailable: Cortex authentication token unconfigured on host"})), |
| 81 | ).into_response(); |
| 82 | Err(err_resp) |
| 83 | } |
| 84 | Err(_) => { |
| 85 | let err_resp = ( |
| 86 | StatusCode::UNAUTHORIZED, |
| 87 | Json(json!({"error": "Unauthorized: invalid or missing Cortex bearer token"})), |
| 88 | ) |
| 89 | .into_response(); |
| 90 | Err(err_resp) |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | #[cfg(test)] |
| 96 | mod tests { |
| 97 | use super::*; |
| 98 | |
| 99 | #[test] |
| 100 | fn test_load_cortex_token_env_override() { |
| 101 | std::env::set_var("CORTEX_TOKEN", "test_token_12345"); |
| 102 | let token = load_cortex_token(); |
| 103 | assert_eq!(token, "test_token_12345"); |
| 104 | std::env::remove_var("CORTEX_TOKEN"); |
| 105 | } |
| 106 | |
| 107 | #[test] |
| 108 | fn test_validate_auth_header_success() { |
| 109 | let expected = "secret_vault_token_42"; |
| 110 | let res = validate_auth_header(Some("Bearer secret_vault_token_42"), expected); |
| 111 | assert_eq!(res, Ok(())); |
| 112 | } |
| 113 | |
| 114 | #[test] |
| 115 | fn test_validate_auth_header_unauthenticated() { |
| 116 | let expected = "secret_vault_token_42"; |
| 117 | let res_none = validate_auth_header(None, expected); |
| 118 | assert_eq!(res_none, Err(StatusCode::UNAUTHORIZED)); |
| 119 | |
| 120 | let res_empty = validate_auth_header(Some(""), expected); |
| 121 | assert_eq!(res_empty, Err(StatusCode::UNAUTHORIZED)); |
| 122 | |
| 123 | let res_no_bearer = validate_auth_header(Some("Basic 12345"), expected); |
| 124 | assert_eq!(res_no_bearer, Err(StatusCode::UNAUTHORIZED)); |
| 125 | } |
| 126 | |
| 127 | #[test] |
| 128 | fn test_validate_auth_header_invalid_token() { |
| 129 | let expected = "secret_vault_token_42"; |
| 130 | let res = validate_auth_header(Some("Bearer wrong_secret"), expected); |
| 131 | assert_eq!(res, Err(StatusCode::UNAUTHORIZED)); |
| 132 | } |
| 133 | |
| 134 | #[test] |
| 135 | fn test_validate_auth_header_unconfigured_host() { |
| 136 | let res = validate_auth_header(Some("Bearer any_token"), ""); |
| 137 | assert_eq!(res, Err(StatusCode::SERVICE_UNAVAILABLE)); |
| 138 | } |
| 139 | } |