forge
Explore
Log in

Don't you dare go Hollow

im_old

Clone
★ 0

0 watching0 forksRustGPL-3.0755.6 KiB84318dcTahinli2 months ago77 commits

model.rs127 lines, 4.3 KiBHistoryRawDownload
1//! Record shapes shared by every im crate.
2//!
3//! No crypto, no database, no CSPRNG here: this module compiles anywhere,
4//! including a browser bundle. Id wrappers are plain newtypes; minting lives
5//! behind the `server` feature where `ulid` is available.
6
7use serde::{Deserialize, Serialize};
8use time::OffsetDateTime;
9
10/// The timezone a fresh account — and every row the migration backfills —
11/// starts on. The value is one of the fixed-offset strings the settings
12/// select offers (iz's format, carried verbatim over the directory).
13pub const DEFAULT_TIMEZONE: &str = "UTC+03:00";
14
15macro_rules! id_wrapper {
16 ($name:ident, $doc:literal) => {
17 #[doc = $doc]
18 #[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
19 #[serde(transparent)]
20 pub struct $name(pub String);
21
22 impl $name {
23 /// A fresh ULID — sortable, URL-safe, unguessable enough for an id.
24 #[cfg(feature = "server")]
25 pub fn mint() -> Self {
26 Self(ulid::Ulid::generate().to_string())
27 }
28
29 pub fn as_str(&self) -> &str {
30 &self.0
31 }
32 }
33
34 impl std::fmt::Display for $name {
35 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
36 f.write_str(&self.0)
37 }
38 }
39
40 impl From<String> for $name {
41 fn from(raw: String) -> Self {
42 Self(raw)
43 }
44 }
45 };
46}
47
48id_wrapper!(UserId, "A user's id — the OIDC `sub` claim.");
49id_wrapper!(ClientId, "An OIDC client's id.");
50
51/// A registered user. `password_hash` and the TOTP material never leave
52/// `accounts`/`totp` — this shape is what the rest of the system sees.
53#[derive(Debug, Clone, Serialize, Deserialize)]
54pub struct User {
55 pub id: UserId,
56 pub email: String,
57 pub name: String,
58 pub totp_confirmed: bool,
59 /// Whether this person has a profile photo: the row keeps the mime
60 /// type, the bytes live as a file under the storage tree (`photos.rs`).
61 pub has_photo: bool,
62 /// How many times the photo has changed: the `/photo/{id}?v=` cache
63 /// buster. Lives in the row, so every process — and every sibling app
64 /// reading `/directory` — sees the same one, restart-proof.
65 pub photo_version: u64,
66 /// im's own admin flag — crosses only via /introspect to registered apps,
67 /// never in id_token/userinfo; apps may derive their own admin
68 /// authorization from it.
69 pub admin: bool,
70 pub disabled: bool,
71 pub created_at: OffsetDateTime,
72 /// Display preferences, server-side only: `light`/`dark`, `en`/`tr`,
73 /// `instrument`/`ledger`. Validation lives in the web layer; core
74 /// stores the strings.
75 pub theme: String,
76 pub language: String,
77 pub ui: String,
78 /// Display-only fixed offset in iz's spelling ("UTC+03:00", or "UTC"
79 /// for zero). Apps read it over the directory to render times.
80 pub timezone: String,
81}
82
83/// An outstanding (or spent) invite, as stored. The raw token exists only in
84/// the mail that carried it; the row holds its digest.
85#[derive(Debug, Clone)]
86pub struct Invite {
87 pub email: String,
88 pub invited_by: Option<UserId>,
89 /// An admin invite mints an admin.
90 pub admin: bool,
91 pub created_at: OffsetDateTime,
92 pub expires_at: OffsetDateTime,
93 pub accepted_at: Option<OffsetDateTime>,
94}
95
96/// An OIDC relying party registered with im.
97#[derive(Debug, Clone)]
98pub struct OidcClient {
99 pub client_id: ClientId,
100 pub name: String,
101 pub secret_hash: String,
102 pub redirect_uris: Vec<String>,
103 pub created_at: OffsetDateTime,
104}
105
106/// An authorization code mid-exchange. Consumed exactly once.
107#[derive(Debug, Clone)]
108pub struct AuthCode {
109 pub client_id: ClientId,
110 pub user_id: UserId,
111 pub redirect_uri: String,
112 pub nonce: Option<String>,
113 pub code_challenge: String,
114 /// The central session that minted this code — the refresh token issued
115 /// off it binds to the same session, so logout kills the chain.
116 pub session_hash: String,
117 pub expires_at: OffsetDateTime,
118}
119
120/// A refresh token row, post-lookup.
121#[derive(Debug, Clone)]
122pub struct RefreshToken {
123 pub user_id: UserId,
124 pub client_id: ClientId,
125 pub session_hash: String,
126 pub expires_at: OffsetDateTime,
127}