mirror of
https://github.com/LemmyNet/activitypub-federation-rust
synced 2024-11-10 06:04:19 +00:00
Changes to make Lemmy work with 0.4 (#29)
* Make it work with Lemmy * working but needs cleanup * almost everything working * debug * stack overflow fix
This commit is contained in:
parent
6b3a4f8942
commit
6a65fa7c98
31 changed files with 427 additions and 269 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -4,7 +4,7 @@ version = 3
|
|||
|
||||
[[package]]
|
||||
name = "activitypub_federation"
|
||||
version = "0.4.0-rc1"
|
||||
version = "0.4.0-rc2"
|
||||
dependencies = [
|
||||
"activitystreams-kinds",
|
||||
"actix-rt",
|
||||
|
|
|
@ -9,7 +9,7 @@ The next step is to allow other servers to fetch our actors and objects. For thi
|
|||
# use activitypub_federation::axum::json::ApubJson;
|
||||
# use anyhow::Error;
|
||||
# use activitypub_federation::traits::tests::Person;
|
||||
# use activitypub_federation::config::RequestData;
|
||||
# use activitypub_federation::config::Data;
|
||||
# use activitypub_federation::traits::tests::DbConnection;
|
||||
# use axum::extract::Path;
|
||||
# use activitypub_federation::config::ApubMiddleware;
|
||||
|
@ -20,7 +20,7 @@ The next step is to allow other servers to fetch our actors and objects. For thi
|
|||
# use axum::TypedHeader;
|
||||
# use axum::response::IntoResponse;
|
||||
# use http::HeaderMap;
|
||||
# async fn generate_user_html(_: String, _: RequestData<DbConnection>) -> axum::response::Response { todo!() }
|
||||
# async fn generate_user_html(_: String, _: Data<DbConnection>) -> axum::response::Response { todo!() }
|
||||
|
||||
#[actix_rt::main]
|
||||
async fn main() -> Result<(), Error> {
|
||||
|
@ -44,7 +44,7 @@ async fn main() -> Result<(), Error> {
|
|||
async fn http_get_user(
|
||||
header_map: HeaderMap,
|
||||
Path(name): Path<String>,
|
||||
data: RequestData<DbConnection>,
|
||||
data: Data<DbConnection>,
|
||||
) -> impl IntoResponse {
|
||||
let accept = header_map.get("accept").map(|v| v.to_str().unwrap());
|
||||
if accept == Some(APUB_JSON_CONTENT_TYPE) {
|
||||
|
@ -71,7 +71,7 @@ To do this we can implement the following HTTP handler which must be bound to pa
|
|||
```rust
|
||||
# use serde::Deserialize;
|
||||
# use axum::{extract::Query, Json};
|
||||
# use activitypub_federation::config::RequestData;
|
||||
# use activitypub_federation::config::Data;
|
||||
# use activitypub_federation::fetch::webfinger::Webfinger;
|
||||
# use anyhow::Error;
|
||||
# use activitypub_federation::traits::tests::DbConnection;
|
||||
|
@ -85,7 +85,7 @@ struct WebfingerQuery {
|
|||
|
||||
async fn webfinger(
|
||||
Query(query): Query<WebfingerQuery>,
|
||||
data: RequestData<DbConnection>,
|
||||
data: Data<DbConnection>,
|
||||
) -> Result<Json<Webfinger>, Error> {
|
||||
let name = extract_webfinger_name(&query.resource, &data)?;
|
||||
let db_user = data.read_local_user(name).await?;
|
||||
|
|
|
@ -13,7 +13,7 @@ let config = FederationConfig::builder()
|
|||
.domain("example.com")
|
||||
.app_data(db_connection)
|
||||
.build()?;
|
||||
let user_id = ObjectId::<DbUser>::new("https://mastodon.social/@LemmyDev")?;
|
||||
let user_id = ObjectId::<DbUser>::parse("https://mastodon.social/@LemmyDev")?;
|
||||
let data = config.to_request_data();
|
||||
let user = user_id.dereference(&data).await;
|
||||
assert!(user.is_ok());
|
||||
|
|
|
@ -11,7 +11,7 @@ Activitypub propagates actions across servers using `Activities`. For this each
|
|||
# use activitypub_federation::traits::tests::{DbConnection, DbUser};
|
||||
# use activitystreams_kinds::activity::FollowType;
|
||||
# use activitypub_federation::traits::ActivityHandler;
|
||||
# use activitypub_federation::config::RequestData;
|
||||
# use activitypub_federation::config::Data;
|
||||
# async fn send_accept() -> Result<(), Error> { Ok(()) }
|
||||
|
||||
#[derive(Deserialize, Serialize, Clone, Debug)]
|
||||
|
@ -37,11 +37,11 @@ impl ActivityHandler for Follow {
|
|||
self.actor.inner()
|
||||
}
|
||||
|
||||
async fn verify(&self, _data: &RequestData<Self::DataType>) -> Result<(), Self::Error> {
|
||||
async fn verify(&self, _data: &Data<Self::DataType>) -> Result<(), Self::Error> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn receive(self, data: &RequestData<Self::DataType>) -> Result<(), Self::Error> {
|
||||
async fn receive(self, data: &Data<Self::DataType>) -> Result<(), Self::Error> {
|
||||
let actor = self.actor.dereference(data).await?;
|
||||
let followed = self.object.dereference(data).await?;
|
||||
data.add_follower(followed, actor).await?;
|
||||
|
@ -57,7 +57,7 @@ Next its time to setup the actual HTTP handler for the inbox. For this we first
|
|||
```
|
||||
# use axum::response::IntoResponse;
|
||||
# use activitypub_federation::axum::inbox::{ActivityData, receive_activity};
|
||||
# use activitypub_federation::config::RequestData;
|
||||
# use activitypub_federation::config::Data;
|
||||
# use activitypub_federation::protocol::context::WithContext;
|
||||
# use activitypub_federation::traits::ActivityHandler;
|
||||
# use activitypub_federation::traits::tests::{DbConnection, DbUser, Follow};
|
||||
|
@ -72,7 +72,7 @@ pub enum PersonAcceptedActivities {
|
|||
}
|
||||
|
||||
async fn http_post_user_inbox(
|
||||
data: RequestData<DbConnection>,
|
||||
data: Data<DbConnection>,
|
||||
activity_data: ActivityData,
|
||||
) -> impl IntoResponse {
|
||||
receive_activity::<WithContext<PersonAcceptedActivities>, DbUser, DbConnection>(
|
||||
|
|
|
@ -17,17 +17,16 @@ To send an activity we need to initialize our previously defined struct, and pic
|
|||
# .app_data(db_connection)
|
||||
# .build()?;
|
||||
# let data = config.to_request_data();
|
||||
# let sender = DB_USER.clone();
|
||||
# let recipient = DB_USER.clone();
|
||||
// Each actor has a keypair. Generate it on signup and store it in the database.
|
||||
let keypair = generate_actor_keypair()?;
|
||||
let activity = Follow {
|
||||
actor: ObjectId::new("https://lemmy.ml/u/nutomic")?,
|
||||
actor: ObjectId::parse("https://lemmy.ml/u/nutomic")?,
|
||||
object: recipient.apub_id.clone().into(),
|
||||
kind: Default::default(),
|
||||
id: "https://lemmy.ml/activities/321".try_into()?
|
||||
};
|
||||
let inboxes = vec![recipient.shared_inbox_or_inbox()];
|
||||
send_activity(activity, keypair.private_key, inboxes, &data).await?;
|
||||
send_activity(activity, &sender, inboxes, &data).await?;
|
||||
# Ok::<(), anyhow::Error>(())
|
||||
# }).unwrap()
|
||||
```
|
||||
|
|
|
@ -9,7 +9,7 @@ It is sometimes necessary to fetch from a URL, but we don't know the exact type
|
|||
# use activitypub_federation::config::FederationConfig;
|
||||
# use serde::{Deserialize, Serialize};
|
||||
# use activitypub_federation::traits::tests::DbConnection;
|
||||
# use activitypub_federation::config::RequestData;
|
||||
# use activitypub_federation::config::Data;
|
||||
# use url::Url;
|
||||
# use activitypub_federation::traits::tests::{Person, Note};
|
||||
|
||||
|
@ -33,25 +33,25 @@ impl ApubObject for SearchableDbObjects {
|
|||
|
||||
async fn read_from_apub_id(
|
||||
object_id: Url,
|
||||
data: &RequestData<Self::DataType>,
|
||||
data: &Data<Self::DataType>,
|
||||
) -> Result<Option<Self>, Self::Error> {
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
async fn into_apub(
|
||||
self,
|
||||
data: &RequestData<Self::DataType>,
|
||||
data: &Data<Self::DataType>,
|
||||
) -> Result<Self::ApubType, Self::Error> {
|
||||
unimplemented!();
|
||||
}
|
||||
|
||||
async fn verify(apub: &Self::ApubType, expected_domain: &Url, _data: &RequestData<Self::DataType>) -> Result<(), Self::Error> {
|
||||
async fn verify(apub: &Self::ApubType, expected_domain: &Url, _data: &Data<Self::DataType>) -> Result<(), Self::Error> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn from_apub(
|
||||
apub: Self::ApubType,
|
||||
data: &RequestData<Self::DataType>,
|
||||
data: &Data<Self::DataType>,
|
||||
) -> Result<Self, Self::Error> {
|
||||
use SearchableDbObjects::*;
|
||||
match apub {
|
||||
|
@ -66,7 +66,7 @@ async fn main() -> Result<(), anyhow::Error> {
|
|||
# let config = FederationConfig::builder().domain("example.com").app_data(DbConnection).build().unwrap();
|
||||
# let data = config.to_request_data();
|
||||
let query = "https://example.com/id/413";
|
||||
let query_result = ObjectId::<SearchableDbObjects>::new(query)?
|
||||
let query_result = ObjectId::<SearchableDbObjects>::parse(query)?
|
||||
.dereference(&data)
|
||||
.await?;
|
||||
match query_result {
|
||||
|
|
|
@ -7,7 +7,7 @@ use crate::{
|
|||
};
|
||||
use activitypub_federation::{
|
||||
activity_queue::send_activity,
|
||||
config::RequestData,
|
||||
config::Data,
|
||||
fetch::object_id::ObjectId,
|
||||
kinds::activity::CreateType,
|
||||
protocol::{context::WithContext, helpers::deserialize_one_or_many},
|
||||
|
@ -29,11 +29,7 @@ pub struct CreatePost {
|
|||
}
|
||||
|
||||
impl CreatePost {
|
||||
pub async fn send(
|
||||
note: Note,
|
||||
inbox: Url,
|
||||
data: &RequestData<DatabaseHandle>,
|
||||
) -> Result<(), Error> {
|
||||
pub async fn send(note: Note, inbox: Url, data: &Data<DatabaseHandle>) -> Result<(), Error> {
|
||||
print!("Sending reply to {}", ¬e.attributed_to);
|
||||
let create = CreatePost {
|
||||
actor: note.attributed_to.clone(),
|
||||
|
@ -43,11 +39,7 @@ impl CreatePost {
|
|||
id: generate_object_id(data.domain())?,
|
||||
};
|
||||
let create_with_context = WithContext::new_default(create);
|
||||
let private_key = data
|
||||
.local_user()
|
||||
.private_key
|
||||
.expect("local user always has private key");
|
||||
send_activity(create_with_context, private_key, vec![inbox], data).await?;
|
||||
send_activity(create_with_context, &data.local_user(), vec![inbox], data).await?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
@ -65,12 +57,12 @@ impl ActivityHandler for CreatePost {
|
|||
self.actor.inner()
|
||||
}
|
||||
|
||||
async fn verify(&self, data: &RequestData<Self::DataType>) -> Result<(), Self::Error> {
|
||||
async fn verify(&self, data: &Data<Self::DataType>) -> Result<(), Self::Error> {
|
||||
DbPost::verify(&self.object, &self.id, data).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn receive(self, data: &RequestData<Self::DataType>) -> Result<(), Self::Error> {
|
||||
async fn receive(self, data: &Data<Self::DataType>) -> Result<(), Self::Error> {
|
||||
DbPost::from_apub(self.object, data).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -8,7 +8,7 @@ use activitypub_federation::{
|
|||
inbox::{receive_activity, ActivityData},
|
||||
json::ApubJson,
|
||||
},
|
||||
config::RequestData,
|
||||
config::Data,
|
||||
fetch::webfinger::{build_webfinger_response, extract_webfinger_name, Webfinger},
|
||||
protocol::context::WithContext,
|
||||
traits::ApubObject,
|
||||
|
@ -31,7 +31,7 @@ impl IntoResponse for Error {
|
|||
#[debug_handler]
|
||||
pub async fn http_get_user(
|
||||
Path(name): Path<String>,
|
||||
data: RequestData<DatabaseHandle>,
|
||||
data: Data<DatabaseHandle>,
|
||||
) -> Result<ApubJson<WithContext<Person>>, Error> {
|
||||
let db_user = data.read_user(&name)?;
|
||||
let apub_user = db_user.into_apub(&data).await?;
|
||||
|
@ -40,7 +40,7 @@ pub async fn http_get_user(
|
|||
|
||||
#[debug_handler]
|
||||
pub async fn http_post_user_inbox(
|
||||
data: RequestData<DatabaseHandle>,
|
||||
data: Data<DatabaseHandle>,
|
||||
activity_data: ActivityData,
|
||||
) -> impl IntoResponse {
|
||||
receive_activity::<WithContext<PersonAcceptedActivities>, DbUser, DatabaseHandle>(
|
||||
|
@ -58,7 +58,7 @@ pub struct WebfingerQuery {
|
|||
#[debug_handler]
|
||||
pub async fn webfinger(
|
||||
Query(query): Query<WebfingerQuery>,
|
||||
data: RequestData<DatabaseHandle>,
|
||||
data: Data<DatabaseHandle>,
|
||||
) -> Result<Json<Webfinger>, Error> {
|
||||
let name = extract_webfinger_name(&query.resource, &data)?;
|
||||
let db_user = data.read_user(&name)?;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use crate::{activities::create_post::CreatePost, database::DatabaseHandle, error::Error};
|
||||
use activitypub_federation::{
|
||||
config::RequestData,
|
||||
config::Data,
|
||||
fetch::object_id::ObjectId,
|
||||
http_signatures::generate_actor_keypair,
|
||||
kinds::actor::PersonType,
|
||||
|
@ -75,7 +75,7 @@ impl ApubObject for DbUser {
|
|||
|
||||
async fn read_from_apub_id(
|
||||
object_id: Url,
|
||||
data: &RequestData<Self::DataType>,
|
||||
data: &Data<Self::DataType>,
|
||||
) -> Result<Option<Self>, Self::Error> {
|
||||
let users = data.users.lock().unwrap();
|
||||
let res = users
|
||||
|
@ -85,24 +85,20 @@ impl ApubObject for DbUser {
|
|||
Ok(res)
|
||||
}
|
||||
|
||||
async fn into_apub(
|
||||
self,
|
||||
_data: &RequestData<Self::DataType>,
|
||||
) -> Result<Self::ApubType, Self::Error> {
|
||||
let public_key = PublicKey::new(self.ap_id.clone().into_inner(), self.public_key.clone());
|
||||
async fn into_apub(self, _data: &Data<Self::DataType>) -> Result<Self::ApubType, Self::Error> {
|
||||
Ok(Person {
|
||||
preferred_username: self.name.clone(),
|
||||
kind: Default::default(),
|
||||
id: self.ap_id.clone(),
|
||||
inbox: self.inbox,
|
||||
public_key,
|
||||
inbox: self.inbox.clone(),
|
||||
public_key: self.public_key(),
|
||||
})
|
||||
}
|
||||
|
||||
async fn verify(
|
||||
apub: &Self::ApubType,
|
||||
expected_domain: &Url,
|
||||
_data: &RequestData<Self::DataType>,
|
||||
_data: &Data<Self::DataType>,
|
||||
) -> Result<(), Self::Error> {
|
||||
verify_domains_match(apub.id.inner(), expected_domain)?;
|
||||
Ok(())
|
||||
|
@ -110,7 +106,7 @@ impl ApubObject for DbUser {
|
|||
|
||||
async fn from_apub(
|
||||
apub: Self::ApubType,
|
||||
_data: &RequestData<Self::DataType>,
|
||||
_data: &Data<Self::DataType>,
|
||||
) -> Result<Self, Self::Error> {
|
||||
Ok(DbUser {
|
||||
name: apub.preferred_username,
|
||||
|
@ -126,15 +122,19 @@ impl ApubObject for DbUser {
|
|||
}
|
||||
|
||||
impl Actor for DbUser {
|
||||
fn id(&self) -> Url {
|
||||
self.ap_id.inner().clone()
|
||||
}
|
||||
|
||||
fn public_key_pem(&self) -> &str {
|
||||
&self.public_key
|
||||
}
|
||||
|
||||
fn private_key_pem(&self) -> Option<String> {
|
||||
self.private_key.clone()
|
||||
}
|
||||
|
||||
fn inbox(&self) -> Url {
|
||||
self.inbox.clone()
|
||||
}
|
||||
|
||||
fn id(&self) -> &Url {
|
||||
self.ap_id.inner()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,7 +6,7 @@ use crate::{
|
|||
objects::person::DbUser,
|
||||
};
|
||||
use activitypub_federation::{
|
||||
config::RequestData,
|
||||
config::Data,
|
||||
fetch::object_id::ObjectId,
|
||||
kinds::{object::NoteType, public},
|
||||
protocol::{helpers::deserialize_one_or_many, verification::verify_domains_match},
|
||||
|
@ -53,22 +53,19 @@ impl ApubObject for DbPost {
|
|||
|
||||
async fn read_from_apub_id(
|
||||
_object_id: Url,
|
||||
_data: &RequestData<Self::DataType>,
|
||||
_data: &Data<Self::DataType>,
|
||||
) -> Result<Option<Self>, Self::Error> {
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
async fn into_apub(
|
||||
self,
|
||||
_data: &RequestData<Self::DataType>,
|
||||
) -> Result<Self::ApubType, Self::Error> {
|
||||
async fn into_apub(self, _data: &Data<Self::DataType>) -> Result<Self::ApubType, Self::Error> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
async fn verify(
|
||||
apub: &Self::ApubType,
|
||||
expected_domain: &Url,
|
||||
_data: &RequestData<Self::DataType>,
|
||||
_data: &Data<Self::DataType>,
|
||||
) -> Result<(), Self::Error> {
|
||||
verify_domains_match(apub.id.inner(), expected_domain)?;
|
||||
Ok(())
|
||||
|
@ -76,7 +73,7 @@ impl ApubObject for DbPost {
|
|||
|
||||
async fn from_apub(
|
||||
apub: Self::ApubType,
|
||||
data: &RequestData<Self::DataType>,
|
||||
data: &Data<Self::DataType>,
|
||||
) -> Result<Self, Self::Error> {
|
||||
println!(
|
||||
"Received post with content {} and id {}",
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use crate::{activities::follow::Follow, instance::DatabaseHandle, objects::person::DbUser};
|
||||
use activitypub_federation::{
|
||||
config::RequestData,
|
||||
config::Data,
|
||||
fetch::object_id::ObjectId,
|
||||
kinds::activity::AcceptType,
|
||||
traits::ActivityHandler,
|
||||
|
@ -42,11 +42,11 @@ impl ActivityHandler for Accept {
|
|||
self.actor.inner()
|
||||
}
|
||||
|
||||
async fn verify(&self, _data: &RequestData<Self::DataType>) -> Result<(), Self::Error> {
|
||||
async fn verify(&self, _data: &Data<Self::DataType>) -> Result<(), Self::Error> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn receive(self, _data: &RequestData<Self::DataType>) -> Result<(), Self::Error> {
|
||||
async fn receive(self, _data: &Data<Self::DataType>) -> Result<(), Self::Error> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@ use crate::{
|
|||
DbPost,
|
||||
};
|
||||
use activitypub_federation::{
|
||||
config::RequestData,
|
||||
config::Data,
|
||||
fetch::object_id::ObjectId,
|
||||
kinds::activity::CreateType,
|
||||
protocol::helpers::deserialize_one_or_many,
|
||||
|
@ -50,12 +50,12 @@ impl ActivityHandler for CreatePost {
|
|||
self.actor.inner()
|
||||
}
|
||||
|
||||
async fn verify(&self, data: &RequestData<Self::DataType>) -> Result<(), Self::Error> {
|
||||
async fn verify(&self, data: &Data<Self::DataType>) -> Result<(), Self::Error> {
|
||||
DbPost::verify(&self.object, &self.id, data).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn receive(self, data: &RequestData<Self::DataType>) -> Result<(), Self::Error> {
|
||||
async fn receive(self, data: &Data<Self::DataType>) -> Result<(), Self::Error> {
|
||||
DbPost::from_apub(self.object, data).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@ use crate::{
|
|||
objects::person::DbUser,
|
||||
};
|
||||
use activitypub_federation::{
|
||||
config::RequestData,
|
||||
config::Data,
|
||||
fetch::object_id::ObjectId,
|
||||
kinds::activity::FollowType,
|
||||
traits::{ActivityHandler, Actor},
|
||||
|
@ -47,13 +47,13 @@ impl ActivityHandler for Follow {
|
|||
self.actor.inner()
|
||||
}
|
||||
|
||||
async fn verify(&self, _data: &RequestData<Self::DataType>) -> Result<(), Self::Error> {
|
||||
async fn verify(&self, _data: &Data<Self::DataType>) -> Result<(), Self::Error> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// Ignore clippy false positive: https://github.com/rust-lang/rust-clippy/issues/6446
|
||||
#[allow(clippy::await_holding_lock)]
|
||||
async fn receive(self, data: &RequestData<Self::DataType>) -> Result<(), Self::Error> {
|
||||
async fn receive(self, data: &Data<Self::DataType>) -> Result<(), Self::Error> {
|
||||
// add to followers
|
||||
let local_user = {
|
||||
let mut users = data.users.lock().unwrap();
|
||||
|
|
|
@ -5,7 +5,7 @@ use crate::{
|
|||
};
|
||||
use activitypub_federation::{
|
||||
actix_web::inbox::receive_activity,
|
||||
config::{ApubMiddleware, FederationConfig, RequestData},
|
||||
config::{ApubMiddleware, Data, FederationConfig},
|
||||
fetch::webfinger::{build_webfinger_response, extract_webfinger_name},
|
||||
protocol::context::WithContext,
|
||||
traits::ApubObject,
|
||||
|
@ -36,7 +36,7 @@ pub fn listen(config: &FederationConfig<DatabaseHandle>) -> Result<(), Error> {
|
|||
/// Handles requests to fetch user json over HTTP
|
||||
pub async fn http_get_user(
|
||||
user_name: web::Path<String>,
|
||||
data: RequestData<DatabaseHandle>,
|
||||
data: Data<DatabaseHandle>,
|
||||
) -> Result<HttpResponse, Error> {
|
||||
let db_user = data.local_user();
|
||||
if user_name.into_inner() == db_user.name {
|
||||
|
@ -53,7 +53,7 @@ pub async fn http_get_user(
|
|||
pub async fn http_post_user_inbox(
|
||||
request: HttpRequest,
|
||||
body: Bytes,
|
||||
data: RequestData<DatabaseHandle>,
|
||||
data: Data<DatabaseHandle>,
|
||||
) -> Result<HttpResponse, Error> {
|
||||
receive_activity::<WithContext<PersonAcceptedActivities>, DbUser, DatabaseHandle>(
|
||||
request, body, &data,
|
||||
|
@ -68,7 +68,7 @@ pub struct WebfingerQuery {
|
|||
|
||||
pub async fn webfinger(
|
||||
query: web::Query<WebfingerQuery>,
|
||||
data: RequestData<DatabaseHandle>,
|
||||
data: Data<DatabaseHandle>,
|
||||
) -> Result<HttpResponse, Error> {
|
||||
let name = extract_webfinger_name(&query.resource, &data)?;
|
||||
let db_user = data.read_user(&name)?;
|
||||
|
|
|
@ -8,7 +8,7 @@ use activitypub_federation::{
|
|||
inbox::{receive_activity, ActivityData},
|
||||
json::ApubJson,
|
||||
},
|
||||
config::{ApubMiddleware, FederationConfig, RequestData},
|
||||
config::{ApubMiddleware, Data, FederationConfig},
|
||||
fetch::webfinger::{build_webfinger_response, extract_webfinger_name, Webfinger},
|
||||
protocol::context::WithContext,
|
||||
traits::ApubObject,
|
||||
|
@ -48,7 +48,7 @@ pub fn listen(config: &FederationConfig<DatabaseHandle>) -> Result<(), Error> {
|
|||
#[debug_handler]
|
||||
async fn http_get_user(
|
||||
Path(name): Path<String>,
|
||||
data: RequestData<DatabaseHandle>,
|
||||
data: Data<DatabaseHandle>,
|
||||
) -> Result<ApubJson<WithContext<Person>>, Error> {
|
||||
let db_user = data.read_user(&name)?;
|
||||
let apub_user = db_user.into_apub(&data).await?;
|
||||
|
@ -57,7 +57,7 @@ async fn http_get_user(
|
|||
|
||||
#[debug_handler]
|
||||
async fn http_post_user_inbox(
|
||||
data: RequestData<DatabaseHandle>,
|
||||
data: Data<DatabaseHandle>,
|
||||
activity_data: ActivityData,
|
||||
) -> impl IntoResponse {
|
||||
receive_activity::<WithContext<PersonAcceptedActivities>, DbUser, DatabaseHandle>(
|
||||
|
@ -75,7 +75,7 @@ struct WebfingerQuery {
|
|||
#[debug_handler]
|
||||
async fn webfinger(
|
||||
Query(query): Query<WebfingerQuery>,
|
||||
data: RequestData<DatabaseHandle>,
|
||||
data: Data<DatabaseHandle>,
|
||||
) -> Result<Json<Webfinger>, Error> {
|
||||
let name = extract_webfinger_name(&query.resource, &data)?;
|
||||
let db_user = data.read_user(&name)?;
|
||||
|
|
|
@ -7,7 +7,7 @@ use crate::{
|
|||
};
|
||||
use activitypub_federation::{
|
||||
activity_queue::send_activity,
|
||||
config::RequestData,
|
||||
config::Data,
|
||||
fetch::{object_id::ObjectId, webfinger::webfinger_resolve_actor},
|
||||
http_signatures::generate_actor_keypair,
|
||||
kinds::actor::PersonType,
|
||||
|
@ -81,15 +81,7 @@ impl DbUser {
|
|||
Ok(Url::parse(&format!("{}/followers", self.ap_id.inner()))?)
|
||||
}
|
||||
|
||||
fn public_key(&self) -> PublicKey {
|
||||
PublicKey::new(self.ap_id.clone().into_inner(), self.public_key.clone())
|
||||
}
|
||||
|
||||
pub async fn follow(
|
||||
&self,
|
||||
other: &str,
|
||||
data: &RequestData<DatabaseHandle>,
|
||||
) -> Result<(), Error> {
|
||||
pub async fn follow(&self, other: &str, data: &Data<DatabaseHandle>) -> Result<(), Error> {
|
||||
let other: DbUser = webfinger_resolve_actor(other, data).await?;
|
||||
let id = generate_object_id(data.domain())?;
|
||||
let follow = Follow::new(self.ap_id.clone(), other.ap_id.clone(), id.clone());
|
||||
|
@ -98,11 +90,7 @@ impl DbUser {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn post(
|
||||
&self,
|
||||
post: DbPost,
|
||||
data: &RequestData<DatabaseHandle>,
|
||||
) -> Result<(), Error> {
|
||||
pub async fn post(&self, post: DbPost, data: &Data<DatabaseHandle>) -> Result<(), Error> {
|
||||
let id = generate_object_id(data.domain())?;
|
||||
let create = CreatePost::new(post.into_apub(data).await?, id.clone());
|
||||
let mut inboxes = vec![];
|
||||
|
@ -118,20 +106,14 @@ impl DbUser {
|
|||
&self,
|
||||
activity: Activity,
|
||||
recipients: Vec<Url>,
|
||||
data: &RequestData<DatabaseHandle>,
|
||||
data: &Data<DatabaseHandle>,
|
||||
) -> Result<(), <Activity as ActivityHandler>::Error>
|
||||
where
|
||||
Activity: ActivityHandler + Serialize + Debug + Send + Sync,
|
||||
<Activity as ActivityHandler>::Error: From<anyhow::Error> + From<serde_json::Error>,
|
||||
{
|
||||
let activity = WithContext::new_default(activity);
|
||||
send_activity(
|
||||
activity,
|
||||
self.private_key.clone().unwrap(),
|
||||
recipients,
|
||||
data,
|
||||
)
|
||||
.await?;
|
||||
send_activity(activity, self, recipients, data).await?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
@ -148,7 +130,7 @@ impl ApubObject for DbUser {
|
|||
|
||||
async fn read_from_apub_id(
|
||||
object_id: Url,
|
||||
data: &RequestData<Self::DataType>,
|
||||
data: &Data<Self::DataType>,
|
||||
) -> Result<Option<Self>, Self::Error> {
|
||||
let users = data.users.lock().unwrap();
|
||||
let res = users
|
||||
|
@ -158,10 +140,7 @@ impl ApubObject for DbUser {
|
|||
Ok(res)
|
||||
}
|
||||
|
||||
async fn into_apub(
|
||||
self,
|
||||
_data: &RequestData<Self::DataType>,
|
||||
) -> Result<Self::ApubType, Self::Error> {
|
||||
async fn into_apub(self, _data: &Data<Self::DataType>) -> Result<Self::ApubType, Self::Error> {
|
||||
Ok(Person {
|
||||
preferred_username: self.name.clone(),
|
||||
kind: Default::default(),
|
||||
|
@ -174,7 +153,7 @@ impl ApubObject for DbUser {
|
|||
async fn verify(
|
||||
apub: &Self::ApubType,
|
||||
expected_domain: &Url,
|
||||
_data: &RequestData<Self::DataType>,
|
||||
_data: &Data<Self::DataType>,
|
||||
) -> Result<(), Self::Error> {
|
||||
verify_domains_match(apub.id.inner(), expected_domain)?;
|
||||
Ok(())
|
||||
|
@ -182,7 +161,7 @@ impl ApubObject for DbUser {
|
|||
|
||||
async fn from_apub(
|
||||
apub: Self::ApubType,
|
||||
data: &RequestData<Self::DataType>,
|
||||
data: &Data<Self::DataType>,
|
||||
) -> Result<Self, Self::Error> {
|
||||
let user = DbUser {
|
||||
name: apub.preferred_username,
|
||||
|
@ -201,14 +180,18 @@ impl ApubObject for DbUser {
|
|||
}
|
||||
|
||||
impl Actor for DbUser {
|
||||
fn id(&self) -> &Url {
|
||||
self.ap_id.inner()
|
||||
fn id(&self) -> Url {
|
||||
self.ap_id.inner().clone()
|
||||
}
|
||||
|
||||
fn public_key_pem(&self) -> &str {
|
||||
&self.public_key
|
||||
}
|
||||
|
||||
fn private_key_pem(&self) -> Option<String> {
|
||||
self.private_key.clone()
|
||||
}
|
||||
|
||||
fn inbox(&self) -> Url {
|
||||
self.inbox.clone()
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use crate::{error::Error, generate_object_id, instance::DatabaseHandle, objects::person::DbUser};
|
||||
use activitypub_federation::{
|
||||
config::RequestData,
|
||||
config::Data,
|
||||
fetch::object_id::ObjectId,
|
||||
kinds::{object::NoteType, public},
|
||||
protocol::{helpers::deserialize_one_or_many, verification::verify_domains_match},
|
||||
|
@ -49,7 +49,7 @@ impl ApubObject for DbPost {
|
|||
|
||||
async fn read_from_apub_id(
|
||||
object_id: Url,
|
||||
data: &RequestData<Self::DataType>,
|
||||
data: &Data<Self::DataType>,
|
||||
) -> Result<Option<Self>, Self::Error> {
|
||||
let posts = data.posts.lock().unwrap();
|
||||
let res = posts
|
||||
|
@ -59,10 +59,7 @@ impl ApubObject for DbPost {
|
|||
Ok(res)
|
||||
}
|
||||
|
||||
async fn into_apub(
|
||||
self,
|
||||
data: &RequestData<Self::DataType>,
|
||||
) -> Result<Self::ApubType, Self::Error> {
|
||||
async fn into_apub(self, data: &Data<Self::DataType>) -> Result<Self::ApubType, Self::Error> {
|
||||
let creator = self.creator.dereference_local(data).await?;
|
||||
Ok(Note {
|
||||
kind: Default::default(),
|
||||
|
@ -76,7 +73,7 @@ impl ApubObject for DbPost {
|
|||
async fn verify(
|
||||
apub: &Self::ApubType,
|
||||
expected_domain: &Url,
|
||||
_data: &RequestData<Self::DataType>,
|
||||
_data: &Data<Self::DataType>,
|
||||
) -> Result<(), Self::Error> {
|
||||
verify_domains_match(apub.id.inner(), expected_domain)?;
|
||||
Ok(())
|
||||
|
@ -84,7 +81,7 @@ impl ApubObject for DbPost {
|
|||
|
||||
async fn from_apub(
|
||||
apub: Self::ApubType,
|
||||
data: &RequestData<Self::DataType>,
|
||||
data: &Data<Self::DataType>,
|
||||
) -> Result<Self, Self::Error> {
|
||||
let post = DbPost {
|
||||
text: apub.content,
|
||||
|
|
|
@ -3,11 +3,11 @@
|
|||
#![doc = include_str!("../docs/09_sending_activities.md")]
|
||||
|
||||
use crate::{
|
||||
config::RequestData,
|
||||
config::Data,
|
||||
error::Error,
|
||||
http_signatures::sign_request,
|
||||
reqwest_shim::ResponseExt,
|
||||
traits::ActivityHandler,
|
||||
traits::{ActivityHandler, Actor},
|
||||
APUB_JSON_CONTENT_TYPE,
|
||||
};
|
||||
use anyhow::anyhow;
|
||||
|
@ -40,21 +40,25 @@ use url::Url;
|
|||
/// signature. Generated with [crate::http_signatures::generate_actor_keypair].
|
||||
/// - `inboxes`: List of actor inboxes that should receive the activity. Should be built by calling
|
||||
/// [crate::traits::Actor::shared_inbox_or_inbox] for each target actor.
|
||||
pub async fn send_activity<Activity, Datatype>(
|
||||
pub async fn send_activity<Activity, Datatype, ActorType>(
|
||||
activity: Activity,
|
||||
private_key: String,
|
||||
actor: &ActorType,
|
||||
inboxes: Vec<Url>,
|
||||
data: &RequestData<Datatype>,
|
||||
data: &Data<Datatype>,
|
||||
) -> Result<(), <Activity as ActivityHandler>::Error>
|
||||
where
|
||||
Activity: ActivityHandler + Serialize,
|
||||
<Activity as ActivityHandler>::Error: From<anyhow::Error> + From<serde_json::Error>,
|
||||
Datatype: Clone,
|
||||
ActorType: Actor,
|
||||
{
|
||||
let config = &data.config;
|
||||
let actor_id = activity.actor();
|
||||
let activity_id = activity.id();
|
||||
let activity_serialized = serde_json::to_string_pretty(&activity)?;
|
||||
let private_key = actor
|
||||
.private_key_pem()
|
||||
.expect("Actor for sending activity has private key");
|
||||
let inboxes: Vec<Url> = inboxes
|
||||
.into_iter()
|
||||
.unique()
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
//! Handles incoming activities, verifying HTTP signatures and other checks
|
||||
|
||||
use crate::{
|
||||
config::RequestData,
|
||||
config::Data,
|
||||
error::Error,
|
||||
fetch::object_id::ObjectId,
|
||||
http_signatures::{verify_inbox_hash, verify_signature},
|
||||
|
@ -17,7 +17,7 @@ use tracing::debug;
|
|||
pub async fn receive_activity<Activity, ActorT, Datatype>(
|
||||
request: HttpRequest,
|
||||
body: Bytes,
|
||||
data: &RequestData<Datatype>,
|
||||
data: &Data<Datatype>,
|
||||
) -> Result<HttpResponse, <Activity as ActivityHandler>::Error>
|
||||
where
|
||||
Activity: ActivityHandler<DataType = Datatype> + DeserializeOwned + Send + 'static,
|
||||
|
@ -112,8 +112,8 @@ mod test {
|
|||
let request_builder =
|
||||
ClientWithMiddleware::from(Client::default()).post("https://example.com/inbox");
|
||||
let activity = Follow {
|
||||
actor: ObjectId::new("http://localhost:123").unwrap(),
|
||||
object: ObjectId::new("http://localhost:124").unwrap(),
|
||||
actor: ObjectId::parse("http://localhost:123").unwrap(),
|
||||
object: ObjectId::parse("http://localhost:124").unwrap(),
|
||||
kind: Default::default(),
|
||||
id: "http://localhost:123/1".try_into().unwrap(),
|
||||
};
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use crate::config::{ApubMiddleware, FederationConfig, RequestData};
|
||||
use crate::config::{ApubMiddleware, Data, FederationConfig};
|
||||
use actix_web::{
|
||||
dev::{forward_ready, Payload, Service, ServiceRequest, ServiceResponse, Transform},
|
||||
Error,
|
||||
|
@ -29,7 +29,7 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
/// Passes [FederationConfig] to HTTP handlers, converting it to [RequestData] in the process
|
||||
/// Passes [FederationConfig] to HTTP handlers, converting it to [Data] in the process
|
||||
#[doc(hidden)]
|
||||
pub struct ApubService<S, T: Clone>
|
||||
where
|
||||
|
@ -61,7 +61,7 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
impl<T: Clone + 'static> FromRequest for RequestData<T> {
|
||||
impl<T: Clone + 'static> FromRequest for Data<T> {
|
||||
type Error = Error;
|
||||
type Future = Ready<Result<Self, Self::Error>>;
|
||||
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
#![doc = include_str!("../../docs/08_receiving_activities.md")]
|
||||
|
||||
use crate::{
|
||||
config::RequestData,
|
||||
config::Data,
|
||||
error::Error,
|
||||
fetch::object_id::ObjectId,
|
||||
http_signatures::{verify_inbox_hash, verify_signature},
|
||||
|
@ -23,7 +23,7 @@ use tracing::debug;
|
|||
/// Handles incoming activities, verifying HTTP signatures and other checks
|
||||
pub async fn receive_activity<Activity, ActorT, Datatype>(
|
||||
activity_data: ActivityData,
|
||||
data: &RequestData<Datatype>,
|
||||
data: &Data<Datatype>,
|
||||
) -> Result<(), <Activity as ActivityHandler>::Error>
|
||||
where
|
||||
Activity: ActivityHandler<DataType = Datatype> + DeserializeOwned + Send + 'static,
|
||||
|
|
|
@ -5,10 +5,10 @@
|
|||
//! # use axum::extract::Path;
|
||||
//! # use activitypub_federation::axum::json::ApubJson;
|
||||
//! # use activitypub_federation::protocol::context::WithContext;
|
||||
//! # use activitypub_federation::config::RequestData;
|
||||
//! # use activitypub_federation::config::Data;
|
||||
//! # use activitypub_federation::traits::ApubObject;
|
||||
//! # use activitypub_federation::traits::tests::{DbConnection, DbUser, Person};
|
||||
//! async fn http_get_user(Path(name): Path<String>, data: RequestData<DbConnection>) -> Result<ApubJson<WithContext<Person>>, Error> {
|
||||
//! async fn http_get_user(Path(name): Path<String>, data: Data<DbConnection>) -> Result<ApubJson<WithContext<Person>>, Error> {
|
||||
//! let user: DbUser = data.read_local_user(name).await?;
|
||||
//! let person = user.into_apub(&data).await?;
|
||||
//!
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use crate::config::{ApubMiddleware, FederationConfig, RequestData};
|
||||
use crate::config::{ApubMiddleware, Data, FederationConfig};
|
||||
use axum::{async_trait, body::Body, extract::FromRequestParts, http::Request, response::Response};
|
||||
use http::{request::Parts, StatusCode};
|
||||
use std::task::{Context, Poll};
|
||||
|
@ -15,7 +15,7 @@ impl<S, T: Clone> Layer<S> for ApubMiddleware<T> {
|
|||
}
|
||||
}
|
||||
|
||||
/// Passes [FederationConfig] to HTTP handlers, converting it to [RequestData] in the process
|
||||
/// Passes [FederationConfig] to HTTP handlers, converting it to [Data] in the process
|
||||
#[doc(hidden)]
|
||||
#[derive(Clone)]
|
||||
pub struct ApubService<S, T: Clone> {
|
||||
|
@ -44,7 +44,7 @@ where
|
|||
}
|
||||
|
||||
#[async_trait]
|
||||
impl<S, T: Clone + 'static> FromRequestParts<S> for RequestData<T>
|
||||
impl<S, T: Clone + 'static> FromRequestParts<S> for Data<T>
|
||||
where
|
||||
S: Send + Sync,
|
||||
T: Send + Sync,
|
||||
|
|
|
@ -28,7 +28,10 @@ use reqwest_middleware::ClientWithMiddleware;
|
|||
use serde::de::DeserializeOwned;
|
||||
use std::{
|
||||
ops::Deref,
|
||||
sync::{atomic::AtomicI32, Arc},
|
||||
sync::{
|
||||
atomic::{AtomicU32, Ordering},
|
||||
Arc,
|
||||
},
|
||||
time::Duration,
|
||||
};
|
||||
use url::Url;
|
||||
|
@ -46,7 +49,7 @@ pub struct FederationConfig<T: Clone> {
|
|||
/// Maximum number of outgoing HTTP requests per incoming HTTP request. See
|
||||
/// [crate::fetch::object_id::ObjectId] for more details.
|
||||
#[builder(default = "20")]
|
||||
pub(crate) http_fetch_limit: i32,
|
||||
pub(crate) http_fetch_limit: u32,
|
||||
#[builder(default = "reqwest::Client::default().into()")]
|
||||
/// HTTP client used for all outgoing requests. Middleware can be used to add functionality
|
||||
/// like log tracing or retry of failed requests.
|
||||
|
@ -102,9 +105,9 @@ impl<T: Clone> FederationConfig<T> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
/// Create new [RequestData] from this. You should prefer to use a middleware if possible.
|
||||
pub fn to_request_data(&self) -> RequestData<T> {
|
||||
RequestData {
|
||||
/// Create new [Data] from this. You should prefer to use a middleware if possible.
|
||||
pub fn to_request_data(&self) -> Data<T> {
|
||||
Data {
|
||||
config: self.clone(),
|
||||
request_counter: Default::default(),
|
||||
}
|
||||
|
@ -127,6 +130,11 @@ impl<T: Clone> FederationConfig<T> {
|
|||
_ => return Err(Error::UrlVerificationError("Invalid url scheme")),
|
||||
};
|
||||
|
||||
// Urls which use our local domain are not a security risk, no further verification needed
|
||||
if self.is_local_url(url) {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
if url.domain().is_none() {
|
||||
return Err(Error::UrlVerificationError("Url must have a domain"));
|
||||
}
|
||||
|
@ -137,11 +145,6 @@ impl<T: Clone> FederationConfig<T> {
|
|||
));
|
||||
}
|
||||
|
||||
// Urls which use our local domain are not a security risk, no further verification needed
|
||||
if self.is_local_url(url) {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
self.url_verifier
|
||||
.verify(url)
|
||||
.await
|
||||
|
@ -253,24 +256,36 @@ clone_trait_object!(UrlVerifier);
|
|||
/// prevent denial of service attacks, where an attacker triggers fetching of recursive objects.
|
||||
///
|
||||
/// <https://www.w3.org/TR/activitypub/#security-recursive-objects>
|
||||
pub struct RequestData<T: Clone> {
|
||||
pub struct Data<T: Clone> {
|
||||
pub(crate) config: FederationConfig<T>,
|
||||
pub(crate) request_counter: AtomicI32,
|
||||
pub(crate) request_counter: AtomicU32,
|
||||
}
|
||||
|
||||
impl<T: Clone> RequestData<T> {
|
||||
impl<T: Clone> Data<T> {
|
||||
/// Returns the data which was stored in [FederationConfigBuilder::app_data]
|
||||
pub fn app_data(&self) -> &T {
|
||||
&self.config.app_data
|
||||
}
|
||||
|
||||
/// Returns the domain that was configured in [FederationConfig].
|
||||
/// The domain that was configured in [FederationConfig].
|
||||
pub fn domain(&self) -> &str {
|
||||
&self.config.domain
|
||||
}
|
||||
|
||||
/// Returns a new instance of `Data` with request counter set to 0.
|
||||
pub fn reset_request_count(&self) -> Self {
|
||||
Data {
|
||||
config: self.config.clone(),
|
||||
request_counter: Default::default(),
|
||||
}
|
||||
}
|
||||
/// Total number of outgoing HTTP requests made with this data.
|
||||
pub fn request_count(&self) -> u32 {
|
||||
self.request_counter.load(Ordering::Relaxed)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Clone> Deref for RequestData<T> {
|
||||
impl<T: Clone> Deref for Data<T> {
|
||||
type Target = T;
|
||||
|
||||
fn deref(&self) -> &T {
|
||||
|
@ -278,7 +293,7 @@ impl<T: Clone> Deref for RequestData<T> {
|
|||
}
|
||||
}
|
||||
|
||||
/// Middleware for HTTP handlers which provides access to [RequestData]
|
||||
/// Middleware for HTTP handlers which provides access to [Data]
|
||||
#[derive(Clone)]
|
||||
pub struct ApubMiddleware<T: Clone>(pub(crate) FederationConfig<T>);
|
||||
|
||||
|
|
97
src/fetch/collection_id.rs
Normal file
97
src/fetch/collection_id.rs
Normal file
|
@ -0,0 +1,97 @@
|
|||
use crate::{config::Data, error::Error, fetch::fetch_object_http, traits::ApubCollection};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::{
|
||||
fmt::{Debug, Display, Formatter},
|
||||
marker::PhantomData,
|
||||
};
|
||||
use url::Url;
|
||||
|
||||
/// Typed wrapper for Activitypub Collection ID which helps with dereferencing.
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[serde(transparent)]
|
||||
pub struct CollectionId<Kind>(Box<Url>, PhantomData<Kind>)
|
||||
where
|
||||
Kind: ApubCollection,
|
||||
for<'de2> <Kind as ApubCollection>::ApubType: Deserialize<'de2>;
|
||||
|
||||
impl<Kind> CollectionId<Kind>
|
||||
where
|
||||
Kind: ApubCollection,
|
||||
for<'de2> <Kind as ApubCollection>::ApubType: Deserialize<'de2>,
|
||||
{
|
||||
/// Construct a new CollectionId instance
|
||||
pub fn parse<T>(url: T) -> Result<Self, url::ParseError>
|
||||
where
|
||||
T: TryInto<Url>,
|
||||
url::ParseError: From<<T as TryInto<Url>>::Error>,
|
||||
{
|
||||
Ok(Self(Box::new(url.try_into()?), PhantomData::<Kind>))
|
||||
}
|
||||
|
||||
/// Fetches collection over HTTP
|
||||
///
|
||||
/// Unlike [ObjectId::fetch](crate::fetch::object_id::ObjectId::fetch) this method doesn't do
|
||||
/// any caching.
|
||||
pub async fn dereference(
|
||||
&self,
|
||||
owner: &<Kind as ApubCollection>::Owner,
|
||||
data: &Data<<Kind as ApubCollection>::DataType>,
|
||||
) -> Result<Kind, <Kind as ApubCollection>::Error>
|
||||
where
|
||||
<Kind as ApubCollection>::Error: From<Error>,
|
||||
{
|
||||
let apub = fetch_object_http(&self.0, data).await?;
|
||||
Kind::verify(&apub, &self.0, data).await?;
|
||||
Kind::from_apub(apub, owner, data).await
|
||||
}
|
||||
}
|
||||
|
||||
/// Need to implement clone manually, to avoid requiring Kind to be Clone
|
||||
impl<Kind> Clone for CollectionId<Kind>
|
||||
where
|
||||
Kind: ApubCollection,
|
||||
for<'de2> <Kind as ApubCollection>::ApubType: serde::Deserialize<'de2>,
|
||||
{
|
||||
fn clone(&self) -> Self {
|
||||
CollectionId(self.0.clone(), self.1)
|
||||
}
|
||||
}
|
||||
|
||||
impl<Kind> Display for CollectionId<Kind>
|
||||
where
|
||||
Kind: ApubCollection,
|
||||
for<'de2> <Kind as ApubCollection>::ApubType: serde::Deserialize<'de2>,
|
||||
{
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "{}", self.0.as_str())
|
||||
}
|
||||
}
|
||||
|
||||
impl<Kind> Debug for CollectionId<Kind>
|
||||
where
|
||||
Kind: ApubCollection,
|
||||
for<'de2> <Kind as ApubCollection>::ApubType: serde::Deserialize<'de2>,
|
||||
{
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "{}", self.0.as_str())
|
||||
}
|
||||
}
|
||||
impl<Kind> From<CollectionId<Kind>> for Url
|
||||
where
|
||||
Kind: ApubCollection,
|
||||
for<'de2> <Kind as ApubCollection>::ApubType: serde::Deserialize<'de2>,
|
||||
{
|
||||
fn from(id: CollectionId<Kind>) -> Self {
|
||||
*id.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<Kind> From<Url> for CollectionId<Kind>
|
||||
where
|
||||
Kind: ApubCollection + Send + 'static,
|
||||
for<'de2> <Kind as ApubCollection>::ApubType: serde::Deserialize<'de2>,
|
||||
{
|
||||
fn from(url: Url) -> Self {
|
||||
CollectionId(Box::new(url), PhantomData::<Kind>)
|
||||
}
|
||||
}
|
|
@ -2,13 +2,15 @@
|
|||
//!
|
||||
#![doc = include_str!("../../docs/07_fetching_data.md")]
|
||||
|
||||
use crate::{config::RequestData, error::Error, reqwest_shim::ResponseExt, APUB_JSON_CONTENT_TYPE};
|
||||
use crate::{config::Data, error::Error, reqwest_shim::ResponseExt, APUB_JSON_CONTENT_TYPE};
|
||||
use http::StatusCode;
|
||||
use serde::de::DeserializeOwned;
|
||||
use std::sync::atomic::Ordering;
|
||||
use tracing::info;
|
||||
use url::Url;
|
||||
|
||||
/// Typed wrapper for collection IDs
|
||||
pub mod collection_id;
|
||||
/// Typed wrapper for Activitypub Object ID which helps with dereferencing and caching
|
||||
pub mod object_id;
|
||||
/// Resolves identifiers of the form `name@example.com`
|
||||
|
@ -24,9 +26,9 @@ pub mod webfinger;
|
|||
/// If the value exceeds [FederationSettings.http_fetch_limit], the request is aborted with
|
||||
/// [Error::RequestLimit]. This prevents denial of service attacks where an attack triggers
|
||||
/// infinite, recursive fetching of data.
|
||||
async fn fetch_object_http<T: Clone, Kind: DeserializeOwned>(
|
||||
pub async fn fetch_object_http<T: Clone, Kind: DeserializeOwned>(
|
||||
url: &Url,
|
||||
data: &RequestData<T>,
|
||||
data: &Data<T>,
|
||||
) -> Result<Kind, Error> {
|
||||
let config = &data.config;
|
||||
// dont fetch local objects this way
|
||||
|
|
|
@ -1,13 +1,25 @@
|
|||
use crate::{config::RequestData, error::Error, fetch::fetch_object_http, traits::ApubObject};
|
||||
use crate::{config::Data, error::Error, fetch::fetch_object_http, traits::ApubObject};
|
||||
use anyhow::anyhow;
|
||||
use chrono::{Duration as ChronoDuration, NaiveDateTime, Utc};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::{
|
||||
fmt::{Debug, Display, Formatter},
|
||||
marker::PhantomData,
|
||||
str::FromStr,
|
||||
};
|
||||
use url::Url;
|
||||
|
||||
impl<T> FromStr for ObjectId<T>
|
||||
where
|
||||
T: ApubObject + Send + 'static,
|
||||
for<'de2> <T as ApubObject>::ApubType: Deserialize<'de2>,
|
||||
{
|
||||
type Err = url::ParseError;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
ObjectId::parse(s)
|
||||
}
|
||||
}
|
||||
/// Typed wrapper for Activitypub Object ID which helps with dereferencing and caching.
|
||||
///
|
||||
/// It provides convenient methods for fetching the object from remote server or local database.
|
||||
|
@ -32,7 +44,7 @@ use url::Url;
|
|||
/// .app_data(db_connection)
|
||||
/// .build()?;
|
||||
/// let request_data = config.to_request_data();
|
||||
/// let object_id = ObjectId::<DbUser>::new("https://lemmy.ml/u/nutomic")?;
|
||||
/// let object_id = ObjectId::<DbUser>::parse("https://lemmy.ml/u/nutomic")?;
|
||||
/// // Attempt to fetch object from local database or fall back to remote server
|
||||
/// let user = object_id.dereference(&request_data).await;
|
||||
/// assert!(user.is_ok());
|
||||
|
@ -55,7 +67,7 @@ where
|
|||
for<'de2> <Kind as ApubObject>::ApubType: serde::Deserialize<'de2>,
|
||||
{
|
||||
/// Construct a new objectid instance
|
||||
pub fn new<T>(url: T) -> Result<Self, url::ParseError>
|
||||
pub fn parse<T>(url: T) -> Result<Self, url::ParseError>
|
||||
where
|
||||
T: TryInto<Url>,
|
||||
url::ParseError: From<<T as TryInto<Url>>::Error>,
|
||||
|
@ -76,7 +88,7 @@ where
|
|||
/// Fetches an activitypub object, either from local database (if possible), or over http.
|
||||
pub async fn dereference(
|
||||
&self,
|
||||
data: &RequestData<<Kind as ApubObject>::DataType>,
|
||||
data: &Data<<Kind as ApubObject>::DataType>,
|
||||
) -> Result<Kind, <Kind as ApubObject>::Error>
|
||||
where
|
||||
<Kind as ApubObject>::Error: From<Error> + From<anyhow::Error>,
|
||||
|
@ -111,7 +123,7 @@ where
|
|||
/// the object is not found in the database.
|
||||
pub async fn dereference_local(
|
||||
&self,
|
||||
data: &RequestData<<Kind as ApubObject>::DataType>,
|
||||
data: &Data<<Kind as ApubObject>::DataType>,
|
||||
) -> Result<Kind, <Kind as ApubObject>::Error>
|
||||
where
|
||||
<Kind as ApubObject>::Error: From<Error>,
|
||||
|
@ -123,7 +135,7 @@ where
|
|||
/// returning none means the object was not found in local db
|
||||
async fn dereference_from_db(
|
||||
&self,
|
||||
data: &RequestData<<Kind as ApubObject>::DataType>,
|
||||
data: &Data<<Kind as ApubObject>::DataType>,
|
||||
) -> Result<Option<Kind>, <Kind as ApubObject>::Error> {
|
||||
let id = self.0.clone();
|
||||
ApubObject::read_from_apub_id(*id, data).await
|
||||
|
@ -131,7 +143,7 @@ where
|
|||
|
||||
async fn dereference_from_http(
|
||||
&self,
|
||||
data: &RequestData<<Kind as ApubObject>::DataType>,
|
||||
data: &Data<<Kind as ApubObject>::DataType>,
|
||||
db_object: Option<Kind>,
|
||||
) -> Result<Kind, <Kind as ApubObject>::Error>
|
||||
where
|
||||
|
@ -238,7 +250,7 @@ pub mod tests {
|
|||
|
||||
#[test]
|
||||
fn test_deserialize() {
|
||||
let id = ObjectId::<DbUser>::new("http://test.com/").unwrap();
|
||||
let id = ObjectId::<DbUser>::parse("http://test.com/").unwrap();
|
||||
|
||||
let string = serde_json::to_string(&id).unwrap();
|
||||
assert_eq!("\"http://test.com/\"", string);
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use crate::{
|
||||
config::RequestData,
|
||||
config::Data,
|
||||
error::{Error, Error::WebfingerResolveFailed},
|
||||
fetch::{fetch_object_http, object_id::ObjectId},
|
||||
traits::{Actor, ApubObject},
|
||||
|
@ -19,7 +19,7 @@ use url::Url;
|
|||
/// is then fetched using [ObjectId::dereference], and the result returned.
|
||||
pub async fn webfinger_resolve_actor<T: Clone, Kind>(
|
||||
identifier: &str,
|
||||
data: &RequestData<T>,
|
||||
data: &Data<T>,
|
||||
) -> Result<Kind, <Kind as ApubObject>::Error>
|
||||
where
|
||||
Kind: ApubObject + Actor + Send + 'static + ApubObject<DataType = T>,
|
||||
|
@ -66,7 +66,7 @@ where
|
|||
/// request. For a parameter of the form `acct:gargron@mastodon.social` it returns `gargron`.
|
||||
///
|
||||
/// Returns an error if query doesn't match local domain.
|
||||
pub fn extract_webfinger_name<T>(query: &str, data: &RequestData<T>) -> Result<String, Error>
|
||||
pub fn extract_webfinger_name<T>(query: &str, data: &Data<T>) -> Result<String, Error>
|
||||
where
|
||||
T: Clone,
|
||||
{
|
||||
|
@ -118,7 +118,7 @@ pub fn build_webfinger_response(subject: String, url: Url) -> Webfinger {
|
|||
}
|
||||
|
||||
/// A webfinger response with information about a `Person` or other type of actor.
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
#[derive(Serialize, Deserialize, Debug, Default)]
|
||||
pub struct Webfinger {
|
||||
/// The actor which is described here, for example `acct:LemmyDev@mastodon.social`
|
||||
pub subject: String,
|
||||
|
@ -133,7 +133,7 @@ pub struct Webfinger {
|
|||
}
|
||||
|
||||
/// A single link included as part of a [Webfinger] response.
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
#[derive(Serialize, Deserialize, Debug, Default)]
|
||||
pub struct WebfingerLink {
|
||||
/// Relationship of the link, such as `self` or `http://webfinger.net/rel/profile-page`
|
||||
pub rel: Option<String>,
|
||||
|
|
|
@ -19,11 +19,7 @@
|
|||
//! Ok::<(), serde_json::error::Error>(())
|
||||
//! ```
|
||||
|
||||
use crate::{
|
||||
config::RequestData,
|
||||
protocol::helpers::deserialize_one_or_many,
|
||||
traits::ActivityHandler,
|
||||
};
|
||||
use crate::{config::Data, protocol::helpers::deserialize_one_or_many, traits::ActivityHandler};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
use url::Url;
|
||||
|
@ -75,11 +71,23 @@ where
|
|||
self.inner.actor()
|
||||
}
|
||||
|
||||
async fn verify(&self, data: &RequestData<Self::DataType>) -> Result<(), Self::Error> {
|
||||
async fn verify(&self, data: &Data<Self::DataType>) -> Result<(), Self::Error> {
|
||||
self.inner.verify(data).await
|
||||
}
|
||||
|
||||
async fn receive(self, data: &RequestData<Self::DataType>) -> Result<(), Self::Error> {
|
||||
async fn receive(self, data: &Data<Self::DataType>) -> Result<(), Self::Error> {
|
||||
self.inner.receive(data).await
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Clone for WithContext<T>
|
||||
where
|
||||
T: Clone,
|
||||
{
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
context: self.context.clone(),
|
||||
inner: self.inner.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,7 +21,7 @@ impl PublicKey {
|
|||
/// Create a new [PublicKey] struct for the `owner` with `public_key_pem`.
|
||||
///
|
||||
/// It uses an standard key id of `{actor_id}#main-key`
|
||||
pub fn new(owner: Url, public_key_pem: String) -> Self {
|
||||
pub(crate) fn new(owner: Url, public_key_pem: String) -> Self {
|
||||
let id = main_key_id(&owner);
|
||||
PublicKey {
|
||||
id,
|
||||
|
|
234
src/traits.rs
234
src/traits.rs
|
@ -1,86 +1,94 @@
|
|||
//! Traits which need to be implemented for federated data types
|
||||
|
||||
use crate::{config::RequestData, protocol::public_key::PublicKey};
|
||||
use crate::{config::Data, protocol::public_key::PublicKey};
|
||||
use async_trait::async_trait;
|
||||
use chrono::NaiveDateTime;
|
||||
use std::ops::Deref;
|
||||
use serde::Deserialize;
|
||||
use std::{fmt::Debug, ops::Deref};
|
||||
use url::Url;
|
||||
|
||||
/// Helper for converting between database structs and federated protocol structs.
|
||||
///
|
||||
/// ```
|
||||
/// # use activitystreams_kinds::{object::NoteType, public};
|
||||
/// # use chrono::{Local, NaiveDateTime};
|
||||
/// # use serde::{Deserialize, Serialize};
|
||||
/// # use url::Url;
|
||||
/// # use activitypub_federation::protocol::public_key::PublicKey;
|
||||
/// # use activitypub_federation::config::RequestData;
|
||||
/// use activitypub_federation::protocol::verification::verify_domains_match;
|
||||
/// # use activitypub_federation::traits::ApubObject;
|
||||
/// # use activitypub_federation::traits::tests::{DbConnection, Person};
|
||||
/// # pub struct DbUser {
|
||||
/// # pub name: String,
|
||||
/// # pub ap_id: Url,
|
||||
/// # pub inbox: Url,
|
||||
/// # pub public_key: String,
|
||||
/// # pub private_key: Option<String>,
|
||||
/// # pub local: bool,
|
||||
/// # pub last_refreshed_at: NaiveDateTime,
|
||||
/// # }
|
||||
///
|
||||
/// #[async_trait::async_trait]
|
||||
/// impl ApubObject for DbUser {
|
||||
/// type DataType = DbConnection;
|
||||
/// type ApubType = Person;
|
||||
/// type Error = anyhow::Error;
|
||||
///
|
||||
/// fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
|
||||
/// Some(self.last_refreshed_at)
|
||||
/// # use activitypub_federation::protocol::{public_key::PublicKey, helpers::deserialize_one_or_many};
|
||||
/// # use activitypub_federation::config::Data;
|
||||
/// # use activitypub_federation::fetch::object_id::ObjectId;
|
||||
/// # use activitypub_federation::protocol::verification::verify_domains_match;
|
||||
/// # use activitypub_federation::traits::{Actor, ApubObject};
|
||||
/// # use activitypub_federation::traits::tests::{DbConnection, DbUser};
|
||||
/// #
|
||||
/// /// How the post is read/written in the local database
|
||||
/// pub struct DbPost {
|
||||
/// pub text: String,
|
||||
/// pub ap_id: ObjectId<DbPost>,
|
||||
/// pub creator: ObjectId<DbUser>,
|
||||
/// pub local: bool,
|
||||
/// }
|
||||
///
|
||||
/// async fn read_from_apub_id(object_id: Url, data: &RequestData<Self::DataType>) -> Result<Option<Self>, Self::Error> {
|
||||
/// /// How the post is serialized and represented as Activitypub JSON
|
||||
/// #[derive(Deserialize, Serialize, Debug)]
|
||||
/// #[serde(rename_all = "camelCase")]
|
||||
/// pub struct Note {
|
||||
/// #[serde(rename = "type")]
|
||||
/// kind: NoteType,
|
||||
/// id: ObjectId<DbPost>,
|
||||
/// pub(crate) attributed_to: ObjectId<DbUser>,
|
||||
/// #[serde(deserialize_with = "deserialize_one_or_many")]
|
||||
/// pub(crate) to: Vec<Url>,
|
||||
/// content: String,
|
||||
/// }
|
||||
///
|
||||
/// #[async_trait::async_trait]
|
||||
/// impl ApubObject for DbPost {
|
||||
/// type DataType = DbConnection;
|
||||
/// type ApubType = Note;
|
||||
/// type Error = anyhow::Error;
|
||||
///
|
||||
/// async fn read_from_apub_id(object_id: Url, data: &Data<Self::DataType>) -> Result<Option<Self>, Self::Error> {
|
||||
/// // Attempt to read object from local database. Return Ok(None) if not found.
|
||||
/// let user: Option<DbUser> = data.read_user_from_apub_id(object_id).await?;
|
||||
/// Ok(user)
|
||||
/// let post: Option<DbPost> = data.read_post_from_apub_id(object_id).await?;
|
||||
/// Ok(post)
|
||||
/// }
|
||||
///
|
||||
/// async fn into_apub(self, data: &RequestData<Self::DataType>) -> Result<Self::ApubType, Self::Error> {
|
||||
/// async fn into_apub(self, data: &Data<Self::DataType>) -> Result<Self::ApubType, Self::Error> {
|
||||
/// // Called when a local object gets sent out over Activitypub. Simply convert it to the
|
||||
/// // protocol struct
|
||||
/// Ok(Person {
|
||||
/// Ok(Note {
|
||||
/// kind: Default::default(),
|
||||
/// preferred_username: self.name,
|
||||
/// id: self.ap_id.clone().into(),
|
||||
/// inbox: self.inbox,
|
||||
/// public_key: PublicKey::new(self.ap_id, self.public_key),
|
||||
/// attributed_to: self.creator,
|
||||
/// to: vec![public()],
|
||||
/// content: self.text,
|
||||
/// })
|
||||
/// }
|
||||
///
|
||||
/// async fn verify(apub: &Self::ApubType, expected_domain: &Url, data: &RequestData<Self::DataType>,) -> Result<(), Self::Error> {
|
||||
/// async fn verify(apub: &Self::ApubType, expected_domain: &Url, data: &Data<Self::DataType>,) -> Result<(), Self::Error> {
|
||||
/// verify_domains_match(apub.id.inner(), expected_domain)?;
|
||||
/// // additional application specific checks
|
||||
/// Ok(())
|
||||
/// }
|
||||
///
|
||||
/// async fn from_apub(apub: Self::ApubType, data: &RequestData<Self::DataType>) -> Result<Self, Self::Error> {
|
||||
/// async fn from_apub(apub: Self::ApubType, data: &Data<Self::DataType>) -> Result<Self, Self::Error> {
|
||||
/// // Called when a remote object gets received over Activitypub. Validate and insert it
|
||||
/// // into the database.
|
||||
///
|
||||
/// let user = DbUser {
|
||||
/// name: apub.preferred_username,
|
||||
/// ap_id: apub.id.into_inner(),
|
||||
/// inbox: apub.inbox,
|
||||
/// public_key: apub.public_key.public_key_pem,
|
||||
/// private_key: None,
|
||||
/// let post = DbPost {
|
||||
/// text: apub.content,
|
||||
/// ap_id: apub.id,
|
||||
/// creator: apub.attributed_to,
|
||||
/// local: false,
|
||||
/// last_refreshed_at: Local::now().naive_local(),
|
||||
/// };
|
||||
///
|
||||
/// // Make sure not to overwrite any local object
|
||||
/// if data.domain() == user.ap_id.domain().unwrap() {
|
||||
/// // Activitypub doesnt distinguish between creating and updating an object. Thats why we
|
||||
/// // need to use upsert functionality here
|
||||
/// data.upsert(&user).await?;
|
||||
/// }
|
||||
/// Ok(user)
|
||||
/// // Here we need to persist the object in the local database. Note that Activitypub
|
||||
/// // doesnt distinguish between creating and updating an object. Thats why we need to
|
||||
/// // use upsert functionality.
|
||||
/// data.upsert(&post).await?;
|
||||
///
|
||||
/// Ok(post)
|
||||
/// }
|
||||
///
|
||||
/// }
|
||||
|
@ -112,13 +120,13 @@ pub trait ApubObject: Sized {
|
|||
/// Should return `Ok(None)` if not found.
|
||||
async fn read_from_apub_id(
|
||||
object_id: Url,
|
||||
data: &RequestData<Self::DataType>,
|
||||
data: &Data<Self::DataType>,
|
||||
) -> Result<Option<Self>, Self::Error>;
|
||||
|
||||
/// Mark remote object as deleted in local database.
|
||||
///
|
||||
/// Called when a `Delete` activity is received, or if fetch returns a `Tombstone` object.
|
||||
async fn delete(self, _data: &RequestData<Self::DataType>) -> Result<(), Self::Error> {
|
||||
async fn delete(self, _data: &Data<Self::DataType>) -> Result<(), Self::Error> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
@ -126,10 +134,7 @@ pub trait ApubObject: Sized {
|
|||
///
|
||||
/// Called when a local object gets fetched by another instance over HTTP, or when an object
|
||||
/// gets sent in an activity.
|
||||
async fn into_apub(
|
||||
self,
|
||||
data: &RequestData<Self::DataType>,
|
||||
) -> Result<Self::ApubType, Self::Error>;
|
||||
async fn into_apub(self, data: &Data<Self::DataType>) -> Result<Self::ApubType, Self::Error>;
|
||||
|
||||
/// Verifies that the received object is valid.
|
||||
///
|
||||
|
@ -141,17 +146,17 @@ pub trait ApubObject: Sized {
|
|||
async fn verify(
|
||||
apub: &Self::ApubType,
|
||||
expected_domain: &Url,
|
||||
data: &RequestData<Self::DataType>,
|
||||
data: &Data<Self::DataType>,
|
||||
) -> Result<(), Self::Error>;
|
||||
|
||||
/// Convert object from ActivityPub type to database type.
|
||||
///
|
||||
/// Called when an object is received from HTTP fetch or as part of an activity. This method
|
||||
/// should do verification and write the received object to database. Note that there is no
|
||||
/// distinction between create and update, so an `upsert` operation should be used.
|
||||
/// should write the received object to database. Note that there is no distinction between
|
||||
/// create and update, so an `upsert` operation should be used.
|
||||
async fn from_apub(
|
||||
apub: Self::ApubType,
|
||||
data: &RequestData<Self::DataType>,
|
||||
data: &Data<Self::DataType>,
|
||||
) -> Result<Self, Self::Error>;
|
||||
}
|
||||
|
||||
|
@ -161,7 +166,7 @@ pub trait ApubObject: Sized {
|
|||
/// # use activitystreams_kinds::activity::FollowType;
|
||||
/// # use url::Url;
|
||||
/// # use activitypub_federation::fetch::object_id::ObjectId;
|
||||
/// # use activitypub_federation::config::RequestData;
|
||||
/// # use activitypub_federation::config::Data;
|
||||
/// # use activitypub_federation::traits::ActivityHandler;
|
||||
/// # use activitypub_federation::traits::tests::{DbConnection, DbUser};
|
||||
/// #[derive(serde::Deserialize)]
|
||||
|
@ -186,11 +191,11 @@ pub trait ApubObject: Sized {
|
|||
/// self.actor.inner()
|
||||
/// }
|
||||
///
|
||||
/// async fn verify(&self, data: &RequestData<Self::DataType>) -> Result<(), Self::Error> {
|
||||
/// async fn verify(&self, data: &Data<Self::DataType>) -> Result<(), Self::Error> {
|
||||
/// Ok(())
|
||||
/// }
|
||||
///
|
||||
/// async fn receive(self, data: &RequestData<Self::DataType>) -> Result<(), Self::Error> {
|
||||
/// async fn receive(self, data: &Data<Self::DataType>) -> Result<(), Self::Error> {
|
||||
/// let local_user = self.object.dereference(data).await?;
|
||||
/// let follower = self.actor.dereference(data).await?;
|
||||
/// data.add_follower(local_user, follower).await?;
|
||||
|
@ -217,19 +222,19 @@ pub trait ActivityHandler {
|
|||
///
|
||||
/// This needs to be a separate method, because it might be used for activities
|
||||
/// like `Undo/Follow`, which shouldn't perform any database write for the inner `Follow`.
|
||||
async fn verify(&self, data: &RequestData<Self::DataType>) -> Result<(), Self::Error>;
|
||||
async fn verify(&self, data: &Data<Self::DataType>) -> Result<(), Self::Error>;
|
||||
|
||||
/// Called when an activity is received.
|
||||
///
|
||||
/// Should perform validation and possibly write action to the database. In case the activity
|
||||
/// has a nested `object` field, must call `object.from_apub` handler.
|
||||
async fn receive(self, data: &RequestData<Self::DataType>) -> Result<(), Self::Error>;
|
||||
async fn receive(self, data: &Data<Self::DataType>) -> Result<(), Self::Error>;
|
||||
}
|
||||
|
||||
/// Trait to allow retrieving common Actor data.
|
||||
pub trait Actor: ApubObject {
|
||||
pub trait Actor: ApubObject + Send + 'static {
|
||||
/// `id` field of the actor
|
||||
fn id(&self) -> &Url;
|
||||
fn id(&self) -> Url;
|
||||
|
||||
/// The actor's public key for verifying signatures of incoming activities.
|
||||
///
|
||||
|
@ -237,12 +242,18 @@ pub trait Actor: ApubObject {
|
|||
/// actor keypair.
|
||||
fn public_key_pem(&self) -> &str;
|
||||
|
||||
/// The actor's private key for signing outgoing activities.
|
||||
///
|
||||
/// Use [generate_actor_keypair](crate::http_signatures::generate_actor_keypair) to create the
|
||||
/// actor keypair.
|
||||
fn private_key_pem(&self) -> Option<String>;
|
||||
|
||||
/// The inbox where activities for this user should be sent to
|
||||
fn inbox(&self) -> Url;
|
||||
|
||||
/// Generates a public key struct for use in the actor json representation
|
||||
fn public_key(&self) -> PublicKey {
|
||||
PublicKey::new(self.id().clone(), self.public_key_pem().to_string())
|
||||
PublicKey::new(self.id(), self.public_key_pem().to_string())
|
||||
}
|
||||
|
||||
/// The actor's shared inbox, if any
|
||||
|
@ -273,15 +284,56 @@ where
|
|||
self.deref().actor()
|
||||
}
|
||||
|
||||
async fn verify(&self, data: &RequestData<Self::DataType>) -> Result<(), Self::Error> {
|
||||
(*self).verify(data).await
|
||||
async fn verify(&self, data: &Data<Self::DataType>) -> Result<(), Self::Error> {
|
||||
self.deref().verify(data).await
|
||||
}
|
||||
|
||||
async fn receive(self, data: &RequestData<Self::DataType>) -> Result<(), Self::Error> {
|
||||
async fn receive(self, data: &Data<Self::DataType>) -> Result<(), Self::Error> {
|
||||
(*self).receive(data).await
|
||||
}
|
||||
}
|
||||
|
||||
/// Trait for federating collections
|
||||
#[async_trait]
|
||||
pub trait ApubCollection: Sized {
|
||||
/// Actor or object that this collection belongs to
|
||||
type Owner;
|
||||
/// App data type passed to handlers. Must be identical to
|
||||
/// [crate::config::FederationConfigBuilder::app_data] type.
|
||||
type DataType: Clone + Send + Sync;
|
||||
/// The type of protocol struct which gets sent over network to federate this database struct.
|
||||
type ApubType: for<'de2> Deserialize<'de2>;
|
||||
/// Error type returned by handler methods
|
||||
type Error;
|
||||
|
||||
/// Reads local collection from database and returns it as Activitypub JSON.
|
||||
async fn read_local(
|
||||
owner: &Self::Owner,
|
||||
data: &Data<Self::DataType>,
|
||||
) -> Result<Self::ApubType, Self::Error>;
|
||||
|
||||
/// Verifies that the received object is valid.
|
||||
///
|
||||
/// You should check here that the domain of id matches `expected_domain`. Additionally you
|
||||
/// should perform any application specific checks.
|
||||
async fn verify(
|
||||
apub: &Self::ApubType,
|
||||
expected_domain: &Url,
|
||||
data: &Data<Self::DataType>,
|
||||
) -> Result<(), Self::Error>;
|
||||
|
||||
/// Convert object from ActivityPub type to database type.
|
||||
///
|
||||
/// Called when an object is received from HTTP fetch or as part of an activity. This method
|
||||
/// should also write the received object to database. Note that there is no distinction
|
||||
/// between create and update, so an `upsert` operation should be used.
|
||||
async fn from_apub(
|
||||
apub: Self::ApubType,
|
||||
owner: &Self::Owner,
|
||||
data: &Data<Self::DataType>,
|
||||
) -> Result<Self, Self::Error>;
|
||||
}
|
||||
|
||||
/// Some impls of these traits for use in tests. Dont use this from external crates.
|
||||
///
|
||||
/// TODO: Should be using `cfg[doctest]` but blocked by <https://github.com/rust-lang/rust/issues/67295>
|
||||
|
@ -303,7 +355,7 @@ pub mod tests {
|
|||
pub struct DbConnection;
|
||||
|
||||
impl DbConnection {
|
||||
pub async fn read_user_from_apub_id<T>(&self, _: Url) -> Result<Option<T>, Error> {
|
||||
pub async fn read_post_from_apub_id<T>(&self, _: Url) -> Result<Option<T>, Error> {
|
||||
Ok(None)
|
||||
}
|
||||
pub async fn read_local_user(&self, _: String) -> Result<DbUser, Error> {
|
||||
|
@ -346,7 +398,7 @@ pub mod tests {
|
|||
apub_id: "https://localhost/123".parse().unwrap(),
|
||||
inbox: "https://localhost/123/inbox".parse().unwrap(),
|
||||
public_key: DB_USER_KEYPAIR.public_key.clone(),
|
||||
private_key: None,
|
||||
private_key: Some(DB_USER_KEYPAIR.private_key.clone()),
|
||||
followers: vec![],
|
||||
local: false,
|
||||
});
|
||||
|
@ -359,29 +411,28 @@ pub mod tests {
|
|||
|
||||
async fn read_from_apub_id(
|
||||
_object_id: Url,
|
||||
_data: &RequestData<Self::DataType>,
|
||||
_data: &Data<Self::DataType>,
|
||||
) -> Result<Option<Self>, Self::Error> {
|
||||
Ok(Some(DB_USER.clone()))
|
||||
}
|
||||
|
||||
async fn into_apub(
|
||||
self,
|
||||
_data: &RequestData<Self::DataType>,
|
||||
_data: &Data<Self::DataType>,
|
||||
) -> Result<Self::ApubType, Self::Error> {
|
||||
let public_key = PublicKey::new(self.apub_id.clone(), self.public_key.clone());
|
||||
Ok(Person {
|
||||
preferred_username: self.name.clone(),
|
||||
kind: Default::default(),
|
||||
id: self.apub_id.into(),
|
||||
inbox: self.inbox,
|
||||
public_key,
|
||||
id: self.apub_id.clone().into(),
|
||||
inbox: self.inbox.clone(),
|
||||
public_key: self.public_key(),
|
||||
})
|
||||
}
|
||||
|
||||
async fn verify(
|
||||
apub: &Self::ApubType,
|
||||
expected_domain: &Url,
|
||||
_data: &RequestData<Self::DataType>,
|
||||
_data: &Data<Self::DataType>,
|
||||
) -> Result<(), Self::Error> {
|
||||
verify_domains_match(apub.id.inner(), expected_domain)?;
|
||||
Ok(())
|
||||
|
@ -389,7 +440,7 @@ pub mod tests {
|
|||
|
||||
async fn from_apub(
|
||||
apub: Self::ApubType,
|
||||
_data: &RequestData<Self::DataType>,
|
||||
_data: &Data<Self::DataType>,
|
||||
) -> Result<Self, Self::Error> {
|
||||
Ok(DbUser {
|
||||
name: apub.preferred_username,
|
||||
|
@ -404,14 +455,18 @@ pub mod tests {
|
|||
}
|
||||
|
||||
impl Actor for DbUser {
|
||||
fn id(&self) -> &Url {
|
||||
&self.apub_id
|
||||
fn id(&self) -> Url {
|
||||
self.apub_id.clone()
|
||||
}
|
||||
|
||||
fn public_key_pem(&self) -> &str {
|
||||
&self.public_key
|
||||
}
|
||||
|
||||
fn private_key_pem(&self) -> Option<String> {
|
||||
self.private_key.clone()
|
||||
}
|
||||
|
||||
fn inbox(&self) -> Url {
|
||||
self.inbox.clone()
|
||||
}
|
||||
|
@ -440,11 +495,11 @@ pub mod tests {
|
|||
self.actor.inner()
|
||||
}
|
||||
|
||||
async fn verify(&self, _: &RequestData<Self::DataType>) -> Result<(), Self::Error> {
|
||||
async fn verify(&self, _: &Data<Self::DataType>) -> Result<(), Self::Error> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn receive(self, _data: &RequestData<Self::DataType>) -> Result<(), Self::Error> {
|
||||
async fn receive(self, _data: &Data<Self::DataType>) -> Result<(), Self::Error> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
@ -463,29 +518,26 @@ pub mod tests {
|
|||
|
||||
async fn read_from_apub_id(
|
||||
_: Url,
|
||||
_: &RequestData<Self::DataType>,
|
||||
_: &Data<Self::DataType>,
|
||||
) -> Result<Option<Self>, Self::Error> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
async fn into_apub(
|
||||
self,
|
||||
_: &RequestData<Self::DataType>,
|
||||
) -> Result<Self::ApubType, Self::Error> {
|
||||
async fn into_apub(self, _: &Data<Self::DataType>) -> Result<Self::ApubType, Self::Error> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
async fn verify(
|
||||
_: &Self::ApubType,
|
||||
_: &Url,
|
||||
_: &RequestData<Self::DataType>,
|
||||
_: &Data<Self::DataType>,
|
||||
) -> Result<(), Self::Error> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
async fn from_apub(
|
||||
_: Self::ApubType,
|
||||
_: &RequestData<Self::DataType>,
|
||||
_: &Data<Self::DataType>,
|
||||
) -> Result<Self, Self::Error> {
|
||||
todo!()
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue