Something went wrong. Try again.
This repository has no description
Something went wrong. Try again.
3.6 kB · 139 lines
Rust
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139use anyhow::{Context, Result};use directories::ProjectDirs;use serde::{Deserialize, Serialize};use std::fs;use std::path::PathBuf;
use crate::client::Session;
#[derive(Debug, Clone, Serialize, Deserialize)]pub struct Credentials { pub username: String, pub password: String, pub pds_uri: String,}
impl Credentials { pub fn new(username: String, password: String, pds_uri: String) -> Self { Self { username, password, pds_uri, } }}
pub struct CredentialStore { config_dir: PathBuf,}
impl CredentialStore { pub fn new() -> Result<Self> { let project_dirs = ProjectDirs::from("com", "thoughtstream", "think") .context("Failed to determine project directories")?;
let config_dir = project_dirs.config_dir().to_path_buf(); // Create config directory if it doesn't exist fs::create_dir_all(&config_dir) .context("Failed to create config directory")?;
Ok(Self { config_dir }) }
fn credentials_path(&self) -> PathBuf { self.config_dir.join("credentials.json") }
fn session_path(&self) -> PathBuf { self.config_dir.join("session.json") }
pub fn store(&self, credentials: &Credentials) -> Result<()> { let json = serde_json::to_string_pretty(credentials) .context("Failed to serialize credentials")?; fs::write(self.credentials_path(), json) .context("Failed to write credentials file")?;
println!("Credentials stored successfully"); Ok(()) }
pub fn load(&self) -> Result<Option<Credentials>> { let path = self.credentials_path(); if !path.exists() { return Ok(None); }
let json = fs::read_to_string(&path) .context("Failed to read credentials file")?; let credentials: Credentials = serde_json::from_str(&json) .context("Failed to parse credentials file")?;
Ok(Some(credentials)) }
pub fn clear(&self) -> Result<()> { let path = self.credentials_path(); if path.exists() { fs::remove_file(&path) .context("Failed to remove credentials file")?; println!("Credentials cleared successfully"); } else { println!("No credentials found to clear"); }
self.clear_session()?;
Ok(()) }
pub fn exists(&self) -> bool { self.credentials_path().exists() }
pub fn store_session(&self, session: &Session) -> Result<()> { let json = serde_json::to_string_pretty(session) .context("Failed to serialize session")?; fs::write(self.session_path(), json) .context("Failed to write session file")?;
Ok(()) }
pub fn load_session(&self) -> Result<Option<Session>> { let path = self.session_path(); if !path.exists() { return Ok(None); }
let json = fs::read_to_string(&path) .context("Failed to read session file")?; let session: Session = serde_json::from_str(&json) .context("Failed to parse session file")?;
Ok(Some(session)) }
pub fn clear_session(&self) -> Result<()> { let path = self.session_path(); if path.exists() { fs::remove_file(&path) .context("Failed to remove session file")?; }
Ok(()) }
pub fn session_exists(&self) -> bool { self.session_path().exists() }}