forge
Explore
Log in

Don't you dare go Hollow

hezarfen_backend

Clone
★ 0

0 watching0 forksRust8.3 MiB6d68754Ahmet Kaan Gümüş2 months ago566 commits

builder.rs93 lines, 2.6 KiBHistoryRawDownload
1//! The deployment operator's account, in the control database.
2//!
3//! A builder creates, suspends and drops schools; it is **not** a user of any
4//! school and holds no role there. The two principals are kept apart by
5//! construction: a builder row lives in a different database and its cookie
6//! carries a different prefix (`builder.<token>` against `<uuid>.<token>`), so
7//! neither extractor can be fed the other's cookie.
8//!
9//! The credential newtypes are [`crate::domain::user`]'s — same rules, same
10//! argon2 discipline, deliberately not a second copy that could drift.
11
12use uuid::Uuid;
13
14use crate::domain::monotonic_id::next_uuid;
15use crate::domain::session::SessionToken;
16use crate::domain::timestamp::Timestamp;
17use crate::domain::user::{PasswordHash, Username};
18
19pub const BUILDER_TABLE: &str = "builder";
20pub const BUILDER_SESSION_TABLE: &str = "builder_session";
21
22#[derive(Debug, Clone, PartialEq, Eq, sqlx::Type)]
23#[sqlx(transparent)]
24pub struct BuilderId(Uuid);
25
26impl BuilderId {
27 /// A plain random UUID deliberately: builder rows are looked up by
28 /// username, never listed by id.
29 pub fn generate() -> Self {
30 Self(next_uuid())
31 }
32
33 /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that
34 /// cannot take the newtype. Static `query!` binds take `self` directly.
35 pub fn uuid(&self) -> Uuid {
36 self.0
37 }
38
39 /// Parses a wire key. A key that is not a UUID parses as the nil UUID,
40 /// which matches no row.
41 pub fn from_key(key: &str) -> Self {
42 Self(Uuid::parse_str(key).unwrap_or(Uuid::nil()))
43 }
44
45 pub fn key(&self) -> String {
46 self.0.to_string()
47 }
48}
49
50#[derive(Debug, Clone, sqlx::FromRow)]
51pub struct Builder {
52 pub(crate) id: BuilderId,
53 pub(crate) username: Username,
54 pub(crate) password_hash: PasswordHash,
55}
56
57impl Builder {
58 pub fn get_id(&self) -> &BuilderId {
59 &self.id
60 }
61
62 pub fn get_username(&self) -> &Username {
63 &self.username
64 }
65
66 pub fn get_password_hash(&self) -> &PasswordHash {
67 &self.password_hash
68 }
69}
70
71/// A builder's login session. [`crate::domain::session::Session`]'s shape
72/// against the control database, with the token type shared so both cookies
73/// are the same 64 hex chars.
74#[derive(Debug, Clone, sqlx::FromRow)]
75pub struct BuilderSession {
76 pub(crate) builder: BuilderId,
77 pub(crate) token: SessionToken,
78 pub(crate) expires_at: Timestamp,
79}
80
81impl BuilderSession {
82 pub fn token(&self) -> &SessionToken {
83 &self.token
84 }
85
86 pub fn builder(&self) -> &BuilderId {
87 &self.builder
88 }
89
90 pub fn is_expired(&self) -> bool {
91 self.expires_at.is_past()
92 }
93}