use serde::{Deserialize, Serialize};
use std::collections::HashMap;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SignedMessage {
pub payload: String, pub public_key_id: String, pub signature: String, pub timestamp: i64, }
impl SignedMessage {
pub fn new(payload: String, public_key_id: String, signature: String, timestamp: i64) -> Self {
Self {
payload,
public_key_id,
signature,
timestamp,
}
}
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct PublicKeyRegistration {
pub public_key: String,
pub owner_id: String,
pub permissions: Vec<String>,
pub metadata: HashMap<String, String>,
pub expires_at: Option<u64>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PublicKeyInfo {
pub id: String,
pub public_key: String,
pub owner_id: String,
pub created_at: i64,
pub expires_at: Option<i64>,
pub is_active: bool,
pub permissions: Vec<String>,
pub metadata: HashMap<String, String>,
}
impl PublicKeyInfo {
pub fn new(id: String, public_key: String, owner_id: String, permissions: Vec<String>) -> Self {
Self {
id,
public_key,
owner_id,
created_at: chrono::Utc::now().timestamp(),
expires_at: None,
is_active: true,
permissions,
metadata: HashMap::new(),
}
}
pub fn is_valid(&self) -> bool {
if !self.is_active {
return false;
}
if let Some(expires_at) = self.expires_at {
return chrono::Utc::now().timestamp() < expires_at;
}
true
}
pub fn with_metadata(mut self, key: String, value: String) -> Self {
self.metadata.insert(key, value);
self
}
pub fn with_expiration(mut self, expires_at: i64) -> Self {
self.expires_at = Some(expires_at);
self
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EncryptedData {
pub data: String,
pub nonce: String,
pub tag: String,
pub algorithm: String,
pub encrypted_at: i64,
}
impl EncryptedData {
pub fn new(data: String, nonce: String, tag: String) -> Self {
Self {
data,
nonce,
tag,
algorithm: "AES-256-GCM".to_string(),
encrypted_at: chrono::Utc::now().timestamp(),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KeyRegistrationRequest {
pub public_key: String,
pub owner_id: String,
pub permissions: Vec<String>,
pub metadata: HashMap<String, String>,
pub expires_at: Option<i64>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KeyRegistrationResponse {
pub success: bool,
pub public_key_id: Option<String>,
pub key: Option<PublicKeyInfo>,
pub error: Option<String>,
}
#[derive(Debug, Clone)]
pub struct VerificationResult {
pub is_valid: bool,
pub public_key_info: Option<PublicKeyInfo>,
pub error: Option<String>,
pub timestamp_valid: bool,
}
impl VerificationResult {
pub fn success(public_key_info: PublicKeyInfo, timestamp_valid: bool) -> Self {
Self {
is_valid: true,
public_key_info: Some(public_key_info),
error: None,
timestamp_valid,
}
}
pub fn failure(error: String) -> Self {
Self {
is_valid: false,
public_key_info: None,
error: Some(error),
timestamp_valid: false,
}
}
}