More shuffling, store actors in db
This commit is contained in:
+43
-43
@@ -1,13 +1,51 @@
|
||||
use crate::{error::MyError, requests::Requests};
|
||||
use crate::{data::ActorCache, error::MyError, requests::Requests};
|
||||
use activitystreams::primitives::XsdAnyUri;
|
||||
use http_signature_normalization_actix::{prelude::*, verify::DeprecatedAlgorithm};
|
||||
use log::{debug, error, warn};
|
||||
use log::{error, warn};
|
||||
use rsa::{hash::Hashes, padding::PaddingScheme, PublicKey, RSAPublicKey};
|
||||
use rsa_pem::KeyExt;
|
||||
use sha2::{Digest, Sha256};
|
||||
use std::{future::Future, pin::Pin};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct MyVerify(pub Requests);
|
||||
pub struct MyVerify(pub Requests, pub ActorCache);
|
||||
|
||||
impl MyVerify {
|
||||
async fn verify(
|
||||
&self,
|
||||
algorithm: Option<Algorithm>,
|
||||
key_id: String,
|
||||
signature: String,
|
||||
signing_string: String,
|
||||
) -> Result<bool, MyError> {
|
||||
let mut uri: XsdAnyUri = key_id.parse()?;
|
||||
uri.as_url_mut().set_fragment(None);
|
||||
let actor = self.1.get(&uri, &self.0).await?;
|
||||
|
||||
let public_key = RSAPublicKey::from_pem_pkcs8(&actor.public_key)?;
|
||||
|
||||
match algorithm {
|
||||
Some(Algorithm::Hs2019) => (),
|
||||
Some(Algorithm::Deprecated(DeprecatedAlgorithm::RsaSha256)) => (),
|
||||
other => {
|
||||
warn!("Invalid algorithm supplied for signature, {:?}", other);
|
||||
return Err(MyError::Algorithm);
|
||||
}
|
||||
};
|
||||
|
||||
let decoded = base64::decode(signature)?;
|
||||
let hashed = Sha256::digest(signing_string.as_bytes());
|
||||
|
||||
public_key.verify(
|
||||
PaddingScheme::PKCS1v15,
|
||||
Some(&Hashes::SHA2_256),
|
||||
&hashed,
|
||||
&decoded,
|
||||
)?;
|
||||
|
||||
Ok(true)
|
||||
}
|
||||
}
|
||||
|
||||
impl SignatureVerify for MyVerify {
|
||||
type Error = MyError;
|
||||
@@ -24,10 +62,10 @@ impl SignatureVerify for MyVerify {
|
||||
let signature = signature.to_owned();
|
||||
let signing_string = signing_string.to_owned();
|
||||
|
||||
let client = self.0.clone();
|
||||
let this = self.clone();
|
||||
|
||||
Box::pin(async move {
|
||||
verify(client, algorithm, key_id, signature, signing_string)
|
||||
this.verify(algorithm, key_id, signature, signing_string)
|
||||
.await
|
||||
.map_err(|e| {
|
||||
error!("Failed to verify, {}", e);
|
||||
@@ -36,41 +74,3 @@ impl SignatureVerify for MyVerify {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
async fn verify(
|
||||
client: Requests,
|
||||
algorithm: Option<Algorithm>,
|
||||
key_id: String,
|
||||
signature: String,
|
||||
signing_string: String,
|
||||
) -> Result<bool, MyError> {
|
||||
debug!("Fetching actor");
|
||||
let actor = client.fetch_actor(&key_id.parse()?).await?;
|
||||
|
||||
debug!("Parsing public key");
|
||||
let public_key = RSAPublicKey::from_pem_pkcs8(&actor.public_key.public_key_pem)?;
|
||||
|
||||
match algorithm {
|
||||
Some(Algorithm::Hs2019) => (),
|
||||
Some(Algorithm::Deprecated(DeprecatedAlgorithm::RsaSha256)) => (),
|
||||
other => {
|
||||
warn!("Invalid algorithm supplied for signature, {:?}", other);
|
||||
return Err(MyError::Algorithm);
|
||||
}
|
||||
};
|
||||
|
||||
debug!("Decoding base64");
|
||||
let decoded = base64::decode(signature)?;
|
||||
debug!("hashing");
|
||||
let hashed = Sha256::digest(signing_string.as_bytes());
|
||||
|
||||
debug!("Verifying signature for {}", key_id);
|
||||
public_key.verify(
|
||||
PaddingScheme::PKCS1v15,
|
||||
Some(&Hashes::SHA2_256),
|
||||
&hashed,
|
||||
&decoded,
|
||||
)?;
|
||||
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user