llm_edge_security/
auth.rs

1//! Authentication implementations
2
3use crate::{SecurityError, SecurityResult};
4use secrecy::{ExposeSecret, Secret};
5use std::collections::HashMap;
6
7/// API key authentication
8pub struct ApiKeyAuth {
9    keys: HashMap<String, Secret<String>>,
10}
11
12impl Default for ApiKeyAuth {
13    fn default() -> Self {
14        Self::new()
15    }
16}
17
18impl ApiKeyAuth {
19    pub fn new() -> Self {
20        Self {
21            keys: HashMap::new(),
22        }
23    }
24
25    pub fn add_key(&mut self, client_id: String, api_key: String) {
26        self.keys.insert(client_id, Secret::new(api_key));
27    }
28
29    pub fn validate(&self, api_key: &str) -> SecurityResult<String> {
30        for (client_id, secret) in &self.keys {
31            if secret.expose_secret() == api_key {
32                return Ok(client_id.clone());
33            }
34        }
35        Err(SecurityError::InvalidApiKey)
36    }
37}
38
39/// JWT token authentication
40pub struct JwtAuth {
41    // TODO: Implement JWT validation
42}
43
44impl Default for JwtAuth {
45    fn default() -> Self {
46        Self::new()
47    }
48}
49
50impl JwtAuth {
51    pub fn new() -> Self {
52        Self {}
53    }
54
55    pub fn validate(&self, _token: &str) -> SecurityResult<String> {
56        // TODO: Implement JWT validation
57        todo!("JWT validation not yet implemented")
58    }
59}