119 lines
3.4 KiB
Rust
119 lines
3.4 KiB
Rust
//! 会话持久化:token + 用户资料存到应用数据目录。
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
use std::path::PathBuf;
|
|
use std::sync::Mutex;
|
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize, Default)]
|
|
pub struct Session {
|
|
// 旧版本没有 origin:不能推断其 token 属于哪个镜像,升级后必须重新登录。
|
|
#[serde(default)]
|
|
pub origin: String,
|
|
#[serde(default)]
|
|
pub token: String,
|
|
#[serde(default)]
|
|
pub user: serde_json::Value,
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use serde_json::json;
|
|
|
|
#[test]
|
|
fn credentials_are_bound_to_origin() {
|
|
let store = SessionStore::new();
|
|
store.set(
|
|
"https://huangguoai.com",
|
|
"secret-token".into(),
|
|
json!({"id": 1}),
|
|
);
|
|
assert_eq!(
|
|
store.token("https://huangguoai.com").as_deref(),
|
|
Some("secret-token")
|
|
);
|
|
assert_eq!(store.token("https://huangguoai.ai"), None);
|
|
assert_eq!(store.token("https://evil.example"), None);
|
|
assert!(store.user("https://evil.example").is_null());
|
|
store.clear();
|
|
assert_eq!(store.token("https://huangguoai.com"), None);
|
|
}
|
|
|
|
#[test]
|
|
fn legacy_sessions_are_not_assigned_to_current_line() {
|
|
let store = SessionStore::new();
|
|
*store.current.lock().unwrap() =
|
|
serde_json::from_str(r#"{"token":"legacy","user":{"id":1}}"#).unwrap();
|
|
assert_eq!(store.token("https://huangguoai.com"), None);
|
|
assert!(store.user("https://huangguoai.com").is_null());
|
|
}
|
|
}
|
|
|
|
pub struct SessionStore {
|
|
pub file: Mutex<Option<PathBuf>>,
|
|
pub current: Mutex<Session>,
|
|
}
|
|
|
|
impl SessionStore {
|
|
pub fn new() -> Self {
|
|
Self {
|
|
file: Mutex::new(None),
|
|
current: Mutex::new(Session::default()),
|
|
}
|
|
}
|
|
|
|
pub fn bind(&self, dir: PathBuf) {
|
|
let mut path = dir;
|
|
path.push("session.json");
|
|
if let Ok(text) = std::fs::read_to_string(&path) {
|
|
if let Ok(sess) = serde_json::from_str::<Session>(&text) {
|
|
*self.current.lock().unwrap() = sess;
|
|
}
|
|
}
|
|
*self.file.lock().unwrap() = Some(path);
|
|
}
|
|
|
|
pub fn token(&self, origin: &str) -> Option<String> {
|
|
let cur = self.current.lock().unwrap();
|
|
(cur.origin == origin && !cur.token.is_empty()).then(|| cur.token.clone())
|
|
}
|
|
|
|
pub fn user(&self, origin: &str) -> serde_json::Value {
|
|
let cur = self.current.lock().unwrap();
|
|
if cur.origin == origin && !cur.token.is_empty() {
|
|
cur.user.clone()
|
|
} else {
|
|
serde_json::Value::Null
|
|
}
|
|
}
|
|
|
|
pub fn set(&self, origin: &str, token: String, user: serde_json::Value) {
|
|
{
|
|
let mut cur = self.current.lock().unwrap();
|
|
cur.origin = origin.to_string();
|
|
cur.token = token;
|
|
cur.user = user;
|
|
}
|
|
self.persist();
|
|
}
|
|
|
|
pub fn clear(&self) {
|
|
{
|
|
let mut cur = self.current.lock().unwrap();
|
|
cur.token.clear();
|
|
cur.origin.clear();
|
|
cur.user = serde_json::Value::Null;
|
|
}
|
|
self.persist();
|
|
}
|
|
|
|
fn persist(&self) {
|
|
let snapshot = self.current.lock().unwrap().clone();
|
|
if let Some(path) = self.file.lock().unwrap().as_ref() {
|
|
if let Ok(text) = serde_json::to_string_pretty(&snapshot) {
|
|
let _ = std::fs::write(path, text);
|
|
}
|
|
}
|
|
}
|
|
}
|