From 6260f4306ec88115453cdb70fda8ca84a53ebac7 Mon Sep 17 00:00:00 2001 From: Astro Date: Mon, 12 Dec 2022 21:31:42 +0100 Subject: [PATCH] fixes --- src/activitypub.rs | 4 +++- src/main.rs | 51 ++++++++++++++++++++++++++++------------------ 2 files changed, 34 insertions(+), 21 deletions(-) diff --git a/src/activitypub.rs b/src/activitypub.rs index 15a7a97..0c0bd18 100644 --- a/src/activitypub.rs +++ b/src/activitypub.rs @@ -8,9 +8,11 @@ pub struct Actor { pub actor_type: String, pub id: String, pub inbox: String, - pub outbox: String, + // pub outbox: String, #[serde(rename = "publicKey")] pub public_key: ActorPublicKey, + #[serde(rename = "preferredUsername")] + pub preferredUsername: Option, } #[derive(Debug, Clone, Serialize, Deserialize)] diff --git a/src/main.rs b/src/main.rs index 03f9259..91561cb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,6 +7,7 @@ use axum::{ Form, Json, RequestExt, Router, }; use serde::{Deserialize, Serialize}; +use serde_json::json; use sigh::{PrivateKey, PublicKey, alg::{RsaSha256, Algorithm}, Key}; use std::{net::SocketAddr, sync::Arc}; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; @@ -18,6 +19,9 @@ pub use send::send; mod activitypub; mod endpoint; +const ACTOR_ID: &str = "https://relay.fedi.buzz/actor"; +const ACTOR_KEY: &str = "https://relay.fedi.buzz/actor#key"; + #[derive(Debug, Clone)] struct State { client: Arc, @@ -32,22 +36,25 @@ impl FromRef for Arc { } } -async fn actor(axum::extract::State(state): axum::extract::State) -> impl IntoResponse { - let id = "https://relay.fedi.buzz/".to_string(); - Json(activitypub::Actor { - jsonld_context: serde_json::Value::String( - "https://www.w3.org/ns/activitystreams".to_string() - ), - actor_type: "Application".to_string(), - id: id.clone(), - inbox: id.clone(), - outbox: id.clone(), - public_key: activitypub::ActorPublicKey { - id: id.clone(), - owner: Some(id), - pem: state.public_key.to_pem().unwrap(), - }, - }) +async fn actor(axum::extract::State(state): axum::extract::State) -> Response { + let id = ACTOR_ID.to_string(); + ([("content-type", "application/activity+json")], + Json(activitypub::Actor { + jsonld_context: json!([ + "https://www.w3.org/ns/activitystreams", + "https://w3id.org/security/v1", + ]), + actor_type: "Service".to_string(), + id: id.clone(), + inbox: "https://relay.fedi.buzz/inbox".to_string(), + // outbox: "https://relay.fedi.buzz/outbox".to_string(), + public_key: activitypub::ActorPublicKey { + id: ACTOR_KEY.to_string(), + owner: Some(id.clone()), + pem: state.public_key.to_pem().unwrap(), + }, + preferredUsername: Some("buzzrelay".to_string()), + })).into_response() } async fn handler( @@ -69,21 +76,24 @@ async fn handler( tokio::spawn(async move { let accept = activitypub::Action { action_type: "Accept".to_string(), - actor: "https://relay.fedi.buzz/".to_string(), + actor: ACTOR_ID.to_string(), to: Some(endpoint.actor.id), object: Some(endpoint.payload), }; dbg!(serde_json::to_string_pretty(&accept)); send::send( client.as_ref(), &endpoint.actor.inbox, - "https://relay.fedi.buzz/", + ACTOR_KEY, &private_key, accept, ).await .map_err(|e| tracing::error!("post: {}", e)); }); - StatusCode::OK.into_response() + (StatusCode::CREATED, + [("content-type", "application/activity+json")], + "{}" + ).into_response() } else { (StatusCode::BAD_REQUEST, "Not a recognized request").into_response() } @@ -103,7 +113,8 @@ async fn main() { let (private_key, public_key) = RsaSha256.generate_keys().unwrap(); let app = Router::new() - .route("/", get(actor).post(handler)) + .route("/actor", get(actor)) + .route("/relay", post(handler)) .with_state(State { client: Arc::new(reqwest::Client::new()), private_key, public_key,