1use anyhow::Result;
2use chrono::{DateTime, Utc};
3use serde::{Deserialize, Serialize};
4
5use crate::keychain::Keychain;
6
7#[derive(Debug, Clone, Serialize, Deserialize)]
8pub struct Session {
9 pub access_jwt: String,
10 pub refresh_jwt: String,
11 pub did: String,
12 pub handle: String,
13 #[serde(default)]
14 pub pds: Option<String>,
15 #[serde(default)]
16 pub created_at: DateTime<Utc>,
17}
18
19impl Default for Session {
20 fn default() -> Self {
21 Self {
22 access_jwt: String::new(),
23 refresh_jwt: String::new(),
24 did: String::new(),
25 handle: String::new(),
26 pds: None,
27 created_at: Utc::now(),
28 }
29 }
30}
31
32pub struct SessionManager {
33 service: String,
34 account: String,
35}
36
37impl Default for SessionManager {
38 fn default() -> Self {
39 Self {
40 service: "tangled-cli".into(),
41 account: "default".into(),
42 }
43 }
44}
45
46impl SessionManager {
47 pub fn new(service: &str, account: &str) -> Self {
48 Self {
49 service: service.into(),
50 account: account.into(),
51 }
52 }
53
54 pub fn save(&self, session: &Session) -> Result<()> {
55 let keychain = Keychain::new(&self.service, &self.account);
56 let json = serde_json::to_string(session)?;
57 keychain.set_password(&json)
58 }
59
60 pub fn load(&self) -> Result<Option<Session>> {
61 let keychain = Keychain::new(&self.service, &self.account);
62 match keychain.get_password() {
63 Ok(json) => Ok(Some(serde_json::from_str(&json)?)),
64 Err(_) => Ok(None),
65 }
66 }
67
68 pub fn clear(&self) -> Result<()> {
69 let keychain = Keychain::new(&self.service, &self.account);
70 keychain.delete_password()
71 }
72}