use std::fmt;
use std::str::FromStr;
pub use spoolframe_types::APP_RP_DOMAIN;
const ALPHABET: &[u8; 32] = b"abcdefghijklmnopqrstuvwxyz234567";
pub const SERVER_ID_BYTES: usize = 10;
pub const SERVER_ID_LEN: usize = 16;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ServerId(String);
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[error("{0:?} is not a server id: 16 characters of a-z and 2-7")]
pub struct InvalidServerId(pub String);
impl ServerId {
pub fn from_bytes(bytes: [u8; SERVER_ID_BYTES]) -> Self {
let mut id = String::with_capacity(SERVER_ID_LEN);
for group in bytes.as_chunks::<5>().0 {
let bits = group
.iter()
.fold(0u64, |bits, &byte| (bits << 8) | u64::from(byte));
for at in (0..8).rev() {
id.push(char::from(ALPHABET[((bits >> (at * 5)) & 31) as usize]));
}
}
Self(id)
}
pub fn parse(id: &str) -> Result<Self, InvalidServerId> {
if id.len() == SERVER_ID_LEN && id.bytes().all(|c| ALPHABET.contains(&c)) {
Ok(Self(id.to_owned()))
} else {
Err(InvalidServerId(id.to_owned()))
}
}
pub fn as_str(&self) -> &str {
&self.0
}
pub fn rp_id(&self) -> String {
format!("{}.{APP_RP_DOMAIN}", self.0)
}
pub fn origin(&self) -> String {
format!("https://{}", self.rp_id())
}
}
impl fmt::Display for ServerId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
impl FromStr for ServerId {
type Err = InvalidServerId;
fn from_str(id: &str) -> Result<Self, Self::Err> {
Self::parse(id)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_id_is_lowercase_base32_of_the_bytes() {
assert_eq!(
ServerId::from_bytes(*b"foobarfoob").as_str(),
"mzxw6ytbojtg633c"
);
assert_eq!(ServerId::from_bytes([0; 10]).as_str(), "aaaaaaaaaaaaaaaa");
assert_eq!(
ServerId::from_bytes([0xff; 10]).as_str(),
"7777777777777777"
);
}
fn is_dns_label(label: &str) -> bool {
(1..=63).contains(&label.len())
&& label
.bytes()
.all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == b'-')
&& !label.starts_with('-')
&& !label.ends_with('-')
}
#[test]
fn every_id_is_a_dns_label_of_16_characters() {
for seed in 0..=255u8 {
let bytes = std::array::from_fn(|i| seed.wrapping_mul(31) ^ (i as u8).wrapping_mul(97));
let id = ServerId::from_bytes(bytes);
assert_eq!(id.as_str().len(), SERVER_ID_LEN, "{id}");
assert!(is_dns_label(id.as_str()), "{id}");
assert_eq!(ServerId::parse(id.as_str()), Ok(id));
}
}
#[test]
fn the_relying_party_and_origin_are_under_spoolframe_app() {
let id = ServerId::parse("mzxw6ytbojtg633c").unwrap();
assert_eq!(id.rp_id(), "mzxw6ytbojtg633c.spoolframe.app");
assert_eq!(id.origin(), "https://mzxw6ytbojtg633c.spoolframe.app");
}
#[test]
fn what_is_not_an_id_as_written_is_refused() {
for bad in [
"",
"mzxw6ytboj",
"mzxw6ytbojtg633ca",
"MZXW6YTBOJTG633C",
"mzxw6ytboj1g633c",
"mzxw6ytboj.g633c",
"mzxw6ytboj-g633c",
] {
assert_eq!(
ServerId::parse(bad),
Err(InvalidServerId(bad.into())),
"{bad}"
);
}
}
}