mirror of
https://github.com/LemmyNet/lemmy.git
synced 2024-12-27 13:21:32 +00:00
Federate actor public keys
This commit is contained in:
parent
5e3902a3bc
commit
17d3d2492c
4 changed files with 75 additions and 26 deletions
|
@ -1,4 +1,5 @@
|
|||
use crate::apub::fetcher::{fetch_remote_object, fetch_remote_user};
|
||||
use crate::apub::signatures::PublicKey;
|
||||
use crate::apub::*;
|
||||
use crate::db::community::{Community, CommunityForm};
|
||||
use crate::db::community_view::CommunityFollowerView;
|
||||
|
@ -54,15 +55,13 @@ pub async fn get_apub_community_list(
|
|||
|
||||
impl Community {
|
||||
fn as_group(&self, conn: &PgConnection) -> Result<GroupExt, Error> {
|
||||
let base_url = make_apub_endpoint(EndpointType::Community, &self.name);
|
||||
|
||||
let mut group = Group::default();
|
||||
let oprops: &mut ObjectProperties = group.as_mut();
|
||||
|
||||
let creator = User_::read(conn, self.creator_id)?;
|
||||
oprops
|
||||
.set_context_xsd_any_uri(context())?
|
||||
.set_id(base_url.to_owned())?
|
||||
.set_id(self.actor_id.to_owned())?
|
||||
.set_name_xsd_string(self.name.to_owned())?
|
||||
.set_published(convert_datetime(self.published))?
|
||||
.set_attributed_to_xsd_any_uri(make_apub_endpoint(EndpointType::User, &creator.name))?;
|
||||
|
@ -80,24 +79,33 @@ impl Community {
|
|||
|
||||
actor_props
|
||||
.set_preferred_username(self.title.to_owned())?
|
||||
.set_inbox(format!("{}/inbox", &base_url))?
|
||||
.set_outbox(format!("{}/outbox", &base_url))?
|
||||
.set_followers(format!("{}/followers", &base_url))?;
|
||||
.set_inbox(format!("{}/inbox", &self.actor_id))?
|
||||
.set_outbox(format!("{}/outbox", &self.actor_id))?
|
||||
.set_followers(format!("{}/followers", &self.actor_id))?;
|
||||
|
||||
Ok(group.extend(actor_props))
|
||||
let public_key = PublicKey {
|
||||
id: format!("{}#main-key", self.actor_id),
|
||||
owner: self.actor_id.to_owned(),
|
||||
public_key_pem: self.public_key.to_owned().unwrap(),
|
||||
};
|
||||
|
||||
Ok(group.extend(actor_props).extend(public_key.to_ext()))
|
||||
}
|
||||
}
|
||||
|
||||
impl CommunityForm {
|
||||
pub fn from_group(group: &GroupExt, conn: &PgConnection) -> Result<Self, Error> {
|
||||
let followers_uri = Url::parse(&group.extension.get_followers().unwrap().to_string())?;
|
||||
let outbox_uri = Url::parse(&group.extension.get_outbox().to_string())?;
|
||||
let oprops = &group.base.base.object_props;
|
||||
let aprops = &group.base.extension;
|
||||
let public_key: &PublicKey = &group.extension.public_key;
|
||||
|
||||
let followers_uri = Url::parse(&aprops.get_followers().unwrap().to_string())?;
|
||||
let outbox_uri = Url::parse(&aprops.get_outbox().to_string())?;
|
||||
let _outbox = fetch_remote_object::<OrderedCollection>(&outbox_uri)?;
|
||||
let _followers = fetch_remote_object::<UnorderedCollection>(&followers_uri)?;
|
||||
let oprops = &group.base.object_props;
|
||||
let aprops = &group.extension;
|
||||
let apub_id = Url::parse(&oprops.get_attributed_to_xsd_any_uri().unwrap().to_string())?;
|
||||
let creator = fetch_remote_user(&apub_id, conn)?;
|
||||
|
||||
Ok(CommunityForm {
|
||||
name: oprops.get_name_xsd_string().unwrap().to_string(),
|
||||
title: aprops.get_preferred_username().unwrap().to_string(),
|
||||
|
@ -118,7 +126,7 @@ impl CommunityForm {
|
|||
actor_id: oprops.get_id().unwrap().to_string(),
|
||||
local: false,
|
||||
private_key: None,
|
||||
public_key: None,
|
||||
public_key: Some(public_key.to_owned().public_key_pem),
|
||||
last_refreshed_at: Some(naive_now()),
|
||||
})
|
||||
}
|
||||
|
|
|
@ -3,7 +3,9 @@ pub mod community;
|
|||
pub mod fetcher;
|
||||
pub mod inbox;
|
||||
pub mod post;
|
||||
pub mod signatures;
|
||||
pub mod user;
|
||||
use crate::apub::signatures::PublicKeyExtension;
|
||||
use crate::Settings;
|
||||
use activitystreams::actor::{properties::ApActorProperties, Group, Person};
|
||||
use activitystreams::ext::Ext;
|
||||
|
@ -12,8 +14,8 @@ use actix_web::HttpResponse;
|
|||
use openssl::{pkey::PKey, rsa::Rsa};
|
||||
use url::Url;
|
||||
|
||||
type GroupExt = Ext<Group, ApActorProperties>;
|
||||
type PersonExt = Ext<Person, ApActorProperties>;
|
||||
type GroupExt = Ext<Ext<Group, ApActorProperties>, PublicKeyExtension>;
|
||||
type PersonExt = Ext<Ext<Person, ApActorProperties>, PublicKeyExtension>;
|
||||
|
||||
static APUB_JSON_CONTENT_TYPE: &str = "application/activity+json";
|
||||
|
||||
|
|
30
server/src/apub/signatures.rs
Normal file
30
server/src/apub/signatures.rs
Normal file
|
@ -0,0 +1,30 @@
|
|||
// For this example, we'll use the Extensible trait, the Extension trait, the Actor trait, and
|
||||
// the Person type
|
||||
use activitystreams::{actor::Actor, ext::Extension};
|
||||
|
||||
// The following is taken from here:
|
||||
// https://docs.rs/activitystreams/0.5.0-alpha.17/activitystreams/ext/index.html
|
||||
|
||||
#[derive(Clone, Debug, Default, serde::Deserialize, serde::Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct PublicKey {
|
||||
pub id: String,
|
||||
pub owner: String,
|
||||
pub public_key_pem: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, serde::Deserialize, serde::Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct PublicKeyExtension {
|
||||
pub public_key: PublicKey,
|
||||
}
|
||||
|
||||
impl PublicKey {
|
||||
pub fn to_ext(&self) -> PublicKeyExtension {
|
||||
PublicKeyExtension {
|
||||
public_key: self.to_owned(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Extension<T> for PublicKeyExtension where T: Actor {}
|
|
@ -1,4 +1,5 @@
|
|||
use crate::apub::{create_apub_response, make_apub_endpoint, EndpointType, PersonExt};
|
||||
use crate::apub::signatures::PublicKey;
|
||||
use crate::apub::{create_apub_response, PersonExt};
|
||||
use crate::db::user::{UserForm, User_};
|
||||
use crate::{convert_datetime, naive_now};
|
||||
use activitystreams::{
|
||||
|
@ -25,15 +26,13 @@ pub async fn get_apub_user(
|
|||
info: Path<UserQuery>,
|
||||
db: web::Data<Pool<ConnectionManager<PgConnection>>>,
|
||||
) -> Result<HttpResponse<Body>, Error> {
|
||||
dbg!(&info.user_name);
|
||||
let user = User_::find_by_email_or_username(&&db.get()?, &info.user_name)?;
|
||||
let base_url = make_apub_endpoint(EndpointType::User, &user.name);
|
||||
|
||||
let mut person = Person::default();
|
||||
let oprops: &mut ObjectProperties = person.as_mut();
|
||||
oprops
|
||||
.set_context_xsd_any_uri(context())?
|
||||
.set_id(base_url.to_string())?
|
||||
.set_id(user.actor_id.to_string())?
|
||||
.set_name_xsd_string(user.name.to_owned())?
|
||||
.set_published(convert_datetime(user.published))?;
|
||||
|
||||
|
@ -48,18 +47,28 @@ pub async fn get_apub_user(
|
|||
let mut actor_props = ApActorProperties::default();
|
||||
|
||||
actor_props
|
||||
.set_inbox(format!("{}/inbox", &base_url))?
|
||||
.set_outbox(format!("{}/outbox", &base_url))?
|
||||
.set_following(format!("{}/following", &base_url))?
|
||||
.set_liked(format!("{}/liked", &base_url))?;
|
||||
.set_inbox(format!("{}/inbox", &user.actor_id))?
|
||||
.set_outbox(format!("{}/outbox", &user.actor_id))?
|
||||
.set_following(format!("{}/following", &user.actor_id))?
|
||||
.set_liked(format!("{}/liked", &user.actor_id))?;
|
||||
|
||||
Ok(create_apub_response(&person.extend(actor_props)))
|
||||
let public_key = PublicKey {
|
||||
id: format!("{}#main-key", user.actor_id),
|
||||
owner: user.actor_id.to_owned(),
|
||||
public_key_pem: user.public_key.unwrap(),
|
||||
};
|
||||
|
||||
Ok(create_apub_response(
|
||||
&person.extend(actor_props).extend(public_key.to_ext()),
|
||||
))
|
||||
}
|
||||
|
||||
impl UserForm {
|
||||
pub fn from_person(person: &PersonExt) -> Result<Self, Error> {
|
||||
let oprops = &person.base.object_props;
|
||||
let aprops = &person.extension;
|
||||
let oprops = &person.base.base.object_props;
|
||||
let aprops = &person.base.extension;
|
||||
let public_key: &PublicKey = &person.extension.public_key;
|
||||
|
||||
Ok(UserForm {
|
||||
name: oprops.get_name_xsd_string().unwrap().to_string(),
|
||||
preferred_username: aprops.get_preferred_username().map(|u| u.to_string()),
|
||||
|
@ -83,7 +92,7 @@ impl UserForm {
|
|||
bio: oprops.get_summary_xsd_string().map(|s| s.to_string()),
|
||||
local: false,
|
||||
private_key: None,
|
||||
public_key: None,
|
||||
public_key: Some(public_key.to_owned().public_key_pem),
|
||||
last_refreshed_at: Some(naive_now()),
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue