mod error;
pub mod insecure;
#[cfg(feature = "rest-api")]
pub mod rest_api;
pub mod storage;
use std::collections::HashMap;
use std::fmt;
use crate::hex::to_hex;
pub use error::{KeyPermissionError, KeyRegistryError};
#[derive(Clone)]
pub struct KeyInfo {
public_key: Vec<u8>,
associated_node_id: String,
metadata: HashMap<String, String>,
}
impl KeyInfo {
pub fn builder(public_key: Vec<u8>, associated_node_id: String) -> KeyInfoBuilder {
KeyInfoBuilder {
public_key,
associated_node_id,
metadata: HashMap::default(),
}
}
pub fn public_key(&self) -> &[u8] {
&self.public_key
}
pub fn associated_node_id(&self) -> &str {
&self.associated_node_id
}
pub fn get_metadata(&self, key: &str) -> Option<&String> {
self.metadata.get(key)
}
pub fn metadata(&self) -> &HashMap<String, String> {
&self.metadata
}
}
impl fmt::Debug for KeyInfo {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
r#"KeyInfo {{ public_key: "{}", associated_node_id: {:?}, metadata: {:?} }}"#,
to_hex(&self.public_key),
&self.associated_node_id,
&self.metadata
)
}
}
pub struct KeyInfoBuilder {
public_key: Vec<u8>,
associated_node_id: String,
metadata: HashMap<String, String>,
}
impl KeyInfoBuilder {
pub fn with_metadata<S: Into<String>>(mut self, key: S, value: S) -> Self {
self.metadata.insert(key.into(), value.into());
self
}
pub fn build(self) -> KeyInfo {
KeyInfo {
public_key: self.public_key,
associated_node_id: self.associated_node_id,
metadata: self.metadata,
}
}
}
type KeyRegistryResult<T> = Result<T, KeyRegistryError>;
pub trait KeyRegistry: Send + Sync {
fn save_key(&mut self, key_info: KeyInfo) -> KeyRegistryResult<()>;
fn save_keys(&mut self, key_infos: Vec<KeyInfo>) -> KeyRegistryResult<()>;
fn delete_key(&mut self, public_key: &[u8]) -> KeyRegistryResult<Option<KeyInfo>>;
fn get_key(&self, public_key: &[u8]) -> KeyRegistryResult<Option<KeyInfo>>;
fn keys<'iter, 'a: 'iter>(
&'a self,
) -> KeyRegistryResult<Box<dyn Iterator<Item = KeyInfo> + 'iter>>;
fn count(&self) -> KeyRegistryResult<usize>;
fn clone_box(&self) -> Box<dyn KeyRegistry>;
}
impl Clone for Box<dyn KeyRegistry> {
fn clone(&self) -> Self {
self.clone_box()
}
}
type KeyPermissionResult<T> = Result<T, KeyPermissionError>;
pub trait KeyPermissionManager: Send {
fn is_permitted(&self, public_key: &[u8], role: &str) -> KeyPermissionResult<bool>;
}