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