Skip to main content

systemprompt_cloud/cli_session/
mod.rs

1//! Persisted CLI authentication sessions, keyed per local or per tenant.
2//!
3//! Exposes [`SessionKey`] (the local-or-tenant discriminator used as a storage
4//! key), the [`CliSession`] record and its [`CliSessionBuilder`], and the
5//! [`SessionStore`] that loads and saves sessions on disk.
6
7mod session;
8mod store;
9
10use serde::{Deserialize, Serialize};
11use systemprompt_identifiers::TenantId;
12
13pub use session::{CliSession, CliSessionBuilder, SessionIdentity};
14pub use store::SessionStore;
15
16pub const LOCAL_SESSION_KEY: &str = "local";
17
18#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
19#[serde(tag = "type", content = "value")]
20pub enum SessionKey {
21    Local,
22    Tenant(TenantId),
23}
24
25impl SessionKey {
26    #[must_use]
27    pub fn from_tenant_id(tenant_id: Option<&TenantId>) -> Self {
28        tenant_id.map_or(Self::Local, |id| Self::Tenant(id.clone()))
29    }
30
31    #[must_use]
32    pub fn as_storage_key(&self) -> String {
33        match self {
34            Self::Local => LOCAL_SESSION_KEY.to_owned(),
35            Self::Tenant(id) => format!("tenant_{}", id),
36        }
37    }
38
39    #[must_use]
40    pub const fn tenant_id(&self) -> Option<&TenantId> {
41        match self {
42            Self::Local => None,
43            Self::Tenant(id) => Some(id),
44        }
45    }
46
47    #[must_use]
48    pub fn tenant_id_str(&self) -> Option<&str> {
49        self.tenant_id().map(TenantId::as_str)
50    }
51
52    #[must_use]
53    pub const fn is_local(&self) -> bool {
54        matches!(self, Self::Local)
55    }
56}
57
58impl std::fmt::Display for SessionKey {
59    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
60        match self {
61            Self::Local => write!(f, "local"),
62            Self::Tenant(id) => write!(f, "tenant:{}", id),
63        }
64    }
65}