Use profile importer in verify_http_signature function
This commit is contained in:
parent
f7211d2a14
commit
38ed905f47
7 changed files with 31 additions and 38 deletions
|
@ -1,6 +1,7 @@
|
|||
# https://github.com/rust-lang/cargo/issues/5034#issuecomment-927105016
|
||||
[target.'cfg(feature = "cargo-clippy")']
|
||||
rustflags = [
|
||||
"-Aclippy::len_zero",
|
||||
"-Aclippy::let_and_return",
|
||||
"-Aclippy::map_entry",
|
||||
"-Aclippy::or_fun_call",
|
||||
|
|
|
@ -41,12 +41,12 @@ impl From<ImportError> for HttpError {
|
|||
}
|
||||
}
|
||||
|
||||
pub async fn get_or_fetch_profile_by_actor_id(
|
||||
pub async fn get_or_import_profile_by_actor_id(
|
||||
db_client: &impl GenericClient,
|
||||
instance: &Instance,
|
||||
actor_id: &str,
|
||||
media_dir: &Path,
|
||||
) -> Result<DbActorProfile, HttpError> {
|
||||
actor_id: &str,
|
||||
) -> Result<DbActorProfile, ImportError> {
|
||||
let profile = match get_profile_by_actor_id(db_client, actor_id).await {
|
||||
Ok(profile) => profile,
|
||||
Err(DatabaseError::NotFound(_)) => {
|
||||
|
@ -56,7 +56,7 @@ pub async fn get_or_fetch_profile_by_actor_id(
|
|||
.await
|
||||
.map_err(|err| {
|
||||
log::warn!("{}", err);
|
||||
ValidationError("failed to fetch actor")
|
||||
err
|
||||
})?;
|
||||
log::info!("fetched profile {}", profile_data.acct);
|
||||
let profile = create_profile(db_client, &profile_data).await?;
|
||||
|
|
|
@ -45,7 +45,7 @@ use super::fetcher::fetchers::{
|
|||
fetch_object,
|
||||
};
|
||||
use super::fetcher::helpers::{
|
||||
get_or_fetch_profile_by_actor_id,
|
||||
get_or_import_profile_by_actor_id,
|
||||
import_profile_by_actor_address,
|
||||
ImportError,
|
||||
};
|
||||
|
@ -211,11 +211,11 @@ pub async fn process_note(
|
|||
.get(0)
|
||||
.ok_or(ValidationError("invalid attributedTo property"))?
|
||||
.to_string();
|
||||
let author = get_or_fetch_profile_by_actor_id(
|
||||
let author = get_or_import_profile_by_actor_id(
|
||||
db_client,
|
||||
&instance,
|
||||
&author_id,
|
||||
&config.media_dir(),
|
||||
&author_id,
|
||||
).await?;
|
||||
let content = object.content
|
||||
.ok_or(ValidationError("no content"))?;
|
||||
|
@ -378,11 +378,11 @@ pub async fn receive_activity(
|
|||
Err(DatabaseError::NotFound(_)) => (),
|
||||
Err(other_error) => return Err(other_error.into()),
|
||||
};
|
||||
let author = get_or_fetch_profile_by_actor_id(
|
||||
let author = get_or_import_profile_by_actor_id(
|
||||
db_client,
|
||||
&config.instance(),
|
||||
&activity.actor,
|
||||
&config.media_dir(),
|
||||
&activity.actor,
|
||||
).await?;
|
||||
let object_id = get_object_id(activity.object)?;
|
||||
let post_id = match parse_object_id(&config.instance_url(), &object_id) {
|
||||
|
@ -417,11 +417,11 @@ pub async fn receive_activity(
|
|||
NOTE
|
||||
},
|
||||
(LIKE, _) | (EMOJI_REACT, _) => {
|
||||
let author = get_or_fetch_profile_by_actor_id(
|
||||
let author = get_or_import_profile_by_actor_id(
|
||||
db_client,
|
||||
&config.instance(),
|
||||
&activity.actor,
|
||||
&config.media_dir(),
|
||||
&activity.actor,
|
||||
).await?;
|
||||
let object_id = get_object_id(activity.object)?;
|
||||
let post_id = match parse_object_id(&config.instance_url(), &object_id) {
|
||||
|
@ -450,11 +450,11 @@ pub async fn receive_activity(
|
|||
NOTE
|
||||
},
|
||||
(FOLLOW, _) => {
|
||||
let source_profile = get_or_fetch_profile_by_actor_id(
|
||||
let source_profile = get_or_import_profile_by_actor_id(
|
||||
db_client,
|
||||
&config.instance(),
|
||||
&activity.actor,
|
||||
&config.media_dir(),
|
||||
&activity.actor,
|
||||
).await?;
|
||||
let source_actor = source_profile.remote_actor().ok().flatten()
|
||||
.ok_or(HttpError::InternalError)?;
|
||||
|
|
|
@ -6,14 +6,13 @@ use actix_web::{
|
|||
};
|
||||
use regex::Regex;
|
||||
|
||||
use crate::activitypub::fetcher::fetchers::fetch_profile_by_actor_id;
|
||||
use crate::activitypub::fetcher::helpers::{
|
||||
get_or_import_profile_by_actor_id,
|
||||
ImportError,
|
||||
};
|
||||
use crate::config::Config;
|
||||
use crate::database::{Pool, get_database_client};
|
||||
use crate::errors::DatabaseError;
|
||||
use crate::models::profiles::queries::{
|
||||
get_profile_by_actor_id,
|
||||
create_profile,
|
||||
};
|
||||
use crate::utils::crypto::{deserialize_public_key, verify_signature};
|
||||
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
|
@ -60,7 +59,7 @@ fn parse_http_signature(
|
|||
|
||||
let signature_parameter_re = Regex::new(SIGNATURE_PARAMETER_RE).unwrap();
|
||||
let mut signature_parameters = HashMap::new();
|
||||
for item in signature_header.split(",") {
|
||||
for item in signature_header.split(',') {
|
||||
let caps = signature_parameter_re.captures(item)
|
||||
.ok_or(VerificationError::HeaderError("invalid signature header"))?;
|
||||
let key = caps["key"].to_string();
|
||||
|
@ -123,23 +122,16 @@ pub async fn verify_http_signature(
|
|||
)?;
|
||||
|
||||
let db_client = &**get_database_client(db_pool).await?;
|
||||
let actor_profile = match get_profile_by_actor_id(db_client, &signature_data.actor_id).await {
|
||||
let actor_profile = match get_or_import_profile_by_actor_id(
|
||||
db_client,
|
||||
&config.instance(),
|
||||
&config.media_dir(),
|
||||
&signature_data.actor_id,
|
||||
).await {
|
||||
Ok(profile) => profile,
|
||||
Err(err) => match err {
|
||||
DatabaseError::NotFound(_) => {
|
||||
let profile_data = fetch_profile_by_actor_id(
|
||||
&config.instance(),
|
||||
&signature_data.actor_id,
|
||||
&config.media_dir(),
|
||||
).await.map_err(|err| {
|
||||
VerificationError::ActorError(err.to_string())
|
||||
})?;
|
||||
let profile = create_profile(db_client, &profile_data).await?;
|
||||
profile
|
||||
},
|
||||
other_error => {
|
||||
return Err(other_error.into());
|
||||
},
|
||||
Err(ImportError::DatabaseError(error)) => return Err(error.into()),
|
||||
Err(other_error) => {
|
||||
return Err(VerificationError::ActorError(other_error.to_string()));
|
||||
},
|
||||
};
|
||||
let actor = actor_profile.remote_actor().ok().flatten()
|
||||
|
|
|
@ -82,7 +82,7 @@ impl Status {
|
|||
.map(|item| Mention::from_profile(item, instance_url))
|
||||
.collect();
|
||||
let tags: Vec<Tag> = post.tags.into_iter()
|
||||
.map(|tag_name| Tag::from_tag_name(tag_name))
|
||||
.map(Tag::from_tag_name)
|
||||
.collect();
|
||||
let account = Account::from_profile(post.author, instance_url);
|
||||
let reblog = if let Some(repost_of) = post.repost_of {
|
||||
|
|
|
@ -395,7 +395,7 @@ async fn unreblog(
|
|||
let activity = create_activity_undo_announce(
|
||||
&config.instance_url(),
|
||||
¤t_user.profile,
|
||||
&repost_id,
|
||||
repost_id,
|
||||
primary_recipient.as_ref(),
|
||||
);
|
||||
deliver_activity(&config, ¤t_user, activity, recipients);
|
||||
|
|
|
@ -22,7 +22,7 @@ pub fn save_file(data: Vec<u8>, output_dir: &Path) -> Result<String, FileError>
|
|||
let digest = Sha256::digest(&data);
|
||||
let mut file_name = hex::encode(digest);
|
||||
let maybe_extension = data.sniff_mime_type()
|
||||
.and_then(|media_type| get_mime_extensions_str(media_type))
|
||||
.and_then(get_mime_extensions_str)
|
||||
.and_then(|extensions| extensions.first());
|
||||
if let Some(extension) = maybe_extension {
|
||||
// Append extension for known media types
|
||||
|
|
Loading…
Reference in a new issue