use crate::identity::attributes::SignerAttributes;
use crate::identity::token::HardwareToken;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ActorType {
Manufacturer,
TestingLab,
IssuingAuthorityOfficer,
BimlDirector,
QuorumCoordinator,
Verifier,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum SigningKeyHandle {
Software {
key_id: String,
algorithm: String,
},
Hardware {
key_id: String,
algorithm: String,
token: HardwareToken,
},
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum EncryptionKeyHandle {
Software {
key_id: String,
algorithm: String,
},
Hardware {
key_id: String,
algorithm: String,
token: HardwareToken,
},
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ActorIdentity {
pub actor_id: String,
pub actor_type: ActorType,
pub quorum_id: Option<String>,
pub signing_key: SigningKeyHandle,
pub encryption_key: Option<EncryptionKeyHandle>,
pub certificate_chain_der: Vec<Vec<u8>>,
pub hardware_token: Option<HardwareToken>,
pub attributes: SignerAttributes,
pub registered_at: DateTime<Utc>,
pub expires_at: Option<DateTime<Utc>>,
}
impl ActorIdentity {
pub fn builder() -> ActorIdentityBuilder {
ActorIdentityBuilder::default()
}
}
#[derive(Debug, Default, Clone)]
pub struct ActorIdentityBuilder {
actor_id: Option<String>,
actor_type: Option<ActorType>,
quorum_id: Option<String>,
signing_key: Option<SigningKeyHandle>,
encryption_key: Option<EncryptionKeyHandle>,
certificate_chain_der: Vec<Vec<u8>>,
hardware_token: Option<HardwareToken>,
attributes: SignerAttributes,
expires_at: Option<DateTime<Utc>>,
}
impl ActorIdentityBuilder {
pub fn actor_id(mut self, id: impl Into<String>) -> Self {
self.actor_id = Some(id.into());
self
}
pub fn actor_type(mut self, t: ActorType) -> Self {
self.actor_type = Some(t);
self
}
pub fn quorum_id(mut self, id: impl Into<String>) -> Self {
self.quorum_id = Some(id.into());
self
}
pub fn signing_key(mut self, key: SigningKeyHandle) -> Self {
self.signing_key = Some(key);
self
}
pub fn encryption_key(mut self, key: EncryptionKeyHandle) -> Self {
self.encryption_key = Some(key);
self
}
pub fn certificate_chain(mut self, chain: Vec<Vec<u8>>) -> Self {
self.certificate_chain_der = chain;
self
}
pub fn hardware_token(mut self, token: HardwareToken) -> Self {
self.hardware_token = Some(token);
self
}
pub fn attributes(mut self, attrs: SignerAttributes) -> Self {
self.attributes = attrs;
self
}
pub fn expires_at(mut self, when: DateTime<Utc>) -> Self {
self.expires_at = Some(when);
self
}
pub fn build(self) -> Result<ActorIdentity, IdentityError> {
Ok(ActorIdentity {
actor_id: self.actor_id.ok_or(IdentityError::MissingField("actor_id"))?,
actor_type: self.actor_type.ok_or(IdentityError::MissingField("actor_type"))?,
quorum_id: self.quorum_id,
signing_key: self.signing_key.ok_or(IdentityError::MissingField("signing_key"))?,
encryption_key: self.encryption_key,
certificate_chain_der: self.certificate_chain_der,
hardware_token: self.hardware_token,
attributes: self.attributes,
registered_at: Utc::now(),
expires_at: self.expires_at,
})
}
}
#[derive(Debug, thiserror::Error)]
pub enum IdentityError {
#[error("missing required field: {0}")]
MissingField(&'static str),
#[error("actor not found: {0}")]
NotFound(String),
#[error("actor already exists: {0}")]
AlreadyExists(String),
#[error("serialization error: {0}")]
Serde(#[from] serde_json::Error),
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn builder_produces_identity() {
let id = ActorIdentity::builder()
.actor_id("biml-director-1")
.actor_type(ActorType::BimlDirector)
.signing_key(SigningKeyHandle::Software {
key_id: "k1".into(),
algorithm: "Ed25519".into(),
})
.build()
.expect("build");
assert_eq!(id.actor_id, "biml-director-1");
assert_eq!(id.actor_type, ActorType::BimlDirector);
}
#[test]
fn builder_fails_without_required_fields() {
let result = ActorIdentity::builder().build();
assert!(result.is_err());
}
}