}
/// Send an activity to a list of recipients, using the correct headers etc.
-pub fn send_activity<A>(
- activity: &A,
- private_key: &str,
- sender_id: &str,
- to: Vec<String>,
-) -> Result<(), Error>
+pub fn send_activity<A>(activity: &A, actor: &dyn ActorType, to: Vec<String>) -> Result<(), Error>
where
A: Serialize + Debug,
{
continue;
}
let request = Request::post(t).header("Host", to_url.domain().unwrap());
- let signature = sign(&request, private_key, sender_id)?;
+ let signature = sign(&request, actor)?;
let res = request
.header("Signature", signature)
.header("Content-Type", "application/json")
insert_activity(&conn, creator.id, &create, true)?;
- send_activity(
- &create,
- &creator.private_key.as_ref().unwrap(),
- &creator.actor_id,
- community.get_follower_inboxes(&conn)?,
- )?;
+ send_activity(&create, creator, community.get_follower_inboxes(&conn)?)?;
Ok(())
}
insert_activity(&conn, creator.id, &update, true)?;
- send_activity(
- &update,
- &creator.private_key.as_ref().unwrap(),
- &creator.actor_id,
- community.get_follower_inboxes(&conn)?,
- )?;
+ send_activity(&update, creator, community.get_follower_inboxes(&conn)?)?;
Ok(())
}
insert_activity(&conn, creator.id, &delete, true)?;
- send_activity(
- &delete,
- &creator.private_key.as_ref().unwrap(),
- &creator.actor_id,
- community.get_follower_inboxes(&conn)?,
- )?;
+ send_activity(&delete, creator, community.get_follower_inboxes(&conn)?)?;
Ok(())
}
insert_activity(&conn, creator.id, &undo, true)?;
- send_activity(
- &undo,
- &creator.private_key.as_ref().unwrap(),
- &creator.actor_id,
- community.get_follower_inboxes(&conn)?,
- )?;
+ send_activity(&undo, creator, community.get_follower_inboxes(&conn)?)?;
Ok(())
}
insert_activity(&conn, mod_.id, &remove, true)?;
- send_activity(
- &remove,
- &mod_.private_key.as_ref().unwrap(),
- &mod_.actor_id,
- community.get_follower_inboxes(&conn)?,
- )?;
+ send_activity(&remove, mod_, community.get_follower_inboxes(&conn)?)?;
Ok(())
}
insert_activity(&conn, mod_.id, &undo, true)?;
- send_activity(
- &undo,
- &mod_.private_key.as_ref().unwrap(),
- &mod_.actor_id,
- community.get_follower_inboxes(&conn)?,
- )?;
+ send_activity(&undo, mod_, community.get_follower_inboxes(&conn)?)?;
Ok(())
}
}
insert_activity(&conn, creator.id, &like, true)?;
- send_activity(
- &like,
- &creator.private_key.as_ref().unwrap(),
- &creator.actor_id,
- community.get_follower_inboxes(&conn)?,
- )?;
+ send_activity(&like, creator, community.get_follower_inboxes(&conn)?)?;
Ok(())
}
insert_activity(&conn, creator.id, &dislike, true)?;
- send_activity(
- &dislike,
- &creator.private_key.as_ref().unwrap(),
- &creator.actor_id,
- community.get_follower_inboxes(&conn)?,
- )?;
+ send_activity(&dislike, creator, community.get_follower_inboxes(&conn)?)?;
Ok(())
}
insert_activity(&conn, creator.id, &undo, true)?;
- send_activity(
- &undo,
- &creator.private_key.as_ref().unwrap(),
- &creator.actor_id,
- community.get_follower_inboxes(&conn)?,
- )?;
+ send_activity(&undo, creator, community.get_follower_inboxes(&conn)?)?;
Ok(())
}
}
fn public_key(&self) -> String {
self.public_key.to_owned().unwrap()
}
+ fn private_key(&self) -> String {
+ self.private_key.to_owned().unwrap()
+ }
/// As a local community, accept the follow request from a remote user.
fn send_accept_follow(&self, follow: &Follow, conn: &PgConnection) -> Result<(), Error> {
insert_activity(&conn, self.creator_id, &accept, true)?;
- send_activity(
- &accept,
- &self.private_key.to_owned().unwrap(),
- &self.actor_id,
- vec![to],
- )?;
+ send_activity(&accept, self, vec![to])?;
Ok(())
}
// Note: For an accept, since it was automatic, no one pushed a button,
// the community was the actor.
// But for delete, the creator is the actor, and does the signing
- send_activity(
- &delete,
- &creator.private_key.as_ref().unwrap(),
- &creator.actor_id,
- self.get_follower_inboxes(&conn)?,
- )?;
+ send_activity(&delete, creator, self.get_follower_inboxes(&conn)?)?;
Ok(())
}
// Note: For an accept, since it was automatic, no one pushed a button,
// the community was the actor.
// But for delete, the creator is the actor, and does the signing
- send_activity(
- &undo,
- &creator.private_key.as_ref().unwrap(),
- &creator.actor_id,
- self.get_follower_inboxes(&conn)?,
- )?;
+ send_activity(&undo, creator, self.get_follower_inboxes(&conn)?)?;
Ok(())
}
// Note: For an accept, since it was automatic, no one pushed a button,
// the community was the actor.
// But for delete, the creator is the actor, and does the signing
- send_activity(
- &remove,
- &mod_.private_key.as_ref().unwrap(),
- &mod_.actor_id,
- self.get_follower_inboxes(&conn)?,
- )?;
+ send_activity(&remove, mod_, self.get_follower_inboxes(&conn)?)?;
Ok(())
}
// Note: For an accept, since it was automatic, no one pushed a button,
// the community was the actor.
// But for remove , the creator is the actor, and does the signing
- send_activity(
- &undo,
- &mod_.private_key.as_ref().unwrap(),
- &mod_.actor_id,
- self.get_follower_inboxes(&conn)?,
- )?;
+ send_activity(&undo, mod_, self.get_follower_inboxes(&conn)?)?;
Ok(())
}
let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?;
let community = Community::read_from_name(&conn, &community_name)?;
- verify(&request, &user.public_key.unwrap())?;
+ verify(&request, &user)?;
insert_activity(&conn, user.id, &follow, false)?;
let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?;
let community = Community::read_from_name(&conn, &community_name)?;
- verify(&request, &user.public_key.unwrap())?;
+ verify(&request, &user)?;
insert_activity(&conn, user.id, &follow, false)?;
+use crate::apub::ActorType;
use activitystreams::ext::Extension;
-use activitystreams::Actor;
use actix_web::HttpRequest;
use failure::Error;
use http::request::Builder;
}
/// Signs request headers with the given keypair.
-/// TODO: would be nice to pass the sending actor in, instead of raw privatekey/id strings
-pub fn sign(request: &Builder, private_key: &str, sender_id: &str) -> Result<String, Error> {
- let signing_key_id = format!("{}#main-key", sender_id);
+pub fn sign(request: &Builder, actor: &dyn ActorType) -> Result<String, Error> {
+ let signing_key_id = format!("{}#main-key", actor.actor_id());
let headers = request
.headers_ref()
headers,
)
.sign(signing_key_id, |signing_string| {
- let private_key = PKey::private_key_from_pem(private_key.as_bytes())?;
+ let private_key = PKey::private_key_from_pem(actor.private_key().as_bytes())?;
let mut signer = Signer::new(MessageDigest::sha256(), &private_key).unwrap();
signer.update(signing_string.as_bytes()).unwrap();
Ok(base64::encode(signer.sign_to_vec()?)) as Result<_, Error>
Ok(signature_header_value)
}
-pub fn verify(request: &HttpRequest, public_key: &str) -> Result<(), Error> {
+pub fn verify(request: &HttpRequest, actor: &dyn ActorType) -> Result<(), Error> {
let headers = request
.headers()
.iter()
.verify(|signature, signing_string| -> Result<bool, Error> {
debug!(
"Verifying with key {}, message {}",
- &public_key, &signing_string
+ &actor.public_key(),
+ &signing_string
);
- let public_key = PKey::public_key_from_pem(public_key.as_bytes())?;
+ let public_key = PKey::public_key_from_pem(actor.public_key().as_bytes())?;
let mut verifier = Verifier::new(MessageDigest::sha256(), &public_key).unwrap();
verifier.update(&signing_string.as_bytes()).unwrap();
Ok(verifier.verify(&base64::decode(signature)?)?)
}
}
-impl<T> Extension<T> for PublicKeyExtension where T: Actor {}
+impl<T> Extension<T> for PublicKeyExtension where T: activitystreams::Actor {}
fn actor_id(&self) -> String;
fn public_key(&self) -> String;
+ fn private_key(&self) -> String;
// These two have default impls, since currently a community can't follow anything,
// and a user can't be followed (yet)
insert_activity(&conn, creator.id, &create, true)?;
- send_activity(
- &create,
- &creator.private_key.as_ref().unwrap(),
- &creator.actor_id,
- community.get_follower_inboxes(&conn)?,
- )?;
+ send_activity(&create, creator, community.get_follower_inboxes(&conn)?)?;
Ok(())
}
insert_activity(&conn, creator.id, &update, true)?;
- send_activity(
- &update,
- &creator.private_key.as_ref().unwrap(),
- &creator.actor_id,
- community.get_follower_inboxes(&conn)?,
- )?;
+ send_activity(&update, creator, community.get_follower_inboxes(&conn)?)?;
Ok(())
}
insert_activity(&conn, self.creator_id, &delete, true)?;
let community = Community::read(conn, self.community_id)?;
- send_activity(
- &delete,
- &creator.private_key.as_ref().unwrap(),
- &creator.actor_id,
- community.get_follower_inboxes(&conn)?,
- )?;
+ send_activity(&delete, creator, community.get_follower_inboxes(&conn)?)?;
Ok(())
}
insert_activity(&conn, self.creator_id, &undo, true)?;
let community = Community::read(conn, self.community_id)?;
- send_activity(
- &undo,
- &creator.private_key.as_ref().unwrap(),
- &creator.actor_id,
- community.get_follower_inboxes(&conn)?,
- )?;
+ send_activity(&undo, creator, community.get_follower_inboxes(&conn)?)?;
Ok(())
}
insert_activity(&conn, mod_.id, &remove, true)?;
let community = Community::read(conn, self.community_id)?;
- send_activity(
- &remove,
- &mod_.private_key.as_ref().unwrap(),
- &mod_.actor_id,
- community.get_follower_inboxes(&conn)?,
- )?;
+ send_activity(&remove, mod_, community.get_follower_inboxes(&conn)?)?;
Ok(())
}
fn send_undo_remove(&self, mod_: &User_, conn: &PgConnection) -> Result<(), Error> {
insert_activity(&conn, mod_.id, &undo, true)?;
let community = Community::read(conn, self.community_id)?;
- send_activity(
- &undo,
- &mod_.private_key.as_ref().unwrap(),
- &mod_.actor_id,
- community.get_follower_inboxes(&conn)?,
- )?;
+ send_activity(&undo, mod_, community.get_follower_inboxes(&conn)?)?;
Ok(())
}
}
insert_activity(&conn, creator.id, &like, true)?;
- send_activity(
- &like,
- &creator.private_key.as_ref().unwrap(),
- &creator.actor_id,
- community.get_follower_inboxes(&conn)?,
- )?;
+ send_activity(&like, creator, community.get_follower_inboxes(&conn)?)?;
Ok(())
}
insert_activity(&conn, creator.id, &dislike, true)?;
- send_activity(
- &dislike,
- &creator.private_key.as_ref().unwrap(),
- &creator.actor_id,
- community.get_follower_inboxes(&conn)?,
- )?;
+ send_activity(&dislike, creator, community.get_follower_inboxes(&conn)?)?;
Ok(())
}
insert_activity(&conn, creator.id, &undo, true)?;
- send_activity(
- &undo,
- &creator.private_key.as_ref().unwrap(),
- &creator.actor_id,
- community.get_follower_inboxes(&conn)?,
- )?;
+ send_activity(&undo, creator, community.get_follower_inboxes(&conn)?)?;
Ok(())
}
}
insert_activity(&conn, creator.id, &create, true)?;
- send_activity(
- &create,
- &creator.private_key.as_ref().unwrap(),
- &creator.actor_id,
- vec![to],
- )?;
+ send_activity(&create, creator, vec![to])?;
Ok(())
}
insert_activity(&conn, creator.id, &update, true)?;
- send_activity(
- &update,
- &creator.private_key.as_ref().unwrap(),
- &creator.actor_id,
- vec![to],
- )?;
+ send_activity(&update, creator, vec![to])?;
Ok(())
}
insert_activity(&conn, creator.id, &delete, true)?;
- send_activity(
- &delete,
- &creator.private_key.as_ref().unwrap(),
- &creator.actor_id,
- vec![to],
- )?;
+ send_activity(&delete, creator, vec![to])?;
Ok(())
}
insert_activity(&conn, creator.id, &undo, true)?;
- send_activity(
- &undo,
- &creator.private_key.as_ref().unwrap(),
- &creator.actor_id,
- vec![to],
- )?;
+ send_activity(&undo, creator, vec![to])?;
Ok(())
}
.to_string();
let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?;
- verify(request, &user.public_key.unwrap())?;
+ verify(request, &user)?;
insert_activity(&conn, user.id, &create, false)?;
.to_string();
let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?;
- verify(request, &user.public_key.unwrap())?;
+ verify(request, &user)?;
insert_activity(&conn, user.id, &create, false)?;
.to_string();
let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?;
- verify(request, &user.public_key.unwrap())?;
+ verify(request, &user)?;
insert_activity(&conn, user.id, &update, false)?;
let user_uri = like.like_props.get_actor_xsd_any_uri().unwrap().to_string();
let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?;
- verify(request, &user.public_key.unwrap())?;
+ verify(request, &user)?;
insert_activity(&conn, user.id, &like, false)?;
.to_string();
let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?;
- verify(request, &user.public_key.unwrap())?;
+ verify(request, &user)?;
insert_activity(&conn, user.id, &dislike, false)?;
.to_string();
let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?;
- verify(request, &user.public_key.unwrap())?;
+ verify(request, &user)?;
insert_activity(&conn, user.id, &update, false)?;
let user_uri = like.like_props.get_actor_xsd_any_uri().unwrap().to_string();
let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?;
- verify(request, &user.public_key.unwrap())?;
+ verify(request, &user)?;
insert_activity(&conn, user.id, &like, false)?;
.to_string();
let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?;
- verify(request, &user.public_key.unwrap())?;
+ verify(request, &user)?;
insert_activity(&conn, user.id, &dislike, false)?;
.into_concrete::<GroupExt>()?;
let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?;
- verify(request, &user.public_key.unwrap())?;
+ verify(request, &user)?;
insert_activity(&conn, user.id, &delete, false)?;
.into_concrete::<GroupExt>()?;
let mod_ = get_or_fetch_and_upsert_remote_user(&mod_uri, &conn)?;
- verify(request, &mod_.public_key.unwrap())?;
+ verify(request, &mod_)?;
insert_activity(&conn, mod_.id, &remove, false)?;
.into_concrete::<PageExt>()?;
let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?;
- verify(request, &user.public_key.unwrap())?;
+ verify(request, &user)?;
insert_activity(&conn, user.id, &delete, false)?;
.into_concrete::<PageExt>()?;
let mod_ = get_or_fetch_and_upsert_remote_user(&mod_uri, &conn)?;
- verify(request, &mod_.public_key.unwrap())?;
+ verify(request, &mod_)?;
insert_activity(&conn, mod_.id, &remove, false)?;
.into_concrete::<Note>()?;
let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?;
- verify(request, &user.public_key.unwrap())?;
+ verify(request, &user)?;
insert_activity(&conn, user.id, &delete, false)?;
.into_concrete::<Note>()?;
let mod_ = get_or_fetch_and_upsert_remote_user(&mod_uri, &conn)?;
- verify(request, &mod_.public_key.unwrap())?;
+ verify(request, &mod_)?;
insert_activity(&conn, mod_.id, &remove, false)?;
.into_concrete::<Note>()?;
let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?;
- verify(request, &user.public_key.unwrap())?;
+ verify(request, &user)?;
insert_activity(&conn, user.id, &delete, false)?;
.into_concrete::<Note>()?;
let mod_ = get_or_fetch_and_upsert_remote_user(&mod_uri, &conn)?;
- verify(request, &mod_.public_key.unwrap())?;
+ verify(request, &mod_)?;
insert_activity(&conn, mod_.id, &remove, false)?;
.into_concrete::<PageExt>()?;
let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?;
- verify(request, &user.public_key.unwrap())?;
+ verify(request, &user)?;
insert_activity(&conn, user.id, &delete, false)?;
.into_concrete::<PageExt>()?;
let mod_ = get_or_fetch_and_upsert_remote_user(&mod_uri, &conn)?;
- verify(request, &mod_.public_key.unwrap())?;
+ verify(request, &mod_)?;
insert_activity(&conn, mod_.id, &remove, false)?;
.into_concrete::<GroupExt>()?;
let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?;
- verify(request, &user.public_key.unwrap())?;
+ verify(request, &user)?;
insert_activity(&conn, user.id, &delete, false)?;
.into_concrete::<GroupExt>()?;
let mod_ = get_or_fetch_and_upsert_remote_user(&mod_uri, &conn)?;
- verify(request, &mod_.public_key.unwrap())?;
+ verify(request, &mod_)?;
insert_activity(&conn, mod_.id, &remove, false)?;
let user_uri = like.like_props.get_actor_xsd_any_uri().unwrap().to_string();
let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?;
- verify(request, &user.public_key.unwrap())?;
+ verify(request, &user)?;
insert_activity(&conn, user.id, &like, false)?;
let user_uri = like.like_props.get_actor_xsd_any_uri().unwrap().to_string();
let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?;
- verify(request, &user.public_key.unwrap())?;
+ verify(request, &user)?;
insert_activity(&conn, user.id, &like, false)?;
self.public_key.to_owned().unwrap()
}
+ fn private_key(&self) -> String {
+ self.private_key.to_owned().unwrap()
+ }
+
/// As a given local user, send out a follow request to a remote community.
fn send_follow(&self, follow_actor_id: &str, conn: &PgConnection) -> Result<(), Error> {
let mut follow = Follow::new();
insert_activity(&conn, self.id, &follow, true)?;
- send_activity(
- &follow,
- &self.private_key.as_ref().unwrap(),
- &follow_actor_id,
- vec![to],
- )?;
+ send_activity(&follow, self, vec![to])?;
Ok(())
}
insert_activity(&conn, self.id, &undo, true)?;
- send_activity(
- &undo,
- &self.private_key.as_ref().unwrap(),
- &follow_actor_id,
- vec![to],
- )?;
+ send_activity(&undo, self, vec![to])?;
Ok(())
}
.to_string();
let community = get_or_fetch_and_upsert_remote_community(&community_uri, conn)?;
- verify(request, &community.public_key.unwrap())?;
+ verify(request, &community)?;
let user = User_::read_from_name(&conn, username)?;
.to_string();
let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?;
- verify(request, &user.public_key.unwrap())?;
+ verify(request, &user)?;
insert_activity(&conn, user.id, &create, false)?;
.to_string();
let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?;
- verify(request, &user.public_key.unwrap())?;
+ verify(request, &user)?;
insert_activity(&conn, user.id, &update, false)?;
.to_string();
let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?;
- verify(request, &user.public_key.unwrap())?;
+ verify(request, &user)?;
insert_activity(&conn, user.id, &delete, false)?;
.to_string();
let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?;
- verify(request, &user.public_key.unwrap())?;
+ verify(request, &user)?;
insert_activity(&conn, user.id, &delete, false)?;