Skip to main content

systemprompt_cloud/
credentials_bootstrap.rs

1use anyhow::Result;
2use chrono::Utc;
3use std::path::Path;
4use std::sync::OnceLock;
5
6use crate::{CloudApiClient, CloudCredentials};
7
8static CREDENTIALS: OnceLock<Option<CloudCredentials>> = OnceLock::new();
9
10#[derive(Debug, Clone, Copy)]
11pub struct CredentialsBootstrap;
12
13#[derive(Debug, thiserror::Error)]
14pub enum CredentialsBootstrapError {
15    #[error(
16        "Credentials not initialized. Call CredentialsBootstrap::init() after \
17         ProfileBootstrap::init()"
18    )]
19    NotInitialized,
20
21    #[error("Credentials already initialized")]
22    AlreadyInitialized,
23
24    #[error("Cloud credentials not available")]
25    NotAvailable,
26
27    #[error("Cloud credentials file not found: {path}")]
28    FileNotFound { path: String },
29
30    #[error("Cloud credentials file invalid: {message}")]
31    InvalidCredentials { message: String },
32
33    #[error("Cloud token has expired. Run 'systemprompt cloud login' to refresh")]
34    TokenExpired,
35
36    #[error("Cloud API validation failed: {message}")]
37    ApiValidationFailed { message: String },
38}
39
40impl CredentialsBootstrap {
41    pub async fn init() -> Result<Option<&'static CloudCredentials>> {
42        if CREDENTIALS.get().is_some() {
43            anyhow::bail!(CredentialsBootstrapError::AlreadyInitialized);
44        }
45
46        if Self::is_fly_container() {
47            tracing::debug!("Fly.io container detected, loading credentials from environment");
48            let creds = Self::load_from_env();
49            if let Some(ref c) = creds {
50                if let Err(e) = Self::validate_with_api(c).await {
51                    tracing::warn!(
52                        error = %e,
53                        "Cloud credential validation failed on Fly.io, continuing with unvalidated credentials"
54                    );
55                }
56            }
57            CREDENTIALS
58                .set(creds)
59                .map_err(|_| CredentialsBootstrapError::AlreadyInitialized)?;
60            return Ok(CREDENTIALS
61                .get()
62                .ok_or(CredentialsBootstrapError::NotInitialized)?
63                .as_ref());
64        }
65
66        let cloud_paths = crate::paths::get_cloud_paths();
67        let credentials_path = cloud_paths.resolve(crate::paths::CloudPath::Credentials);
68
69        let creds = Self::load_credentials_from_path(&credentials_path)?;
70        Self::validate_with_api(&creds).await?;
71
72        CREDENTIALS
73            .set(Some(creds))
74            .map_err(|_| CredentialsBootstrapError::AlreadyInitialized)?;
75        Ok(CREDENTIALS
76            .get()
77            .ok_or(CredentialsBootstrapError::NotInitialized)?
78            .as_ref())
79    }
80
81    async fn validate_with_api(creds: &CloudCredentials) -> Result<()> {
82        let client = CloudApiClient::new(&creds.api_url, &creds.api_token)?;
83        client.get_user().await.map_err(|e| {
84            anyhow::anyhow!(CredentialsBootstrapError::ApiValidationFailed {
85                message: e.to_string()
86            })
87        })?;
88        tracing::debug!("Cloud credentials validated with API");
89        Ok(())
90    }
91
92    fn is_fly_container() -> bool {
93        std::env::var("FLY_APP_NAME").is_ok()
94    }
95
96    fn load_from_env() -> Option<CloudCredentials> {
97        let api_token = std::env::var("SYSTEMPROMPT_API_TOKEN")
98            .ok()
99            .filter(|s| !s.is_empty())?;
100
101        let user_email = std::env::var("SYSTEMPROMPT_USER_EMAIL")
102            .ok()
103            .filter(|s| !s.is_empty())?;
104
105        tracing::debug!("Loading cloud credentials from environment variables");
106
107        Some(CloudCredentials {
108            api_token,
109            api_url: std::env::var("SYSTEMPROMPT_API_URL")
110                .ok()
111                .filter(|s| !s.is_empty())
112                .unwrap_or_else(|| "https://api.systemprompt.io".into()),
113            authenticated_at: Utc::now(),
114            user_email,
115        })
116    }
117
118    pub fn get() -> Result<Option<&'static CloudCredentials>, CredentialsBootstrapError> {
119        CREDENTIALS
120            .get()
121            .map(|opt| opt.as_ref())
122            .ok_or(CredentialsBootstrapError::NotInitialized)
123    }
124
125    pub fn require() -> Result<&'static CloudCredentials, CredentialsBootstrapError> {
126        Self::get()?.ok_or(CredentialsBootstrapError::NotAvailable)
127    }
128
129    pub fn is_initialized() -> bool {
130        CREDENTIALS.get().is_some()
131    }
132
133    pub fn init_empty() {
134        let _ = CREDENTIALS.set(None);
135    }
136
137    pub async fn try_init() -> Result<Option<&'static CloudCredentials>> {
138        if CREDENTIALS.get().is_some() {
139            return Self::get().map_err(Into::into);
140        }
141        Self::init().await
142    }
143
144    pub fn expires_within(duration: chrono::Duration) -> bool {
145        match Self::get() {
146            Ok(Some(c)) => c.expires_within(duration),
147            Ok(None) => false,
148            Err(e) => {
149                tracing::debug!(error = %e, "Credentials not available for expiry check");
150                false
151            },
152        }
153    }
154
155    pub async fn reload() -> Result<CloudCredentials, CredentialsBootstrapError> {
156        let cloud_paths = crate::paths::get_cloud_paths();
157        let credentials_path = cloud_paths.resolve(crate::paths::CloudPath::Credentials);
158
159        let creds = Self::load_credentials_from_path(&credentials_path).map_err(|e| {
160            CredentialsBootstrapError::InvalidCredentials {
161                message: e.to_string(),
162            }
163        })?;
164
165        Self::validate_with_api(&creds).await.map_err(|e| {
166            CredentialsBootstrapError::ApiValidationFailed {
167                message: e.to_string(),
168            }
169        })?;
170
171        Ok(creds)
172    }
173
174    fn load_credentials_from_path(path: &Path) -> Result<CloudCredentials> {
175        let creds = CloudCredentials::load_from_path(path).map_err(|e| {
176            if path.exists() {
177                anyhow::anyhow!(CredentialsBootstrapError::InvalidCredentials {
178                    message: e.to_string(),
179                })
180            } else {
181                anyhow::anyhow!(CredentialsBootstrapError::FileNotFound {
182                    path: path.display().to_string()
183                })
184            }
185        })?;
186
187        if creds.is_token_expired() {
188            anyhow::bail!(CredentialsBootstrapError::TokenExpired);
189        }
190
191        if creds.expires_within(chrono::Duration::hours(1)) {
192            tracing::warn!(
193                "Cloud token will expire soon. Consider running 'systemprompt cloud login' to \
194                 refresh."
195            );
196        }
197
198        tracing::debug!(
199            "Loaded cloud credentials from {} (user: {:?})",
200            path.display(),
201            creds.user_email
202        );
203
204        Ok(creds)
205    }
206}
207
208#[cfg(test)]
209pub mod test_helpers {
210    use chrono::{Duration, Utc};
211
212    use crate::CloudCredentials;
213
214    pub fn valid_credentials() -> CloudCredentials {
215        CloudCredentials {
216            api_token: "test_token_valid_eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9".into(),
217            api_url: "https://api.test.systemprompt.io".into(),
218            authenticated_at: Utc::now(),
219            user_email: "test@example.com".into(),
220        }
221    }
222
223    pub fn expired_credentials() -> CloudCredentials {
224        let mut creds = valid_credentials();
225        creds.authenticated_at = Utc::now() - Duration::days(30);
226        creds
227    }
228
229    pub fn expiring_soon_credentials() -> CloudCredentials {
230        let mut creds = valid_credentials();
231        creds.authenticated_at = Utc::now() - Duration::hours(23);
232        creds
233    }
234
235    pub fn minimal_credentials() -> CloudCredentials {
236        CloudCredentials {
237            api_token: "minimal_test_token".into(),
238            api_url: "https://api.systemprompt.io".into(),
239            authenticated_at: Utc::now(),
240            user_email: "minimal@example.com".into(),
241        }
242    }
243}