Work towards authorized fetch

This commit is contained in:
Violet white 2020-01-12 22:09:56 -05:00
parent f3c05dae62
commit 2b4e802914
3 changed files with 24 additions and 8 deletions

View file

@ -42,6 +42,7 @@ pub struct NewInstance {
lazy_static! { lazy_static! {
static ref LOCAL_INSTANCE: RwLock<Option<Instance>> = RwLock::new(None); static ref LOCAL_INSTANCE: RwLock<Option<Instance>> = RwLock::new(None);
static ref INSTANCE_USER: RwLock<Option<User>> = RwLock::new(None);
} }
impl Instance { impl Instance {
@ -57,6 +58,14 @@ impl Instance {
.ok_or(Error::NotFound) .ok_or(Error::NotFound)
} }
pub fn set_local_user(u: User) {
INSTANCE_USER.write().unwrap().replace(u);
}
pub fn get_local_user() -> Result<User> {
INSTANCE_USER.read().unwrap().clone().ok_or(Error::NotFound)
}
pub fn get_local_uncached(conn: &Connection) -> Result<Instance> { pub fn get_local_uncached(conn: &Connection) -> Result<Instance> {
instances::table instances::table
.filter(instances::local.eq(true)) .filter(instances::local.eq(true))

View file

@ -29,7 +29,6 @@ table! {
is_owner -> Bool, is_owner -> Bool,
} }
} }
table! { table! {
blogs (id) { blogs (id) {
id -> Int4, id -> Int4,

View file

@ -230,20 +230,28 @@ impl User {
} }
fn fetch(url: &str) -> Result<CustomPerson> { fn fetch(url: &str) -> Result<CustomPerson> {
let mut headers = plume_common::activity_pub::request::headers();
headers.insert(
ACCEPT,
HeaderValue::from_str(
&ap_accept_header()
.into_iter()
.collect::<Vec<_>>()
.join(", "),
)?,
);
let lu = Instance::get_local_user()?;
let mut res = ClientBuilder::new() let mut res = ClientBuilder::new()
.connect_timeout(Some(std::time::Duration::from_secs(5))) .connect_timeout(Some(std::time::Duration::from_secs(5)))
.build()? .build()?
.get(url) .get(url)
.headers(headers.clone())
.header( .header(
ACCEPT, "Signature",
HeaderValue::from_str( plume_common::activity_pub::request::signature(&lu, &headers).expect(""),
&ap_accept_header()
.into_iter()
.collect::<Vec<_>>()
.join(", "),
)?,
) )
.send()?; .send()?;
let text = &res.text()?; let text = &res.text()?;
// without this workaround, publicKey is not correctly deserialized // without this workaround, publicKey is not correctly deserialized
let ap_sign = serde_json::from_str::<ApSignature>(text)?; let ap_sign = serde_json::from_str::<ApSignature>(text)?;