use std::collections::HashMap;
use std::fmt;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use crate::error::TenancyError;
use crate::ports::Encryptor;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
pub struct TenantId(pub i64);
impl fmt::Display for TenantId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Domain {
pub host: String,
pub is_primary: bool,
}
impl Domain {
pub fn new(host: impl Into<String>, is_primary: bool) -> Self {
Self {
host: normalize_host(&host.into()),
is_primary,
}
}
pub fn primary(host: impl Into<String>) -> Self {
Self::new(host, true)
}
}
pub fn normalize_host(host: &str) -> String {
let host = host.trim().to_ascii_lowercase();
let without_port = match host.rsplit_once(':') {
Some((name, port))
if !name.contains(':')
&& !name.is_empty()
&& port.chars().all(|c| c.is_ascii_digit()) =>
{
name.to_string()
}
_ => host,
};
without_port.trim_end_matches('.').to_string()
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Tenant {
pub id: TenantId,
pub name: String,
pub slug: String,
pub domains: Vec<Domain>,
pub database: Option<String>,
pub maintenance: bool,
pub config: TenantConfig,
pub data: serde_json::Map<String, serde_json::Value>,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
}
impl Tenant {
pub fn new(name: impl Into<String>, slug: impl Into<String>) -> Result<Self, TenancyError> {
let slug = slug.into();
validate_slug(&slug)?;
let now = Utc::now();
Ok(Self {
id: TenantId(0),
name: name.into(),
slug,
domains: Vec::new(),
database: None,
maintenance: false,
config: TenantConfig::default(),
data: serde_json::Map::new(),
created_at: now,
updated_at: now,
})
}
pub fn primary_domain(&self) -> Option<&str> {
self.domains
.iter()
.find(|domain| domain.is_primary)
.or_else(|| self.domains.first())
.map(|domain| domain.host.as_str())
}
pub fn database_name(&self, prefix: &str) -> String {
if let Some(database) = &self.database {
return database.clone();
}
let candidate = format!("{prefix}{}", self.slug);
let sanitized: String = candidate
.chars()
.map(|c| {
if c.is_ascii_lowercase() || c.is_ascii_digit() || c == '_' {
c
} else {
'_'
}
})
.collect();
debug_assert!(sanitized
.chars()
.all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '_'));
sanitized
}
}
pub fn validate_slug(slug: &str) -> Result<(), TenancyError> {
let valid = !slug.is_empty()
&& slug
.chars()
.all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '-');
if valid {
Ok(())
} else {
Err(TenancyError::InvalidSlug(slug.to_string()))
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ConfigValue {
pub value: String,
pub encrypted: bool,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct TenantConfig(HashMap<String, ConfigValue>);
impl TenantConfig {
pub fn get(&self, key: &str) -> Option<&str> {
self.0.get(key).map(|entry| entry.value.as_str())
}
pub fn set(&mut self, key: impl Into<String>, value: impl Into<String>) {
self.0.insert(
key.into(),
ConfigValue {
value: value.into(),
encrypted: false,
},
);
}
pub fn set_encrypted(&mut self, key: impl Into<String>, value: impl Into<String>) {
self.0.insert(
key.into(),
ConfigValue {
value: value.into(),
encrypted: true,
},
);
}
pub fn remove(&mut self, key: &str) -> Option<ConfigValue> {
self.0.remove(key)
}
pub fn iter(&self) -> impl Iterator<Item = (&str, &ConfigValue)> {
self.0.iter().map(|(key, value)| (key.as_str(), value))
}
pub async fn get_decrypted(
&self,
key: &str,
encryptor: &dyn Encryptor,
) -> Result<Option<String>, TenancyError> {
let Some(entry) = self.0.get(key) else {
return Ok(None);
};
if !entry.encrypted {
return Ok(Some(entry.value.clone()));
}
let ciphertext = decode_hex(&entry.value)?;
let plaintext = encryptor.open(&ciphertext).await?;
String::from_utf8(plaintext)
.map(Some)
.map_err(|error| TenancyError::Crypto(error.to_string()))
}
}
fn decode_hex(input: &str) -> Result<Vec<u8>, TenancyError> {
if !input.len().is_multiple_of(2) {
return Err(TenancyError::Crypto("odd hex length".to_string()));
}
let mut out = Vec::with_capacity(input.len() / 2);
for pair in input.as_bytes().chunks(2) {
let pair =
std::str::from_utf8(pair).map_err(|error| TenancyError::Crypto(error.to_string()))?;
let byte = u8::from_str_radix(pair, 16)
.map_err(|error| TenancyError::Crypto(error.to_string()))?;
out.push(byte);
}
Ok(out)
}