|
@@ -2,26 +2,26 @@ use std::collections::HashMap;
|
|
use ring::signature::Ed25519KeyPair;
|
|
use ring::signature::Ed25519KeyPair;
|
|
use sha2::Digest;
|
|
use sha2::Digest;
|
|
use crate::schema;
|
|
use crate::schema;
|
|
|
|
+use microrm::prelude::*;
|
|
|
|
|
|
-pub struct CertStore<'a> {
|
|
|
|
- db: &'a crate::DB,
|
|
|
|
|
|
+pub struct CertStore {
|
|
keys: HashMap<String, Ed25519KeyPair>,
|
|
keys: HashMap<String, Ed25519KeyPair>,
|
|
}
|
|
}
|
|
|
|
|
|
-impl<'a> CertStore<'a> {
|
|
|
|
- pub fn new(db: &'a crate::DB) -> Self {
|
|
|
|
- Self { db, keys: HashMap::new() }
|
|
|
|
|
|
+impl CertStore {
|
|
|
|
+ pub fn new() -> Self {
|
|
|
|
+ Self { keys: HashMap::new() }
|
|
}
|
|
}
|
|
|
|
|
|
- fn realm_id(&self, realm_name: &str) -> Option<schema::RealmID> {
|
|
|
|
- microrm::query::get_one_by(self.db, schema::RealmColumns::Shortname, realm_name).map(|x| x.id())
|
|
|
|
|
|
+ fn realm_id(&self, qi: µrm::QueryInterface, realm_name: &str) -> Option<schema::RealmID> {
|
|
|
|
+ qi.get().by(schema::Realm::Shortname, realm_name).one().ok()?.map(|v| v.id())
|
|
}
|
|
}
|
|
|
|
|
|
- pub fn generate_in(&self, realm_name: &str) -> Result<String, &'static str> {
|
|
|
|
|
|
+ pub fn generate_in(&self, qi: µrm::QueryInterface, realm_name: &str) -> Result<String, &'static str> {
|
|
let mut rng = ring::rand::SystemRandom::new();
|
|
let mut rng = ring::rand::SystemRandom::new();
|
|
let sign_generated = Ed25519KeyPair::generate_pkcs8(&mut rng);
|
|
let sign_generated = Ed25519KeyPair::generate_pkcs8(&mut rng);
|
|
|
|
|
|
- let realm_id = self.realm_id(realm_name).ok_or("Failed to find realm")?;
|
|
|
|
|
|
+ let realm_id = self.realm_id(qi, realm_name).ok_or("Failed to find realm")?;
|
|
|
|
|
|
if let Err(_) = sign_generated {
|
|
if let Err(_) = sign_generated {
|
|
return Err("Failed to generate key")
|
|
return Err("Failed to generate key")
|
|
@@ -35,16 +35,16 @@ impl<'a> CertStore<'a> {
|
|
let mut key_id = base64::encode(key_hasher.finalize());
|
|
let mut key_id = base64::encode(key_hasher.finalize());
|
|
key_id.truncate(16);
|
|
key_id.truncate(16);
|
|
|
|
|
|
- microrm::query::add(self.db, &schema::Key { realm: realm_id, key_id: key_id.clone(), keydata }).expect("Couldn't add key");
|
|
|
|
|
|
+ qi.add(&schema::Key { realm: realm_id, key_id: key_id.clone(), keydata }).expect("Couldn't add key");
|
|
|
|
|
|
Ok(key_id)
|
|
Ok(key_id)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-pub fn inspect(db: &crate::DB, realm_name: &str) {
|
|
|
|
- let cs = CertStore::new(db);
|
|
|
|
- println!("Certstore loaded.");
|
|
|
|
- let realm = microrm::query::get_one_by(db, schema::RealmColumns::Shortname, realm_name);
|
|
|
|
|
|
+pub fn inspect(db: µrm::DB, realm_name: &str) {
|
|
|
|
+ let qi = db.query_interface();
|
|
|
|
+
|
|
|
|
+ let realm = qi.get().by(schema::Realm::Shortname, realm_name).one().expect("couldn't load realm");
|
|
if realm.is_none() {
|
|
if realm.is_none() {
|
|
println!("No such realm {}", realm_name);
|
|
println!("No such realm {}", realm_name);
|
|
return
|
|
return
|
|
@@ -52,15 +52,16 @@ pub fn inspect(db: &crate::DB, realm_name: &str) {
|
|
let realm = realm.unwrap();
|
|
let realm = realm.unwrap();
|
|
|
|
|
|
println!("Retrieving keys for {} realm...", realm_name);
|
|
println!("Retrieving keys for {} realm...", realm_name);
|
|
- let keys = microrm::query::get_all_by(db, schema::KeyColumns::Realm, realm.id()).expect("Can get keys");
|
|
|
|
|
|
+ let keys = qi.get().by(schema::Key::Realm, &realm.id()).all().expect("couldn't load keys"); // microrm::query::get_all_by(db, schema::KeyColumns::Realm, realm.id()).expect("Can get keys");
|
|
for key in keys {
|
|
for key in keys {
|
|
- println!("[{:20}]", key.key_id);
|
|
|
|
|
|
+ println!("- [{:20}]", key.key_id);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-pub fn generate(db: &crate::DB, realm_name: &str) {
|
|
|
|
- let cs = CertStore::new(db);
|
|
|
|
- if let Err(e) = cs.generate_in(realm_name) {
|
|
|
|
|
|
+pub fn generate(db: µrm::DB, realm_name: &str) {
|
|
|
|
+ let cs = CertStore::new();
|
|
|
|
+ let qi = db.query_interface();
|
|
|
|
+ if let Err(e) = cs.generate_in(&qi, realm_name) {
|
|
println!("Failed to generate key: {}", e);
|
|
println!("Failed to generate key: {}", e);
|
|
}
|
|
}
|
|
}
|
|
}
|