WIP: signing
This commit is contained in:
@@ -14,3 +14,4 @@ impl<'a, T: Actor, U: Object> Create<'a, T, U> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
use base64;
|
||||
use hex;
|
||||
use chrono::Utc;
|
||||
use openssl::sha::{sha256, sha512};
|
||||
use serde_json;
|
||||
|
||||
// Comments are from the Mastodon source code, to knremow what to do.
|
||||
|
||||
pub trait Signer {
|
||||
fn get_key_id(&self) -> String;
|
||||
|
||||
/// Sign some data with the signer keypair
|
||||
fn sign(&self, to_sign: String) -> String; // Base64.strict_encode64(creator.keypair.sign(OpenSSL::Digest::SHA256.new, to_be_signed))
|
||||
}
|
||||
|
||||
pub trait Signable {
|
||||
fn sign<T>(&mut self, creator: T) -> &mut Self where T: Signer;
|
||||
|
||||
fn hash(data: String) -> String {
|
||||
let bytes = data.into_bytes();
|
||||
hex::encode(sha256(&bytes[..]))
|
||||
}
|
||||
}
|
||||
|
||||
impl Signable for serde_json::Value {
|
||||
fn sign<T>(&mut self, creator: T) -> &mut serde_json::Value where T: Signer {
|
||||
let mut options = json!({
|
||||
"type": "RsaSignature2017",
|
||||
"creator": creator.get_key_id(), // [ActivityPub::TagManager.instance.uri_for(creator), '#main-key'].join,
|
||||
"created": Utc::now().to_rfc3339()
|
||||
});
|
||||
|
||||
//options_hash = hash(options.without('type', 'id', 'signatureValue').merge('@context' => CONTEXT))
|
||||
let options_hash = Self::hash(String::from(""));
|
||||
let document_hash = Self::hash(self.to_string());
|
||||
let to_be_signed = options_hash + &document_hash;
|
||||
|
||||
let signature = base64::encode(&creator.sign(to_be_signed));
|
||||
|
||||
options["signaureValue"] = serde_json::Value::String(signature);
|
||||
self["signature"] = options;
|
||||
self
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,15 @@
|
||||
#![feature(plugin, custom_derive)]
|
||||
#![plugin(rocket_codegen)]
|
||||
|
||||
extern crate base64;
|
||||
extern crate bcrypt;
|
||||
extern crate chrono;
|
||||
extern crate heck;
|
||||
extern crate hex;
|
||||
#[macro_use]
|
||||
extern crate diesel;
|
||||
extern crate dotenv;
|
||||
extern crate openssl;
|
||||
extern crate rocket;
|
||||
extern crate rocket_contrib;
|
||||
#[feature(custom_attribute)]
|
||||
|
||||
Reference in New Issue
Block a user