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//!
7//! Copyright (c) systemprompt.io — Business Source License 1.1.
8//! See <https://systemprompt.io> for licensing details.
9
10mod session;
11mod session_file;
12mod store;
13
14use serde::{Deserialize, Serialize};
15use systemprompt_identifiers::TenantId;
16
17pub use session::{CliSession, CliSessionBuilder, SessionBinding, SessionIdentity};
18pub use store::SessionStore;
19
20pub const LOCAL_SESSION_KEY: &str = "local";
21
22#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
23#[serde(tag = "type", content = "value")]
24pub enum SessionKey {
25    Local,
26    Tenant(TenantId),
27}
28
29impl SessionKey {
30    #[must_use]
31    pub fn from_tenant_id(tenant_id: Option<&TenantId>) -> Self {
32        tenant_id.map_or(Self::Local, |id| Self::Tenant(id.clone()))
33    }
34
35    #[must_use]
36    pub fn as_storage_key(&self) -> String {
37        match self {
38            Self::Local => LOCAL_SESSION_KEY.to_owned(),
39            Self::Tenant(id) => format!("tenant_{}", id),
40        }
41    }
42
43    #[must_use]
44    pub const fn tenant_id(&self) -> Option<&TenantId> {
45        match self {
46            Self::Local => None,
47            Self::Tenant(id) => Some(id),
48        }
49    }
50
51    #[must_use]
52    pub fn tenant_id_str(&self) -> Option<&str> {
53        self.tenant_id().map(TenantId::as_str)
54    }
55
56    #[must_use]
57    pub const fn is_local(&self) -> bool {
58        matches!(self, Self::Local)
59    }
60}
61
62impl std::fmt::Display for SessionKey {
63    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
64        match self {
65            Self::Local => write!(f, "local"),
66            Self::Tenant(id) => write!(f, "tenant:{}", id),
67        }
68    }
69}