2018-05-04 17:18:00 +02:00
|
|
|
use base64;
|
|
|
|
use diesel::PgConnection;
|
|
|
|
use hex;
|
|
|
|
use openssl::hash::{Hasher, MessageDigest};
|
|
|
|
use openssl::sha::sha256;
|
|
|
|
use reqwest::header::{Date, Headers, UserAgent};
|
|
|
|
use std::time::SystemTime;
|
|
|
|
|
|
|
|
use activity_pub::sign::Signer;
|
|
|
|
|
|
|
|
const USER_AGENT: &'static str = "Plume/0.1.0";
|
|
|
|
|
|
|
|
header! {
|
|
|
|
(Signature, "Signature") => [String]
|
|
|
|
}
|
|
|
|
|
|
|
|
header! {
|
|
|
|
(Digest, "Digest") => [String]
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn headers() -> Headers {
|
|
|
|
let mut headers = Headers::new();
|
|
|
|
headers.set(UserAgent::new(USER_AGENT));
|
|
|
|
headers.set(Date(SystemTime::now().into()));
|
|
|
|
headers
|
|
|
|
}
|
|
|
|
|
2018-05-04 17:24:56 +02:00
|
|
|
pub fn signature<S: Signer>(signer: &S, headers: Headers, conn: &PgConnection) -> Signature {
|
2018-05-04 17:18:00 +02:00
|
|
|
let signed_string = headers.iter().map(|h| format!("{}: {}", h.name().to_lowercase(), h.value_string())).collect::<Vec<String>>().join("\n");
|
|
|
|
let signed_headers = headers.iter().map(|h| h.name().to_string()).collect::<Vec<String>>().join(" ").to_lowercase();
|
|
|
|
|
2018-05-08 21:38:37 +02:00
|
|
|
println!("Signing {}", signed_string);
|
|
|
|
println!("signed header {}", signed_headers);
|
2018-05-04 17:18:00 +02:00
|
|
|
let data = signer.sign(signed_string);
|
2018-05-08 21:38:37 +02:00
|
|
|
let sign = base64::encode(&data[..]);
|
|
|
|
println!("sigature {}", sign);
|
2018-05-04 17:18:00 +02:00
|
|
|
|
|
|
|
Signature(format!(
|
2018-05-08 21:38:37 +02:00
|
|
|
"keyId=\"{key_id}\",algorithm=\"rsa-sha256\",headers=\"{signed_headers}\",signature=\"{signature}\"",
|
2018-05-04 17:18:00 +02:00
|
|
|
key_id = signer.get_key_id(conn),
|
|
|
|
signed_headers = signed_headers,
|
|
|
|
signature = sign
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn digest(body: String) -> Digest {
|
|
|
|
let mut hasher = Hasher::new(MessageDigest::sha256()).unwrap();
|
|
|
|
hasher.update(&body.into_bytes()[..]);
|
|
|
|
let res = base64::encode(&hasher.finish().unwrap());
|
|
|
|
Digest(format!("SHA-256={}", res))
|
|
|
|
}
|