//! Record shapes shared by every im crate. //! //! No crypto, no database, no CSPRNG here: this module compiles anywhere, //! including a browser bundle. Id wrappers are plain newtypes; minting lives //! behind the `server` feature where `ulid` is available. use serde::{Deserialize, Serialize}; use time::OffsetDateTime; /// The timezone a fresh account — and every row the migration backfills — /// starts on. The value is one of the fixed-offset strings the settings /// select offers (iz's format, carried verbatim over the directory). pub const DEFAULT_TIMEZONE: &str = "UTC+03:00"; macro_rules! id_wrapper { ($name:ident, $doc:literal) => { #[doc = $doc] #[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] #[serde(transparent)] pub struct $name(pub String); impl $name { /// A fresh ULID — sortable, URL-safe, unguessable enough for an id. #[cfg(feature = "server")] pub fn mint() -> Self { Self(ulid::Ulid::generate().to_string()) } pub fn as_str(&self) -> &str { &self.0 } } impl std::fmt::Display for $name { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.write_str(&self.0) } } impl From for $name { fn from(raw: String) -> Self { Self(raw) } } }; } id_wrapper!(UserId, "A user's id — the OIDC `sub` claim."); id_wrapper!(ClientId, "An OIDC client's id."); /// A registered user. `password_hash` and the TOTP material never leave /// `accounts`/`totp` — this shape is what the rest of the system sees. #[derive(Debug, Clone, Serialize, Deserialize)] pub struct User { pub id: UserId, pub email: String, pub name: String, pub totp_confirmed: bool, /// Whether this person has a profile photo: the row keeps the mime /// type, the bytes live as a file under the storage tree (`photos.rs`). pub has_photo: bool, /// How many times the photo has changed: the `/photo/{id}?v=` cache /// buster. Lives in the row, so every process — and every sibling app /// reading `/directory` — sees the same one, restart-proof. pub photo_version: u64, /// im's own admin flag — crosses only via /introspect to registered apps, /// never in id_token/userinfo; apps may derive their own admin /// authorization from it. pub admin: bool, pub disabled: bool, pub created_at: OffsetDateTime, /// Display preferences, server-side only: `light`/`dark`, `en`/`tr`, /// `instrument`/`ledger`. Validation lives in the web layer; core /// stores the strings. pub theme: String, pub language: String, pub ui: String, /// Display-only fixed offset in iz's spelling ("UTC+03:00", or "UTC" /// for zero). Apps read it over the directory to render times. pub timezone: String, } /// An outstanding (or spent) invite, as stored. The raw token exists only in /// the mail that carried it; the row holds its digest. #[derive(Debug, Clone)] pub struct Invite { pub email: String, pub invited_by: Option, /// An admin invite mints an admin. pub admin: bool, pub created_at: OffsetDateTime, pub expires_at: OffsetDateTime, pub accepted_at: Option, } /// An OIDC relying party registered with im. #[derive(Debug, Clone)] pub struct OidcClient { pub client_id: ClientId, pub name: String, pub secret_hash: String, pub redirect_uris: Vec, pub created_at: OffsetDateTime, } /// An authorization code mid-exchange. Consumed exactly once. #[derive(Debug, Clone)] pub struct AuthCode { pub client_id: ClientId, pub user_id: UserId, pub redirect_uri: String, pub nonce: Option, pub code_challenge: String, /// The central session that minted this code — the refresh token issued /// off it binds to the same session, so logout kills the chain. pub session_hash: String, pub expires_at: OffsetDateTime, } /// A refresh token row, post-lookup. #[derive(Debug, Clone)] pub struct RefreshToken { pub user_id: UserId, pub client_id: ClientId, pub session_hash: String, pub expires_at: OffsetDateTime, }