Skip to main content

auth/
config.rs

1use platform_core::{AppContext, RuntimeConfigDescriptor, RuntimeConfigScope, RuntimeConfigType};
2use std::sync::LazyLock;
3
4const SESSION_CACHE_KEY: &str = "auth.session_cache";
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub enum SessionCacheMode {
8    Database,
9    Redis,
10}
11
12#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13pub struct AuthRuntimeConfig {
14    pub session_cache: SessionCacheMode,
15}
16
17impl AuthRuntimeConfig {
18    #[must_use]
19    pub fn from_context(ctx: &AppContext) -> Self {
20        ctx.runtime_config
21            .snapshot()
22            .raw(SESSION_CACHE_KEY)
23            .and_then(serde_json::Value::as_str)
24            .and_then(SessionCacheMode::from_value)
25            .map_or_else(Self::default, |session_cache| Self { session_cache })
26    }
27}
28
29impl Default for AuthRuntimeConfig {
30    fn default() -> Self {
31        Self {
32            session_cache: SessionCacheMode::Database,
33        }
34    }
35}
36
37impl SessionCacheMode {
38    fn from_value(value: &str) -> Option<Self> {
39        match value {
40            "database" => Some(Self::Database),
41            "redis" => Some(Self::Redis),
42            _ => None,
43        }
44    }
45}
46
47pub static RUNTIME_CONFIG: LazyLock<Vec<RuntimeConfigDescriptor>> = LazyLock::new(|| {
48    vec![RuntimeConfigDescriptor {
49        key: SESSION_CACHE_KEY.to_owned(),
50        scope: RuntimeConfigScope::Shared,
51        group: None,
52        section: None,
53        order: 10,
54        visible_when: None,
55        generated: None,
56        value_type: RuntimeConfigType::Enum(&["database", "redis"]),
57        default: serde_json::json!("database"),
58        editable: true,
59        restart_only: true,
60        description: "Session cache backend used by auth session resolution.",
61    }]
62});