Rewild Your Web
at main 104 lines 2.9 kB view raw
1--- original 2+++ modified 3@@ -5,6 +5,9 @@ 4 #![deny(unsafe_code)] 5 6 use std::fmt::{self, Debug, Display}; 7+use std::fs::File; 8+use std::io::Read; 9+use std::path::Path; 10 use std::sync::{LazyLock, OnceLock}; 11 use std::thread::{self, JoinHandle}; 12 13@@ -17,6 +20,7 @@ 14 use hyper_util::client::legacy::Error as HyperError; 15 use ipc_channel::ipc::{self, IpcSender}; 16 use ipc_channel::router::ROUTER; 17+use log::warn; 18 use malloc_size_of::malloc_size_of_is_0; 19 use malloc_size_of_derive::MallocSizeOf; 20 use mime::Mime; 21@@ -59,6 +63,7 @@ 22 /// An implementation of the [Fetch specification](https://fetch.spec.whatwg.org/) 23 pub mod fetch { 24 pub mod headers; 25+ pub mod utils; 26 } 27 28 /// A loading context, for context-specific sniffing, as defined in 29@@ -596,6 +601,64 @@ 30 Prefetch, 31 } 32 33+#[derive(Clone, Debug, Deserialize, Serialize)] 34+pub struct BasicAuthCacheEntry { 35+ pub user_name: String, 36+ pub password: String, 37+} 38+ 39+#[derive(Clone, Debug, Deserialize, Serialize)] 40+pub struct BearerAuthCacheEntry { 41+ pub token: String, 42+} 43+ 44+#[derive(Clone, Debug, Deserialize, Serialize)] 45+pub enum AuthCacheEntry { 46+ Basic(BasicAuthCacheEntry), 47+ Bearer(BearerAuthCacheEntry), 48+} 49+ 50+#[derive(Clone, Debug, Deserialize, Serialize)] 51+pub struct AtProtoSessionState { 52+ pub endpoint: ServoUrl, 53+ pub access_jwt: String, 54+ pub refresh_jwt: String, 55+ /// Origins authorized to perform write operations via at:// protocol. 56+ #[serde(default)] 57+ pub authorized_origins: Vec<String>, 58+} 59+ 60+static ATPROTO_SESSION_FILE: &str = "atproto_session.json"; 61+ 62+impl AtProtoSessionState { 63+ pub fn load(config_dir: &Path) -> Option<Self> { 64+ let path = config_dir.join(ATPROTO_SESSION_FILE); 65+ 66+ let Ok(mut file) = File::open(&path) else { 67+ return None; 68+ }; 69+ 70+ let mut string_buffer: String = String::new(); 71+ if file.read_to_string(&mut string_buffer).is_err() { 72+ return None; 73+ } 74+ 75+ serde_json::from_str(&string_buffer).ok() 76+ } 77+ 78+ pub fn save(&self, config_dir: &Path) { 79+ servo_base::write_json_to_file(&self, config_dir, ATPROTO_SESSION_FILE); 80+ } 81+ 82+ // Delete the persistent storage. 83+ pub fn reset(config_dir: &Path) { 84+ let path = config_dir.join(ATPROTO_SESSION_FILE); 85+ if let Err(err) = std::fs::remove_file(path) { 86+ warn!("Failed to remove {ATPROTO_SESSION_FILE}: {err}"); 87+ } 88+ } 89+} 90+ 91 #[derive(Debug, Deserialize, Serialize)] 92 pub enum CoreResourceMsg { 93 Fetch(RequestBuilder, FetchChannels), 94@@ -655,6 +718,10 @@ 95 /// and exit 96 Exit(GenericOneshotSender<()>), 97 CollectMemoryReport(ReportsChan), 98+ /// Update the ATProto session state. 99+ UpdateAtProtoSession(Option<AtProtoSessionState>), 100+ /// Retrieve the current ATProto session state. 101+ GetAtProtoSession(IpcSender<Option<AtProtoSessionState>>), 102 } 103 104 #[derive(Clone, Debug, Deserialize, Serialize)]