use jacquard_common::types::string::{Did as AtDid, Handle as AtHandle}; use serde::{Deserialize, Serialize}; #[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)] pub struct Did(String); impl Did { pub fn parse(value: &str) -> Result { if value.trim().is_empty() { return Err(IdentityError::EmptyValue); } AtDid::::new(value.to_owned()).map_err(|_| IdentityError::InvalidDid)?; Ok(Self(value.to_owned())) } pub fn as_str(&self) -> &str { &self.0 } } #[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)] pub struct Handle(String); impl Handle { pub fn parse(value: &str) -> Result { if value.trim().is_empty() { return Err(IdentityError::EmptyValue); } let handle = AtHandle::::new_owned(value).map_err(|_| IdentityError::InvalidHandle)?; Ok(Self(handle.as_str().to_owned())) } pub fn as_str(&self) -> &str { &self.0 } } impl std::fmt::Display for Handle { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.write_str(&self.0) } } pub fn normalize_login(input: &str) -> Result { let trimmed = input.trim(); let (mention, body) = match trimmed.strip_prefix('@') { Some(rest) => (true, rest), None => (false, trimmed), }; if body.is_empty() { return Err(IdentityError::EmptyValue); } let candidate = match body.split_once('@') { None => body.to_owned(), Some(_) if mention => return Err(IdentityError::AmbiguousLogin), Some((local, domain)) => { if local.is_empty() || domain.is_empty() || domain.contains('@') { return Err(IdentityError::AmbiguousLogin); } format!("{local}.{domain}") } }; Handle::parse(&candidate) } #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] pub struct Identity { pub did: Did, pub handle: Handle, } #[derive(Clone, Debug, Eq, PartialEq, thiserror::Error)] pub enum IdentityError { #[error("identity value must not be empty")] EmptyValue, #[error("ambiguous login")] AmbiguousLogin, #[error("handle must be a dotted domain name")] InvalidHandle, #[error("invalid DID")] InvalidDid, } #[cfg(test)] mod tests { use super::*; fn normalized(input: &str) -> Result { normalize_login(input).map(|handle| handle.to_string()) } #[test] fn logins_normalize_to_one_handle() { for (input, expected) in [ ("teq.shatteredsky.net", "teq.shatteredsky.net"), ("@teq.shatteredsky.net", "teq.shatteredsky.net"), ("teq@shatteredsky.net", "teq.shatteredsky.net"), (" Teq@ShatteredSky.net ", "teq.shatteredsky.net"), ("shatteredsky.net", "shatteredsky.net"), ("@shatteredsky.net", "shatteredsky.net"), ("alice@alice.example.com", "alice.alice.example.com"), ] { assert_eq!( normalized(input).as_deref(), Ok(expected), "input {input:?}" ); } } #[test] fn malformed_logins_and_dids_are_rejected() { for (input, expected) in [ ("", IdentityError::EmptyValue), ("@", IdentityError::EmptyValue), ("@teq@x.net", IdentityError::AmbiguousLogin), ("teq@", IdentityError::AmbiguousLogin), ("a@b@c", IdentityError::AmbiguousLogin), ("teq", IdentityError::InvalidHandle), ("te q.net", IdentityError::InvalidHandle), ] { assert_eq!(normalized(input), Err(expected), "input {input:?}"); } assert!(Did::parse("did:plc:aaaaaaaaaaaaaaaaaaaaaaaa").is_ok()); assert!(Did::parse("did:web:shatteredsky.net").is_ok()); assert_eq!(Did::parse(""), Err(IdentityError::EmptyValue)); assert_eq!(Did::parse("not a did"), Err(IdentityError::InvalidDid)); } }