db.rs
⎇
Raw
1use std::path::Path;
2use std::sync::Arc;
3
4pub use api_types::{AuthMode, Mode};
5use rusqlite::types::{FromSql, FromSqlError, FromSqlResult, ToSql, ToSqlOutput, ValueRef};
6use rusqlite::{Connection, OptionalExtension, params};
7
8const SCHEMA_VERSION: i64 = 11;
9
10/// SQL adapter for [`Mode`]. A newtype is needed because both the rusqlite
11/// traits and `Mode` are foreign to this crate.
12///
13/// The stored strings are unchanged ("rw"/"ro"), so old databases still read.
14struct SqlMode(Mode);
15
16impl FromSql for SqlMode {
17 fn column_result(v: ValueRef<'_>) -> FromSqlResult<Self> {
18 let s = v.as_str()?;
19 Mode::from_wire(s)
20 .map(SqlMode)
21 .ok_or_else(|| FromSqlError::Other(format!("unknown mode {s:?}").into()))
22 }
23}
24
25impl ToSql for SqlMode {
26 fn to_sql(&self) -> rusqlite::Result<ToSqlOutput<'_>> {
27 Ok(ToSqlOutput::from(self.0.as_str()))
28 }
29}
30
31/// SQL adapter for [`AuthMode`], for the same reason as [`SqlMode`].
32struct SqlAuthMode(AuthMode);
33
34impl FromSql for SqlAuthMode {
35 fn column_result(v: ValueRef<'_>) -> FromSqlResult<Self> {
36 let s = v.as_str()?;
37 AuthMode::from_wire(s)
38 .map(SqlAuthMode)
39 .ok_or_else(|| FromSqlError::Other(format!("unknown auth mode {s:?}").into()))
40 }
41}
42
43impl ToSql for SqlAuthMode {
44 fn to_sql(&self) -> rusqlite::Result<ToSqlOutput<'_>> {
45 Ok(ToSqlOutput::from(self.0.as_str()))
46 }
47}
48
49#[derive(Debug, Clone)]
50pub struct User {
51 pub id: i64,
52 pub name: String,
53 pub is_admin: bool,
54 /// Disabled users cannot sign in and their sessions are rejected.
55 pub active: bool,
56 /// Profile setting: single click opens entries (off = click selects).
57 pub single_click: bool,
58 /// Profile setting: show thumbnails in the grid.
59 pub thumbnails: bool,
60 /// Preferred UI language tag ("en", "de", "fr"); None = follow the
61 /// browser.
62 pub language: Option<String>,
63 /// Profile setting: the root the UI opens by default. May point at a
64 /// root the user no longer has; the API filters that out.
65 pub default_root_id: Option<i64>,
66 /// What this account needs to sign in.
67 pub auth_mode: AuthMode,
68 /// Whether a password is set. False means the account signs in with
69 /// passkeys only. See [`NO_PASSWORD`].
70 pub has_password: bool,
71}
72
73/// The `pass_hash` of an account with no password.
74///
75/// A sentinel rather than NULL: `users.pass_hash` is `NOT NULL`, and SQLite
76/// cannot drop that constraint without rebuilding the table. Nothing verifies
77/// against it — [`Db::verify_password`] swaps in [`DUMMY_HASH`] so the reject
78/// costs the same as a wrong password, and `PasswordHash::new("")` fails
79/// anyway.
80pub const NO_PASSWORD: &str = "";
81
82/// How many passkeys one account may hold.
83///
84/// Also the exact number of credentials a named sign-in challenge lists. The
85/// two are one number on purpose: the challenge pads a short list with decoys
86/// so its length says nothing about the account, and that only works while no
87/// account can push past the padding.
88pub const PASSKEY_LIMIT: usize = 8;
89
90/// How many app passwords one account may hold. One per client is the point.
91pub const APP_PASSWORD_LIMIT: usize = 10;
92
93/// What [`Db::delete_passkey`] did.
94#[derive(Debug, Clone, Copy, PartialEq, Eq)]
95pub enum PasskeyDeleted {
96 Gone,
97 /// No such passkey, or it belongs to someone else.
98 NotFound,
99 /// Removing it would have left the account unreachable.
100 LastCredential,
101}
102
103/// One registered passkey. `passkey` is the serialized `webauthn-rs`
104/// credential; everything else is for the settings list.
105#[derive(Debug, Clone)]
106pub struct PasskeyRow {
107 pub id: i64,
108 pub name: String,
109 pub created_at: String,
110 pub last_used_at: Option<String>,
111 pub discoverable: Option<bool>,
112 /// `webauthn_rs::prelude::Passkey` as JSON.
113 pub passkey: String,
114}
115
116/// One app password, without its secret.
117#[derive(Debug, Clone)]
118pub struct AppPasswordRow {
119 pub id: i64,
120 pub name: String,
121 pub created_at: String,
122 pub last_used_at: Option<String>,
123}
124
125#[derive(Debug, Clone)]
126pub struct RootRow {
127 pub id: i64,
128 /// Path relative to the server root; "." means the whole root.
129 pub path: String,
130 pub mode: Mode,
131}
132
133#[derive(Debug, Clone)]
134pub struct ShareRow {
135 pub id: i64,
136 pub token: String,
137 pub creator_id: i64,
138 /// Path of the shared item relative to the server root.
139 pub target: String,
140 pub is_file: bool,
141 pub mode: Mode,
142 pub created_at: String,
143 pub expires_at: Option<String>,
144 /// Argon2 hash of the share's password, when it has one. Resolve,
145 /// listing and download all stay locked until the visitor enters it and
146 /// gets an unlock cookie.
147 pub password_hash: Option<String>,
148}
149
150impl ShareRow {
151 pub fn is_expired(&self) -> bool {
152 match &self.expires_at {
153 Some(e) => chrono::DateTime::parse_from_rfc3339(e)
154 .map(|t| chrono::Utc::now() >= t.with_timezone(&chrono::Utc))
155 .unwrap_or(false),
156 None => false,
157 }
158 }
159}
160
161/// A [`ShareRow`] together with the account that created it.
162#[derive(Debug, Clone)]
163pub struct ShareWithCreator {
164 pub share: ShareRow,
165 pub creator_name: String,
166 /// Whether that account can still sign in. Deactivating an account leaves
167 /// its shares live.
168 pub creator_active: bool,
169}
170
171/// The columns [`map_user`] reads, in order. Every SELECT that builds a
172/// [`User`] uses one of these two, so a new column is added in one place.
173/// `USER_COLS_U` is the same list qualified for the queries that join
174/// `users u` against `user_roots`.
175const USER_COLS: &str = "id, name, is_admin != 0, active != 0, single_click != 0,
176 thumbnails != 0, language, default_root_id, auth_mode, pass_hash != ''";
177const USER_COLS_U: &str = "u.id, u.name, u.is_admin != 0, u.active != 0, u.single_click != 0,
178 u.thumbnails != 0, u.language, u.default_root_id, u.auth_mode, u.pass_hash != ''";
179/// How many columns the two lists above cover. The joined queries read the
180/// root columns starting here.
181const USER_COL_COUNT: usize = 10;
182
183/// Every query can fail, and every caller decides what to do about it.
184///
185/// Earlier versions swallowed read errors and returned a default (an empty
186/// root list, a count of 0). That turned a broken database into a plausible
187/// answer: "you have no folders" instead of an error. One contract now.
188pub type DbResult<T> = Result<T, rusqlite::Error>;
189
190#[derive(Clone)]
191pub struct Db(Arc<tokio::sync::Mutex<Connection>>);
192
193impl std::fmt::Debug for Db {
194 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
195 f.debug_struct("Db").finish()
196 }
197}
198
199impl Db {
200 pub async fn open(path: &Path) -> anyhow::Result<Self> {
201 if let Some(parent) = path.parent()
202 && !parent.as_os_str().is_empty()
203 {
204 std::fs::create_dir_all(parent)?;
205 }
206 let conn = Connection::open(path)?;
207 conn.pragma_update(None, "journal_mode", "WAL")?;
208 // WAL plus NORMAL: fsync only at checkpoints. A crash can lose the
209 // last commits, never the database file.
210 conn.pragma_update(None, "synchronous", "NORMAL")?;
211 conn.pragma_update(None, "foreign_keys", "ON")?;
212 conn.pragma_update(None, "busy_timeout", "5000")?;
213 Self::migrate(&conn)?;
214 Ok(Self(Arc::new(tokio::sync::Mutex::new(conn))))
215 }
216
217 /// Open a fresh in-memory database (used by tests — no temp file needed).
218 pub async fn open_in_memory() -> anyhow::Result<Self> {
219 let conn = Connection::open_in_memory()?;
220 conn.pragma_update(None, "foreign_keys", "ON")?;
221 conn.pragma_update(None, "busy_timeout", "5000")?;
222 Self::migrate(&conn)?;
223 Ok(Self(Arc::new(tokio::sync::Mutex::new(conn))))
224 }
225
226 fn migrate(conn: &Connection) -> rusqlite::Result<()> {
227 conn.execute(
228 "CREATE TABLE IF NOT EXISTS meta (key TEXT PRIMARY KEY, value TEXT NOT NULL)",
229 [],
230 )?;
231 let version: i64 = conn
232 .query_row(
233 "SELECT value FROM meta WHERE key = 'schema_version'",
234 [],
235 |r| r.get::<_, String>(0),
236 )
237 .optional()?
238 .and_then(|v| v.parse().ok())
239 .unwrap_or(0);
240
241 if version < 1 {
242 conn.execute_batch(SCHEMA_V1)?;
243 }
244 if version < 2 {
245 // User management (M7): a disabled flag so admins can suspend
246 // accounts without deleting them.
247 conn.execute_batch("ALTER TABLE users ADD COLUMN active INTEGER NOT NULL DEFAULT 1")?;
248 }
249 if version < 3 {
250 // Per-user profile settings: click-to-open mode. No migration
251 // from the old behaviour — everyone starts on the new default
252 // (off: single click selects, double click opens).
253 conn.execute_batch(
254 "ALTER TABLE users ADD COLUMN single_click INTEGER NOT NULL DEFAULT 0",
255 )?;
256 }
257 if version < 4 {
258 // Per-user UI language preference; NULL means "follow the
259 // browser".
260 conn.execute_batch("ALTER TABLE users ADD COLUMN language TEXT")?;
261 }
262 if version < 5 {
263 // The share list is queried by creator on every shares page.
264 conn.execute_batch(
265 "CREATE INDEX IF NOT EXISTS idx_shares_creator ON shares(creator_id)",
266 )?;
267 }
268 if version < 6 {
269 // Unlocks cascade with their share, which cascades with its
270 // creator's account.
271 conn.execute_batch(
272 "ALTER TABLE shares ADD COLUMN password_hash TEXT;
273 CREATE TABLE IF NOT EXISTS share_unlocks (
274 token TEXT PRIMARY KEY,
275 share_id INTEGER NOT NULL REFERENCES shares(id) ON DELETE CASCADE,
276 created_at TEXT NOT NULL
277 );",
278 )?;
279 }
280 if version < 7 {
281 // `delete_share` cascades into share_unlocks, which is a full
282 // scan of that table without this.
283 conn.execute_batch(
284 "CREATE INDEX IF NOT EXISTS idx_share_unlocks_share
285 ON share_unlocks(share_id)",
286 )?;
287 }
288 if version < 8 {
289 // On by default, so `--cache` is the only step needed to get
290 // thumbnails.
291 conn.execute_batch(
292 "ALTER TABLE users ADD COLUMN thumbnails INTEGER NOT NULL DEFAULT 1",
293 )?;
294 }
295 if version < 9 {
296 // Per-user default root. No foreign key on purpose: removing a
297 // root must not fail because of this column.
298 conn.execute_batch("ALTER TABLE users ADD COLUMN default_root_id INTEGER")?;
299 }
300 if version < 10 {
301 // Passkeys, and how they combine with the password.
302 //
303 // `webauthn_id` is the WebAuthn user handle: a random uuid the
304 // authenticator stores inside a discoverable credential and hands
305 // back at sign-in. It must never change once a passkey exists, or
306 // that passkey can no longer be traced to its account. Filled in
307 // lazily on the first registration, so accounts that never use a
308 // passkey keep it NULL.
309 conn.execute_batch(
310 "ALTER TABLE users ADD COLUMN auth_mode TEXT NOT NULL DEFAULT 'either';
311 ALTER TABLE users ADD COLUMN webauthn_id TEXT;
312 CREATE TABLE IF NOT EXISTS passkeys (
313 id INTEGER PRIMARY KEY AUTOINCREMENT,
314 user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
315 cred_id BLOB NOT NULL UNIQUE,
316 passkey TEXT NOT NULL,
317 name TEXT NOT NULL,
318 discoverable INTEGER,
319 created_at TEXT NOT NULL,
320 last_used_at TEXT
321 );
322 CREATE INDEX IF NOT EXISTS idx_passkeys_user ON passkeys(user_id);
323 CREATE UNIQUE INDEX IF NOT EXISTS idx_users_webauthn_id
324 ON users(webauthn_id) WHERE webauthn_id IS NOT NULL;",
325 )?;
326 }
327 if version < 11 {
328 // `secret_hash` is UNIQUE because the lookup keys on it.
329 conn.execute_batch(
330 "CREATE TABLE IF NOT EXISTS app_passwords (
331 id INTEGER PRIMARY KEY AUTOINCREMENT,
332 user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
333 name TEXT NOT NULL,
334 secret_hash TEXT NOT NULL UNIQUE,
335 created_at TEXT NOT NULL,
336 last_used_at TEXT
337 );
338 CREATE INDEX IF NOT EXISTS idx_app_passwords_user
339 ON app_passwords(user_id);",
340 )?;
341 }
342 conn.execute(
343 "INSERT OR REPLACE INTO meta (key, value) VALUES ('schema_version', ?1)",
344 [SCHEMA_VERSION.to_string()],
345 )?;
346 Ok(())
347 }
348
349 // ---------- users ----------
350
351 pub async fn user_count(&self) -> DbResult<i64> {
352 let c = self.0.lock().await;
353 let mut stmt = c.prepare_cached("SELECT COUNT(*) FROM users")?;
354 stmt.query_row([], |r| r.get(0))
355 }
356
357 /// Create the first admin account with the whole root visible (read-write).
358 ///
359 /// `None` means a user already existed. The `WHERE NOT EXISTS` guard runs
360 /// inside the same transaction as the insert, so two concurrent first-boot
361 /// setups cannot both win; a caller's earlier `user_count` check is only
362 /// an optimization, not the guarantee.
363 pub async fn create_admin(&self, name: &str, pass_hash: &str) -> DbResult<Option<User>> {
364 let mut c = self.0.lock().await;
365 let tx = c.transaction()?;
366 let inserted = tx.execute(
367 "INSERT INTO users (name, pass_hash, is_admin, created_at)
368 SELECT ?1, ?2, 1, ?3 WHERE NOT EXISTS (SELECT 1 FROM users)",
369 params![name, pass_hash, now()],
370 )?;
371 if inserted == 0 {
372 return Ok(None); // dropping `tx` rolls back
373 }
374 let user_id = tx.last_insert_rowid();
375 tx.execute(
376 "INSERT INTO user_roots (user_id, path, mode) VALUES (?1, '.', 'rw')",
377 params![user_id],
378 )?;
379 tx.commit()?;
380 Ok(Some(User {
381 id: user_id,
382 name: name.to_string(),
383 is_admin: true,
384 active: true,
385 single_click: false,
386 thumbnails: true,
387 language: None,
388 default_root_id: None,
389 auth_mode: AuthMode::Either,
390 has_password: true,
391 }))
392 }
393
394 pub async fn verify_password(&self, name: &str, password: &str) -> DbResult<Option<User>> {
395 // The guard is scoped to the query alone. Argon2 below is slow by
396 // design; holding the single connection lock across it would make one
397 // login serialize every other database access.
398 let row: Option<(User, String)> = {
399 let c = self.0.lock().await;
400 c.query_row(
401 &format!("SELECT {USER_COLS}, pass_hash FROM users WHERE name = ?1"),
402 [name],
403 |r| Ok((map_user(r)?, r.get(USER_COL_COUNT)?)),
404 )
405 .optional()?
406 };
407 // An unknown name, a disabled account and a passkey-only account all
408 // still pay for one Argon2 verify, so the response time does not tell
409 // them apart from a real account with a wrong password.
410 let (user, hash) = match row {
411 Some((u, h)) if u.active && u.has_password => (Some(u), h),
412 _ => (None, DUMMY_HASH.clone()),
413 };
414 let ok = crate::auth::verify_password_async(password, &hash).await;
415 Ok(user.filter(|_| ok))
416 }
417
418 pub async fn create_session(&self, user_id: i64, token: &str) -> DbResult<()> {
419 let c = self.0.lock().await;
420 c.execute(
421 "INSERT INTO sessions (token, user_id, created_at, last_seen_at)
422 VALUES (?1, ?2, ?3, ?4)",
423 params![token, user_id, now(), now()],
424 )?;
425 Ok(())
426 }
427
428 pub async fn delete_session(&self, token: &str) -> DbResult<()> {
429 let c = self.0.lock().await;
430 c.execute("DELETE FROM sessions WHERE token = ?1", [token])?;
431 Ok(())
432 }
433
434 /// The session's user plus that user's roots, in one round trip. Every
435 /// authenticated request needs both, so they are not two queries.
436 pub async fn session_user_with_roots(
437 &self,
438 token: &str,
439 ) -> DbResult<Option<(User, Vec<RootRow>)>> {
440 let c = self.0.lock().await;
441 let mut stmt = c.prepare_cached(&format!(
442 "SELECT {USER_COLS_U}, r.id, r.path, r.mode
443 FROM sessions s
444 JOIN users u ON u.id = s.user_id
445 LEFT JOIN user_roots r ON r.user_id = u.id
446 WHERE s.token = ?1 AND u.active = 1
447 ORDER BY r.id",
448 ))?;
449 // One row per root; a user without roots still returns one row, with
450 // the root columns NULL.
451 let mut user: Option<User> = None;
452 let mut roots: Vec<RootRow> = Vec::new();
453 let mut rows = stmt.query([token])?;
454 while let Some(r) = rows.next()? {
455 if user.is_none() {
456 user = Some(map_user(r)?);
457 }
458 if let Some(id) = r.get::<_, Option<i64>>(USER_COL_COUNT)? {
459 roots.push(RootRow {
460 id,
461 path: r.get(USER_COL_COUNT + 1)?,
462 mode: r.get::<_, SqlMode>(USER_COL_COUNT + 2)?.0,
463 });
464 }
465 }
466 Ok(user.map(|u| (u, roots)))
467 }
468
469 // ---------- roots ----------
470
471 pub async fn user_roots(&self, user_id: i64) -> DbResult<Vec<RootRow>> {
472 let c = self.0.lock().await;
473 let mut stmt = c.prepare_cached(
474 "SELECT id, path, mode FROM user_roots WHERE user_id = ?1 ORDER BY id",
475 )?;
476 let rows = stmt.query_map([user_id], |r| {
477 Ok(RootRow {
478 id: r.get(0)?,
479 path: r.get(1)?,
480 mode: r.get::<_, SqlMode>(2)?.0,
481 })
482 })?;
483 rows.collect()
484 }
485
486 // ---------- admin: user management (M7) ----------
487
488 /// Every user with their roots, in one query. The admin user list needs
489 /// both, and a per-user roots query would be one round trip per user.
490 pub async fn all_users_with_roots(&self) -> DbResult<Vec<(User, Vec<RootRow>)>> {
491 let c = self.0.lock().await;
492 let mut stmt = c.prepare_cached(&format!(
493 "SELECT {USER_COLS_U}, r.id, r.path, r.mode
494 FROM users u
495 LEFT JOIN user_roots r ON r.user_id = u.id
496 ORDER BY u.id, r.id",
497 ))?;
498 // Rows arrive grouped by user, so a new user id starts a new group.
499 let mut out: Vec<(User, Vec<RootRow>)> = Vec::new();
500 let mut rows = stmt.query([])?;
501 while let Some(r) = rows.next()? {
502 let uid: i64 = r.get(0)?;
503 if out.last().is_none_or(|(u, _)| u.id != uid) {
504 out.push((map_user(r)?, Vec::new()));
505 }
506 if let Some(id) = r.get::<_, Option<i64>>(USER_COL_COUNT)? {
507 out.last_mut().expect("pushed above").1.push(RootRow {
508 id,
509 path: r.get(USER_COL_COUNT + 1)?,
510 mode: r.get::<_, SqlMode>(USER_COL_COUNT + 2)?.0,
511 });
512 }
513 }
514 Ok(out)
515 }
516
517 pub async fn find_user_by_id(&self, id: i64) -> DbResult<Option<User>> {
518 let c = self.0.lock().await;
519 c.query_row(
520 &format!("SELECT {USER_COLS} FROM users WHERE id = ?1"),
521 [id],
522 map_user,
523 )
524 .optional()
525 }
526
527 pub async fn find_user_by_name(&self, name: &str) -> DbResult<Option<User>> {
528 let c = self.0.lock().await;
529 c.query_row(
530 &format!("SELECT {USER_COLS} FROM users WHERE name = ?1"),
531 [name],
532 map_user,
533 )
534 .optional()
535 }
536
537 pub async fn count_admins(&self) -> DbResult<i64> {
538 let c = self.0.lock().await;
539 c.query_row(
540 "SELECT COUNT(*) FROM users WHERE is_admin = 1 AND active = 1",
541 [],
542 |r| r.get(0),
543 )
544 }
545
546 /// Create a user with the given roots (path, mode) pairs.
547 pub async fn create_user(
548 &self,
549 name: &str,
550 pass_hash: &str,
551 is_admin: bool,
552 roots: &[(String, Mode)],
553 ) -> DbResult<User> {
554 let mut c = self.0.lock().await;
555 let tx = c.transaction()?;
556 tx.execute(
557 "INSERT INTO users (name, pass_hash, is_admin, active, created_at)
558 VALUES (?1, ?2, ?3, 1, ?4)",
559 params![name, pass_hash, is_admin as i64, now()],
560 )?;
561 let user_id = tx.last_insert_rowid();
562 for (path, mode) in roots {
563 tx.execute(
564 "INSERT INTO user_roots (user_id, path, mode) VALUES (?1, ?2, ?3)",
565 params![user_id, path, SqlMode(*mode)],
566 )?;
567 }
568 tx.commit()?;
569 Ok(User {
570 id: user_id,
571 name: name.to_string(),
572 is_admin,
573 active: true,
574 single_click: false,
575 thumbnails: true,
576 language: None,
577 default_root_id: None,
578 auth_mode: AuthMode::Either,
579 has_password: true,
580 })
581 }
582
583 pub async fn set_user_single_click(&self, id: i64, single_click: bool) -> DbResult<()> {
584 let c = self.0.lock().await;
585 c.execute(
586 "UPDATE users SET single_click = ?1 WHERE id = ?2",
587 params![single_click as i64, id],
588 )?;
589 Ok(())
590 }
591
592 pub async fn set_user_thumbnails(&self, id: i64, thumbnails: bool) -> DbResult<()> {
593 let c = self.0.lock().await;
594 c.execute(
595 "UPDATE users SET thumbnails = ?1 WHERE id = ?2",
596 params![thumbnails as i64, id],
597 )?;
598 Ok(())
599 }
600
601 pub async fn set_user_default_root(&self, id: i64, root_id: Option<i64>) -> DbResult<()> {
602 let c = self.0.lock().await;
603 c.execute(
604 "UPDATE users SET default_root_id = ?1 WHERE id = ?2",
605 params![root_id, id],
606 )?;
607 Ok(())
608 }
609
610 pub async fn set_user_language(&self, id: i64, language: Option<&str>) -> DbResult<()> {
611 let c = self.0.lock().await;
612 c.execute(
613 "UPDATE users SET language = ?1 WHERE id = ?2",
614 params![language, id],
615 )?;
616 Ok(())
617 }
618
619 /// Apply an admin edit atomically: every `Some` field is written in one
620 /// transaction, so a failure midway leaves the user unchanged.
621 pub async fn update_user(
622 &self,
623 id: i64,
624 pass_hash: Option<&str>,
625 is_admin: Option<bool>,
626 active: Option<bool>,
627 roots: Option<&[(String, Mode)]>,
628 ) -> DbResult<()> {
629 let mut c = self.0.lock().await;
630 let tx = c.transaction()?;
631 // An admin sets a password to get someone back into a locked-out
632 // account, so every other way in goes with it: the passkeys, the app
633 // passwords, the requirement to use one. Always rather than on
634 // request, so a reset cannot leave a credential the user no longer
635 // has.
636 if let Some(h) = pass_hash {
637 set_password(&tx, id, h)?;
638 tx.execute("DELETE FROM passkeys WHERE user_id = ?1", [id])?;
639 tx.execute("DELETE FROM app_passwords WHERE user_id = ?1", [id])?;
640 tx.execute(
641 "UPDATE users SET auth_mode = ?1 WHERE id = ?2",
642 params![SqlAuthMode(AuthMode::Either), id],
643 )?;
644 }
645 if let Some(a) = is_admin {
646 tx.execute(
647 "UPDATE users SET is_admin = ?1 WHERE id = ?2",
648 params![a as i64, id],
649 )?;
650 }
651 if let Some(a) = active {
652 tx.execute(
653 "UPDATE users SET active = ?1 WHERE id = ?2",
654 params![a as i64, id],
655 )?;
656 }
657 if let Some(roots) = roots {
658 tx.execute("DELETE FROM user_roots WHERE user_id = ?1", [id])?;
659 for (path, mode) in roots {
660 tx.execute(
661 "INSERT INTO user_roots (user_id, path, mode) VALUES (?1, ?2, ?3)",
662 params![id, path, SqlMode(*mode)],
663 )?;
664 }
665 }
666 tx.commit()
667 }
668
669 // ---------- credentials: password, passkeys, sign-in mode ----------
670
671 /// Set the password. Leaves every session alone, so the caller must pair
672 /// this with [`Db::delete_other_sessions`] — see
673 /// [`crate::api::passkeys`]'s `invalidate_elsewhere`. The admin path uses
674 /// [`set_password`] instead, which ends every session including its own.
675 ///
676 /// Only for setting a real one. Clearing it is [`Db::clear_user_password`],
677 /// which has a rule to keep.
678 pub async fn set_password_keeping_sessions(&self, id: i64, pass_hash: &str) -> DbResult<()> {
679 let c = self.0.lock().await;
680 c.execute(
681 "UPDATE users SET pass_hash = ?1 WHERE id = ?2",
682 params![pass_hash, id],
683 )?;
684 Ok(())
685 }
686
687 /// Leave the account on its passkeys alone. `false` means that would have
688 /// locked it out, so nothing changed.
689 pub async fn clear_user_password(&self, id: i64) -> DbResult<bool> {
690 let mut c = self.0.lock().await;
691 let tx = c.transaction()?;
692 tx.execute(
693 "UPDATE users SET pass_hash = ?1 WHERE id = ?2",
694 params![NO_PASSWORD, id],
695 )?;
696 commit_if_reachable(tx, id)
697 }
698
699 /// `false` means the account does not satisfy the new mode, so it stands.
700 pub async fn set_user_auth_mode(&self, id: i64, mode: AuthMode) -> DbResult<bool> {
701 let mut c = self.0.lock().await;
702 let tx = c.transaction()?;
703 tx.execute(
704 "UPDATE users SET auth_mode = ?1 WHERE id = ?2",
705 params![SqlAuthMode(mode), id],
706 )?;
707 commit_if_reachable(tx, id)
708 }
709
710 /// Drop every session of this user except `keep`.
711 ///
712 /// Called after any credential change. Otherwise a session stolen before
713 /// the change keeps working for its full 30 days.
714 pub async fn delete_other_sessions(&self, user_id: i64, keep: &str) -> DbResult<()> {
715 let c = self.0.lock().await;
716 c.execute(
717 "DELETE FROM sessions WHERE user_id = ?1 AND token != ?2",
718 params![user_id, keep],
719 )?;
720 Ok(())
721 }
722
723 /// This account's WebAuthn user handle, creating it on first use.
724 ///
725 /// Stable for the lifetime of the account: a discoverable passkey stores
726 /// this value and hands it back at sign-in, so changing it would orphan
727 /// every existing passkey.
728 pub async fn user_webauthn_id(&self, id: i64) -> DbResult<uuid::Uuid> {
729 let c = self.0.lock().await;
730 let existing: Option<String> = c
731 .query_row("SELECT webauthn_id FROM users WHERE id = ?1", [id], |r| {
732 r.get(0)
733 })
734 .optional()?
735 .flatten();
736 if let Some(parsed) = existing
737 .as_deref()
738 .and_then(|s| uuid::Uuid::parse_str(s).ok())
739 {
740 return Ok(parsed);
741 }
742 let fresh = uuid::Uuid::new_v4();
743 c.execute(
744 "UPDATE users SET webauthn_id = ?1 WHERE id = ?2",
745 params![fresh.to_string(), id],
746 )?;
747 Ok(fresh)
748 }
749
750 /// The account a discoverable credential's user handle points at.
751 pub async fn find_user_by_webauthn_id(&self, wid: &uuid::Uuid) -> DbResult<Option<User>> {
752 let c = self.0.lock().await;
753 c.query_row(
754 &format!("SELECT {USER_COLS} FROM users WHERE webauthn_id = ?1"),
755 [wid.to_string()],
756 map_user,
757 )
758 .optional()
759 }
760
761 pub async fn user_passkeys(&self, user_id: i64) -> DbResult<Vec<PasskeyRow>> {
762 let c = self.0.lock().await;
763 let mut stmt = c.prepare_cached(
764 "SELECT id, name, created_at, last_used_at, discoverable, passkey
765 FROM passkeys WHERE user_id = ?1 ORDER BY id",
766 )?;
767 let rows = stmt.query_map([user_id], map_passkey)?;
768 rows.collect()
769 }
770
771 pub async fn count_passkeys(&self, user_id: i64) -> DbResult<i64> {
772 let c = self.0.lock().await;
773 c.query_row(
774 "SELECT COUNT(*) FROM passkeys WHERE user_id = ?1",
775 [user_id],
776 |r| r.get(0),
777 )
778 }
779
780 /// The per-install secret behind the decoy credentials a named passkey
781 /// challenge is padded with. Created on first use, so no migration.
782 pub async fn decoy_secret(&self) -> DbResult<String> {
783 let c = self.0.lock().await;
784 let existing: Option<String> = c
785 .query_row(
786 "SELECT value FROM meta WHERE key = 'decoy_secret'",
787 [],
788 |r| r.get(0),
789 )
790 .optional()?;
791 if let Some(secret) = existing {
792 return Ok(secret);
793 }
794 let fresh = crate::auth::random_token();
795 c.execute(
796 "INSERT OR REPLACE INTO meta (key, value) VALUES ('decoy_secret', ?1)",
797 [&fresh],
798 )?;
799 Ok(fresh)
800 }
801
802 /// The length in bytes of every credential id registered on this server,
803 /// one entry per credential.
804 ///
805 /// Decoys draw their length from this list. Authenticators disagree on it —
806 /// a security key emits 64 bytes, a platform key often 16 or 32 — so a
807 /// decoy of one fixed length would stand out next to the real entries.
808 ///
809 /// Duplicates are kept deliberately. Drawing from the distinct lengths
810 /// would make a rare length as likely as a common one, and decoys that do
811 /// not match how the install actually looks are the thing worth avoiding.
812 pub async fn cred_id_lengths(&self) -> DbResult<Vec<usize>> {
813 let c = self.0.lock().await;
814 let mut stmt = c.prepare("SELECT length(cred_id) FROM passkeys ORDER BY id")?;
815 let rows = stmt.query_map([], |r| r.get::<_, i64>(0))?;
816 rows.map(|r| r.map(|n| n.max(1) as usize))
817 .collect::<Result<Vec<_>, _>>()
818 }
819
820 /// Store a freshly registered passkey. A duplicate `cred_id` is a unique
821 /// violation, which is the intended answer: the same credential must not
822 /// be registered twice, not even to a second account.
823 ///
824 /// `None` means the account is already at [`PASSKEY_LIMIT`]. The count and
825 /// the insert share one transaction, so two registrations landing together
826 /// cannot put the account one over and give its sign-in challenge a
827 /// telltale length.
828 pub async fn add_passkey(
829 &self,
830 user_id: i64,
831 cred_id: &[u8],
832 passkey: &str,
833 name: &str,
834 discoverable: Option<bool>,
835 ) -> DbResult<Option<PasskeyRow>> {
836 let mut c = self.0.lock().await;
837 let tx = c.transaction()?;
838 let held: i64 = tx.query_row(
839 "SELECT COUNT(*) FROM passkeys WHERE user_id = ?1",
840 [user_id],
841 |r| r.get(0),
842 )?;
843 if held as usize >= PASSKEY_LIMIT {
844 return Ok(None);
845 }
846 tx.execute(
847 "INSERT INTO passkeys (user_id, cred_id, passkey, name, discoverable, created_at)
848 VALUES (?1, ?2, ?3, ?4, ?5, ?6)",
849 params![user_id, cred_id, passkey, name, discoverable, now()],
850 )?;
851 let id = tx.last_insert_rowid();
852 let row = tx.query_row(
853 "SELECT id, name, created_at, last_used_at, discoverable, passkey
854 FROM passkeys WHERE id = ?1",
855 [id],
856 map_passkey,
857 )?;
858 tx.commit()?;
859 Ok(Some(row))
860 }
861
862 pub async fn delete_passkey(&self, id: i64, user_id: i64) -> DbResult<PasskeyDeleted> {
863 let mut c = self.0.lock().await;
864 let tx = c.transaction()?;
865 let hit = tx.execute(
866 "DELETE FROM passkeys WHERE id = ?1 AND user_id = ?2",
867 params![id, user_id],
868 )? > 0;
869 if !hit {
870 tx.rollback()?;
871 return Ok(PasskeyDeleted::NotFound);
872 }
873 Ok(if commit_if_reachable(tx, user_id)? {
874 PasskeyDeleted::Gone
875 } else {
876 PasskeyDeleted::LastCredential
877 })
878 }
879
880 /// Record a successful assertion: the re-serialized credential (its
881 /// signature counter and backup flags may have moved) and the time.
882 pub async fn passkey_used(&self, id: i64, passkey: &str) -> DbResult<()> {
883 let c = self.0.lock().await;
884 c.execute(
885 "UPDATE passkeys SET passkey = ?1, last_used_at = ?2 WHERE id = ?3",
886 params![passkey, now(), id],
887 )?;
888 Ok(())
889 }
890
891 // ---------- app passwords (WebDAV) ----------
892
893 pub async fn app_passwords(&self, user_id: i64) -> DbResult<Vec<AppPasswordRow>> {
894 let c = self.0.lock().await;
895 let mut stmt = c.prepare_cached(
896 "SELECT id, name, created_at, last_used_at
897 FROM app_passwords WHERE user_id = ?1 ORDER BY id",
898 )?;
899 let rows = stmt.query_map([user_id], map_app_password)?;
900 rows.collect()
901 }
902
903 /// `None` means the account is already at [`APP_PASSWORD_LIMIT`]. Count
904 /// and insert share one transaction, so the cap cannot be raced.
905 pub async fn add_app_password(
906 &self,
907 user_id: i64,
908 name: &str,
909 secret_hash: &str,
910 ) -> DbResult<Option<AppPasswordRow>> {
911 let mut c = self.0.lock().await;
912 let tx = c.transaction()?;
913 let held: i64 = tx.query_row(
914 "SELECT COUNT(*) FROM app_passwords WHERE user_id = ?1",
915 [user_id],
916 |r| r.get(0),
917 )?;
918 if held as usize >= APP_PASSWORD_LIMIT {
919 return Ok(None);
920 }
921 tx.execute(
922 "INSERT INTO app_passwords (user_id, name, secret_hash, created_at)
923 VALUES (?1, ?2, ?3, ?4)",
924 params![user_id, name, secret_hash, now()],
925 )?;
926 let row = tx.query_row(
927 "SELECT id, name, created_at, last_used_at FROM app_passwords WHERE id = ?1",
928 [tx.last_insert_rowid()],
929 map_app_password,
930 )?;
931 tx.commit()?;
932 Ok(Some(row))
933 }
934
935 /// `false` means no row matched: an unknown id, or someone else's.
936 ///
937 /// No reachability check, unlike [`Db::delete_passkey`]: an app password
938 /// never signs in to the web UI.
939 pub async fn delete_app_password(&self, id: i64, user_id: i64) -> DbResult<bool> {
940 let c = self.0.lock().await;
941 Ok(c.execute(
942 "DELETE FROM app_passwords WHERE id = ?1 AND user_id = ?2",
943 params![id, user_id],
944 )? > 0)
945 }
946
947 /// The account an app password opens. Stamps `last_used_at` on the way.
948 ///
949 /// Inactive accounts are excluded here, because nothing downstream in the
950 /// WebDAV path looks at the flag.
951 pub async fn user_by_app_password(&self, secret_hash: &str) -> DbResult<Option<User>> {
952 let c = self.0.lock().await;
953 let user = c
954 .query_row(
955 &format!(
956 "SELECT {USER_COLS_U}
957 FROM app_passwords a
958 JOIN users u ON u.id = a.user_id
959 WHERE a.secret_hash = ?1 AND u.active = 1"
960 ),
961 [secret_hash],
962 map_user,
963 )
964 .optional()?;
965 if user.is_some() {
966 // A mount re-sends its credential on every request. An hour's
967 // resolution keeps that from writing the stamp on each one.
968 c.execute(
969 "UPDATE app_passwords SET last_used_at = ?1
970 WHERE secret_hash = ?2 AND (last_used_at IS NULL OR last_used_at < ?3)",
971 params![now(), secret_hash, an_hour_ago()],
972 )?;
973 }
974 Ok(user)
975 }
976
977 /// Delete a user. `false` means no row matched.
978 pub async fn delete_user(&self, id: i64) -> DbResult<bool> {
979 let c = self.0.lock().await;
980 Ok(c.execute("DELETE FROM users WHERE id = ?1", [id])? > 0)
981 }
982
983 // ---------- shares ----------
984
985 #[allow(clippy::too_many_arguments)] // one row's columns, all required
986 pub async fn create_share(
987 &self,
988 creator_id: i64,
989 token: &str,
990 target: &str,
991 is_file: bool,
992 mode: Mode,
993 expires_at: Option<&str>,
994 password_hash: Option<&str>,
995 ) -> DbResult<ShareRow> {
996 let c = self.0.lock().await;
997 c.execute(
998 "INSERT INTO shares
999 (token, creator_id, target, is_file, mode, created_at, expires_at, password_hash)
1000 VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)",
1001 params![
1002 token,
1003 creator_id,
1004 target,
1005 is_file as i64,
1006 SqlMode(mode),
1007 now(),
1008 expires_at,
1009 password_hash
1010 ],
1011 )?;
1012 let id = c.last_insert_rowid();
1013 Ok(ShareRow {
1014 id,
1015 token: token.to_string(),
1016 creator_id,
1017 target: target.to_string(),
1018 is_file,
1019 mode,
1020 created_at: now(),
1021 expires_at: expires_at.map(|s| s.to_string()),
1022 password_hash: password_hash.map(|s| s.to_string()),
1023 })
1024 }
1025
1026 /// Record that a visitor entered `share_id`'s password, and return the
1027 /// token that proves it (the value of their unlock cookie).
1028 pub async fn create_share_unlock(&self, share_id: i64) -> DbResult<String> {
1029 let token = crate::auth::random_token();
1030 let c = self.0.lock().await;
1031 // Old unlocks go first. The cookie carrying them is a session
1032 // cookie, so it is already gone from every browser; without this the
1033 // rows would accumulate forever, one per unlock.
1034 c.execute(
1035 "DELETE FROM share_unlocks WHERE created_at < ?1",
1036 [expiry_cutoff()],
1037 )?;
1038 c.execute(
1039 "INSERT INTO share_unlocks (token, share_id, created_at) VALUES (?1, ?2, ?3)",
1040 params![token, share_id, now()],
1041 )?;
1042 Ok(token)
1043 }
1044
1045 /// Whether `token` is a live unlock for `share_id`.
1046 ///
1047 /// The share id is part of the lookup, so an unlock for one share cannot
1048 /// open another.
1049 pub async fn share_unlock_valid(&self, token: &str, share_id: i64) -> DbResult<bool> {
1050 let c = self.0.lock().await;
1051 let mut stmt =
1052 c.prepare_cached("SELECT 1 FROM share_unlocks WHERE token = ?1 AND share_id = ?2")?;
1053 Ok(stmt
1054 .query_row(params![token, share_id], |_| Ok(()))
1055 .optional()?
1056 .is_some())
1057 }
1058
1059 pub async fn share_by_token(&self, token: &str) -> DbResult<Option<ShareRow>> {
1060 let c = self.0.lock().await;
1061 let sql = "SELECT id, token, creator_id, target, is_file, mode, created_at, expires_at,
1062 password_hash
1063 FROM shares WHERE token = ?1";
1064 let mut stmt = c.prepare_cached(sql)?;
1065 stmt.query_row([token], map_share).optional()
1066 }
1067
1068 pub async fn user_shares(&self, creator_id: i64) -> DbResult<Vec<ShareRow>> {
1069 let c = self.0.lock().await;
1070 let sql = "SELECT id, token, creator_id, target, is_file, mode, created_at, expires_at,
1071 password_hash
1072 FROM shares WHERE creator_id = ?1 ORDER BY id DESC";
1073 let mut stmt = c.prepare_cached(sql)?;
1074 let rows = stmt.query_map([creator_id], map_share)?;
1075 rows.collect()
1076 }
1077
1078 /// Revoke every share on `target` or on anything beneath it. Returns how
1079 /// many were dropped.
1080 ///
1081 /// Called when a path stops meaning what it meant: the item was deleted,
1082 /// renamed, or moved away. A share names a path, and a path is not a
1083 /// stable identity, so leaving the row behind would let a *new* item that
1084 /// later takes the freed path inherit the old link's audience.
1085 ///
1086 /// `substr` rather than `LIKE`: a target containing `%` or `_` would make
1087 /// a `LIKE` pattern over-match and revoke unrelated shares.
1088 pub async fn revoke_shares_at(&self, target: &str) -> DbResult<usize> {
1089 let c = self.0.lock().await;
1090 c.execute(
1091 "DELETE FROM shares
1092 WHERE target = ?1 OR substr(target, 1, length(?1) + 1) = ?1 || '/'",
1093 [target],
1094 )
1095 }
1096
1097 /// Delete one of `creator_id`'s shares. `false` means no row matched.
1098 pub async fn delete_share(&self, id: i64, creator_id: i64) -> DbResult<bool> {
1099 let c = self.0.lock().await;
1100 let n = c.execute(
1101 "DELETE FROM shares WHERE id = ?1 AND creator_id = ?2",
1102 params![id, creator_id],
1103 )?;
1104 Ok(n > 0)
1105 }
1106
1107 /// Every share on the server with its creator. Grouped by account name,
1108 /// newest link within an account first.
1109 ///
1110 /// The join cannot miss: `shares.creator_id` cascades on delete, so a share
1111 /// never outlives the account that made it.
1112 pub async fn all_shares_with_creators(&self) -> DbResult<Vec<ShareWithCreator>> {
1113 let c = self.0.lock().await;
1114 // Columns 0..8 are `map_share`'s order, unchanged from `user_shares`.
1115 let sql = "SELECT s.id, s.token, s.creator_id, s.target, s.is_file, s.mode,
1116 s.created_at, s.expires_at, s.password_hash,
1117 u.name, u.active != 0
1118 FROM shares s
1119 JOIN users u ON u.id = s.creator_id
1120 ORDER BY u.name COLLATE NOCASE, s.id DESC";
1121 let mut stmt = c.prepare_cached(sql)?;
1122 let rows = stmt.query_map([], |r| {
1123 Ok(ShareWithCreator {
1124 share: map_share(r)?,
1125 creator_name: r.get(9)?,
1126 creator_active: r.get(10)?,
1127 })
1128 })?;
1129 rows.collect()
1130 }
1131
1132 /// Revoke a share whoever created it. The owner-scoped
1133 /// [`Self::delete_share`] is what the user-facing API uses.
1134 pub async fn admin_delete_share(&self, id: i64) -> DbResult<bool> {
1135 let c = self.0.lock().await;
1136 Ok(c.execute("DELETE FROM shares WHERE id = ?1", [id])? > 0)
1137 }
1138
1139 // ---------- settings ----------
1140
1141 /// Folders excluded from search, as paths relative to the server root.
1142 ///
1143 /// Stored as one JSON array in a settings row. A table of its own would
1144 /// be overkill for a hand-edited list read once per search.
1145 pub async fn search_excludes(&self) -> DbResult<Vec<String>> {
1146 let raw = self.get_setting("search_excludes").await?;
1147 // Normalised on read as well as on write. A value edited straight
1148 // into the database would otherwise never match: `is_excluded`
1149 // compares against paths with no slash at either end.
1150 let clean = |v: Vec<String>| -> Vec<String> {
1151 v.into_iter()
1152 .map(|p| p.trim().replace('\\', "/").trim_matches('/').to_string())
1153 .filter(|p| !p.is_empty() && p != ".")
1154 .collect()
1155 };
1156 // A hand-edited, unparseable value falls back to no exclusions,
1157 // the same as an absent row.
1158 Ok(raw
1159 .as_deref()
1160 .and_then(|v| serde_json::from_str::<Vec<String>>(v).ok())
1161 .map(clean)
1162 .unwrap_or_default())
1163 }
1164
1165 pub async fn set_search_excludes(&self, paths: &[String]) -> DbResult<()> {
1166 let json = serde_json::to_string(paths).unwrap_or_else(|_| "[]".to_string());
1167 self.set_setting("search_excludes", &json).await
1168 }
1169
1170 pub async fn get_setting(&self, key: &str) -> DbResult<Option<String>> {
1171 let c = self.0.lock().await;
1172 let mut stmt = c.prepare_cached("SELECT value FROM settings WHERE key = ?1")?;
1173 stmt.query_row([key], |r| r.get(0)).optional()
1174 }
1175
1176 pub async fn set_setting(&self, key: &str, value: &str) -> DbResult<()> {
1177 let c = self.0.lock().await;
1178 c.execute(
1179 "INSERT INTO settings (key, value) VALUES (?1, ?2)
1180 ON CONFLICT(key) DO UPDATE SET value = ?2",
1181 params![key, value],
1182 )?;
1183 Ok(())
1184 }
1185
1186 /// Whether users may create writable (read-write) shares. Off by default;
1187 /// the admin setting gates it.
1188 pub async fn allow_writable_shares(&self) -> DbResult<bool> {
1189 Ok(self.get_setting("allow_writable_shares").await?.as_deref() == Some("1"))
1190 }
1191
1192 pub async fn set_allow_writable_shares(&self, v: bool) -> DbResult<()> {
1193 self.set_setting("allow_writable_shares", if v { "1" } else { "0" })
1194 .await
1195 }
1196}
1197
1198/// Write a new password hash and drop every session that was opened with the
1199/// old one.
1200///
1201/// The two belong together: a password is changed because the old one is
1202/// suspect (an admin resetting a compromised account), and a session that
1203/// survives the reset leaves whoever holds it signed in. Takes the
1204/// transaction so the caller can bundle it with its other edits.
1205fn set_password(tx: &rusqlite::Transaction<'_>, id: i64, pass_hash: &str) -> DbResult<()> {
1206 tx.execute(
1207 "UPDATE users SET pass_hash = ?1 WHERE id = ?2",
1208 params![pass_hash, id],
1209 )?;
1210 tx.execute("DELETE FROM sessions WHERE user_id = ?1", [id])?;
1211 Ok(())
1212}
1213
1214/// Commit a credential change, or undo it if the account would be left with
1215/// no way to sign in.
1216///
1217/// The handlers check the same rules first, so the person gets a message that
1218/// says which rule. This is the guarantee behind those checks: a count read
1219/// before the write can already be stale when the write lands, and two
1220/// requests racing could otherwise take away the last credential between them.
1221/// `false` means the change was rolled back.
1222fn commit_if_reachable(tx: rusqlite::Transaction<'_>, user_id: i64) -> DbResult<bool> {
1223 let (has_password, mode) = tx.query_row(
1224 "SELECT pass_hash != '', auth_mode FROM users WHERE id = ?1",
1225 [user_id],
1226 |r| Ok((r.get::<_, bool>(0)?, r.get::<_, SqlAuthMode>(1)?)),
1227 )?;
1228 let passkeys: i64 = tx.query_row(
1229 "SELECT COUNT(*) FROM passkeys WHERE user_id = ?1",
1230 [user_id],
1231 |r| r.get(0),
1232 )?;
1233 let reachable = match mode.0 {
1234 AuthMode::Either => has_password || passkeys > 0,
1235 AuthMode::Both => has_password && passkeys > 0,
1236 };
1237 if !reachable {
1238 tx.rollback()?;
1239 return Ok(false);
1240 }
1241 tx.commit()?;
1242 Ok(true)
1243}
1244
1245/// Column order matched by [`USER_COLS`] and [`USER_COLS_U`].
1246fn map_user(r: &rusqlite::Row) -> DbResult<User> {
1247 Ok(User {
1248 id: r.get(0)?,
1249 name: r.get(1)?,
1250 is_admin: r.get(2)?,
1251 active: r.get(3)?,
1252 single_click: r.get(4)?,
1253 thumbnails: r.get(5)?,
1254 language: r.get(6)?,
1255 default_root_id: r.get(7)?,
1256 auth_mode: r.get::<_, SqlAuthMode>(8)?.0,
1257 has_password: r.get(9)?,
1258 })
1259}
1260
1261/// Column order matched by the `passkeys` SELECTs above.
1262fn map_passkey(r: &rusqlite::Row) -> DbResult<PasskeyRow> {
1263 Ok(PasskeyRow {
1264 id: r.get(0)?,
1265 name: r.get(1)?,
1266 created_at: r.get(2)?,
1267 last_used_at: r.get(3)?,
1268 discoverable: r.get(4)?,
1269 passkey: r.get(5)?,
1270 })
1271}
1272
1273/// Column order matched by the `app_passwords` SELECTs above.
1274fn map_app_password(r: &rusqlite::Row) -> DbResult<AppPasswordRow> {
1275 Ok(AppPasswordRow {
1276 id: r.get(0)?,
1277 name: r.get(1)?,
1278 created_at: r.get(2)?,
1279 last_used_at: r.get(3)?,
1280 })
1281}
1282
1283/// Column order matched by the two `shares` SELECTs above.
1284fn map_share(r: &rusqlite::Row) -> DbResult<ShareRow> {
1285 Ok(ShareRow {
1286 id: r.get(0)?,
1287 token: r.get(1)?,
1288 creator_id: r.get(2)?,
1289 target: r.get(3)?,
1290 is_file: r.get::<_, i64>(4)? != 0,
1291 mode: r.get::<_, SqlMode>(5)?.0,
1292 created_at: r.get(6)?,
1293 expires_at: r.get(7)?,
1294 password_hash: r.get(8)?,
1295 })
1296}
1297
1298/// A hash of a random string nobody knows. Verified against when the login
1299/// name does not exist, so both paths cost one Argon2 run.
1300static DUMMY_HASH: std::sync::LazyLock<String> = std::sync::LazyLock::new(|| {
1301 crate::auth::hash_password(&crate::auth::random_token()).expect("argon2 hash")
1302});
1303
1304/// How long an unlock row outlives its cookie. The cookie dies with the
1305/// browser, so this only bounds the rows left behind by closed sessions.
1306const UNLOCK_MAX_AGE_DAYS: i64 = 7;
1307
1308/// The timestamp an unlock row must be newer than to survive a cleanup.
1309fn expiry_cutoff() -> String {
1310 (chrono::Utc::now() - chrono::Duration::days(UNLOCK_MAX_AGE_DAYS))
1311 .to_rfc3339_opts(chrono::SecondsFormat::Secs, true)
1312}
1313
1314/// An hour back, in the same format as [`now`]. The format sorts
1315/// lexicographically, so SQL can compare the two as text.
1316fn an_hour_ago() -> String {
1317 (chrono::Utc::now() - chrono::Duration::hours(1))
1318 .to_rfc3339_opts(chrono::SecondsFormat::Secs, true)
1319}
1320
1321fn now() -> String {
1322 chrono::Utc::now().to_rfc3339_opts(chrono::SecondsFormat::Secs, true)
1323}
1324
1325const SCHEMA_V1: &str = r#"
1326CREATE TABLE IF NOT EXISTS users (
1327 id INTEGER PRIMARY KEY AUTOINCREMENT,
1328 name TEXT NOT NULL UNIQUE COLLATE NOCASE,
1329 pass_hash TEXT NOT NULL,
1330 is_admin INTEGER NOT NULL DEFAULT 0,
1331 created_at TEXT NOT NULL
1332);
1333
1334CREATE TABLE IF NOT EXISTS user_roots (
1335 id INTEGER PRIMARY KEY AUTOINCREMENT,
1336 user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
1337 path TEXT NOT NULL,
1338 mode TEXT NOT NULL CHECK (mode IN ('rw','ro')),
1339 UNIQUE (user_id, path)
1340);
1341
1342CREATE TABLE IF NOT EXISTS shares (
1343 id INTEGER PRIMARY KEY AUTOINCREMENT,
1344 token TEXT NOT NULL UNIQUE,
1345 creator_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
1346 target TEXT NOT NULL,
1347 is_file INTEGER NOT NULL,
1348 mode TEXT NOT NULL CHECK (mode IN ('rw','ro')),
1349 created_at TEXT NOT NULL,
1350 expires_at TEXT
1351);
1352
1353CREATE TABLE IF NOT EXISTS sessions (
1354 token TEXT PRIMARY KEY,
1355 user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
1356 created_at TEXT NOT NULL,
1357 last_seen_at TEXT NOT NULL
1358);
1359
1360CREATE TABLE IF NOT EXISTS settings (
1361 key TEXT PRIMARY KEY,
1362 value TEXT NOT NULL
1363);
1364INSERT OR IGNORE INTO settings (key, value) VALUES ('allow_writable_shares', '0');
1365"#;
1366
1367#[cfg(test)]
1368mod tests {
1369 use super::*;
1370
1371 // Most tests use an in-memory DB (the file-based path is still covered
1372 // by `v1_db_migrates_to_v2` and the integration harness' `Db::open`).
1373 async fn mem() -> Db {
1374 Db::open_in_memory().await.unwrap()
1375 }
1376
1377 /// `update_user` is the only way production edits these fields, so the
1378 /// tests exercise that path rather than per-field helpers.
1379 async fn edit(db: &Db, id: i64, pass: Option<&str>, admin: Option<bool>, active: Option<bool>) {
1380 db.update_user(id, pass, admin, active, None).await.unwrap();
1381 }
1382
1383 /// A timestamp `d` in the past, in the format the stamps use.
1384 fn ago(d: chrono::Duration) -> String {
1385 (chrono::Utc::now() - d).to_rfc3339_opts(chrono::SecondsFormat::Secs, true)
1386 }
1387
1388 /// Backdate a stamp, which production code has no reason to do.
1389 async fn set_last_used(db: &Db, secret_hash: &str, at: &str) {
1390 db.0.lock()
1391 .await
1392 .execute(
1393 "UPDATE app_passwords SET last_used_at = ?1 WHERE secret_hash = ?2",
1394 params![at, secret_hash],
1395 )
1396 .unwrap();
1397 }
1398
1399 async fn db_with_admin() -> (Db, User) {
1400 let db = mem().await;
1401 let hash = crate::auth::hash_password("admin1234").unwrap();
1402 let admin = db.create_admin("admin", &hash).await.unwrap().unwrap();
1403 (db, admin)
1404 }
1405
1406 #[tokio::test]
1407 async fn fresh_db_state() {
1408 let db = mem().await;
1409 assert_eq!(db.user_count().await.unwrap(), 0);
1410 assert_eq!(db.count_admins().await.unwrap(), 0);
1411 assert!(!db.allow_writable_shares().await.unwrap());
1412 assert!(db.find_user_by_name("nobody").await.unwrap().is_none());
1413 assert!(db.find_user_by_id(1).await.unwrap().is_none());
1414 assert!(db.all_users_with_roots().await.unwrap().is_empty());
1415 }
1416
1417 #[tokio::test]
1418 async fn app_passwords_open_one_account_and_stamp_their_use() {
1419 let (db, admin) = db_with_admin().await;
1420 let secret = crate::auth::app_password();
1421 let hash = crate::auth::app_password_hash(&secret);
1422 let row = db
1423 .add_app_password(admin.id, "laptop", &hash)
1424 .await
1425 .unwrap()
1426 .unwrap();
1427 assert_eq!(row.name, "laptop");
1428 assert!(row.last_used_at.is_none());
1429
1430 let hit = db.user_by_app_password(&hash).await.unwrap().unwrap();
1431 assert_eq!(hit.id, admin.id);
1432 let listed = |db: &Db| {
1433 let db = db.clone();
1434 async move {
1435 db.app_passwords(admin.id).await.unwrap()[0]
1436 .last_used_at
1437 .clone()
1438 }
1439 };
1440 assert!(listed(&db).await.is_some(), "first use must stamp the row");
1441
1442 // Ten minutes back is inside the hour window, and far enough from
1443 // `now()` that a rewrite would show at second resolution.
1444 let inside = ago(chrono::Duration::minutes(10));
1445 set_last_used(&db, &hash, &inside).await;
1446 db.user_by_app_password(&hash).await.unwrap().unwrap();
1447 assert_eq!(
1448 listed(&db).await.as_deref(),
1449 Some(inside.as_str()),
1450 "a second use inside the hour wrote the stamp again"
1451 );
1452
1453 let outside = ago(chrono::Duration::hours(2));
1454 set_last_used(&db, &hash, &outside).await;
1455 db.user_by_app_password(&hash).await.unwrap().unwrap();
1456 assert_ne!(
1457 listed(&db).await.as_deref(),
1458 Some(outside.as_str()),
1459 "a use after the window left the stamp stale"
1460 );
1461
1462 // The raw secret is not the key, and an inactive account does not match.
1463 let other = crate::auth::app_password_hash(&crate::auth::app_password());
1464 assert!(db.user_by_app_password(&other).await.unwrap().is_none());
1465 assert!(db.user_by_app_password(&secret).await.unwrap().is_none());
1466 edit(&db, admin.id, None, None, Some(false)).await;
1467 assert!(db.user_by_app_password(&hash).await.unwrap().is_none());
1468 edit(&db, admin.id, None, None, Some(true)).await;
1469 assert!(db.user_by_app_password(&hash).await.unwrap().is_some());
1470
1471 for i in 1..APP_PASSWORD_LIMIT {
1472 let h = crate::auth::app_password_hash(&crate::auth::app_password());
1473 assert!(
1474 db.add_app_password(admin.id, &format!("c{i}"), &h)
1475 .await
1476 .unwrap()
1477 .is_some()
1478 );
1479 }
1480 let h = crate::auth::app_password_hash(&crate::auth::app_password());
1481 assert!(
1482 db.add_app_password(admin.id, "one-too-many", &h)
1483 .await
1484 .unwrap()
1485 .is_none()
1486 );
1487
1488 assert!(db.delete_app_password(row.id, admin.id).await.unwrap());
1489 assert!(!db.delete_app_password(row.id, admin.id).await.unwrap());
1490 assert!(db.user_by_app_password(&hash).await.unwrap().is_none());
1491 }
1492
1493 #[tokio::test]
1494 async fn an_admin_password_reset_revokes_the_app_passwords() {
1495 let (db, admin) = db_with_admin().await;
1496 let hash = crate::auth::app_password_hash(&crate::auth::app_password());
1497 db.add_app_password(admin.id, "mount", &hash)
1498 .await
1499 .unwrap()
1500 .unwrap();
1501
1502 // An edit that sets no password leaves them alone.
1503 edit(&db, admin.id, None, None, Some(true)).await;
1504 assert_eq!(db.app_passwords(admin.id).await.unwrap().len(), 1);
1505
1506 // A reset is meant to revoke every way in that existed before it.
1507 let fresh = crate::auth::hash_password("rescued12").unwrap();
1508 edit(&db, admin.id, Some(&fresh), None, None).await;
1509 assert!(db.app_passwords(admin.id).await.unwrap().is_empty());
1510 assert!(db.user_by_app_password(&hash).await.unwrap().is_none());
1511 }
1512
1513 #[tokio::test]
1514 async fn v1_db_migrates_to_v2() {
1515 let dir = tempfile::tempdir().unwrap();
1516 let path = dir.path().join("legacy.sqlite");
1517 {
1518 let conn = rusqlite::Connection::open(&path).unwrap();
1519 conn.execute_batch(SCHEMA_V1).unwrap();
1520 conn.execute(
1521 "INSERT INTO users (name, pass_hash, is_admin, created_at)
1522 VALUES ('legacy', 'hash', 1, '2024-01-01T00:00:00Z')",
1523 [],
1524 )
1525 .unwrap();
1526 conn.execute(
1527 "INSERT INTO user_roots (user_id, path, mode) VALUES (1, 'docs', 'rw')",
1528 [],
1529 )
1530 .unwrap();
1531 }
1532 let db = Db::open(&path).await.unwrap();
1533 assert_eq!(db.user_count().await.unwrap(), 1);
1534 let u = db.find_user_by_name("legacy").await.unwrap().unwrap();
1535 assert!(u.active, "v2 migration must default active to true");
1536 assert!(u.is_admin);
1537 assert_eq!(db.user_roots(u.id).await.unwrap().len(), 1);
1538 // Migrations are idempotent.
1539 let db2 = Db::open(&path).await.unwrap();
1540 assert_eq!(db2.user_count().await.unwrap(), 1);
1541 assert!(
1542 db2.find_user_by_name("legacy")
1543 .await
1544 .unwrap()
1545 .unwrap()
1546 .active
1547 );
1548 }
1549
1550 #[tokio::test]
1551 async fn admin_user_and_passwords() {
1552 let (db, admin) = db_with_admin().await;
1553 assert!(admin.is_admin);
1554 assert!(admin.active);
1555 // Root "." rw is assigned by create_admin.
1556 let roots = db.user_roots(admin.id).await.unwrap();
1557 assert_eq!(roots.len(), 1);
1558 assert_eq!(roots[0].path, ".");
1559 assert_eq!(roots[0].mode, Mode::Rw);
1560
1561 assert!(
1562 db.verify_password("admin", "admin1234")
1563 .await
1564 .unwrap()
1565 .is_some()
1566 );
1567 assert!(db.verify_password("admin", "nope").await.unwrap().is_none());
1568 // Name lookup is case-insensitive (COLLATE NOCASE).
1569 assert!(
1570 db.verify_password("ADMIN", "admin1234")
1571 .await
1572 .unwrap()
1573 .is_some()
1574 );
1575 // Disabled users cannot verify.
1576 edit(&db, admin.id, None, None, Some(false)).await;
1577 assert!(
1578 db.verify_password("admin", "admin1234")
1579 .await
1580 .unwrap()
1581 .is_none()
1582 );
1583 edit(&db, admin.id, None, None, Some(true)).await;
1584 assert!(
1585 db.verify_password("admin", "admin1234")
1586 .await
1587 .unwrap()
1588 .is_some()
1589 );
1590 }
1591
1592 #[tokio::test]
1593 async fn setup_is_won_by_exactly_one_caller() {
1594 let db = mem().await;
1595 let hash = crate::auth::hash_password("admin1234").unwrap();
1596 assert!(db.create_admin("first", &hash).await.unwrap().is_some());
1597 // The guard lives in the insert, so a different name loses too.
1598 assert!(db.create_admin("second", &hash).await.unwrap().is_none());
1599 assert_eq!(db.user_count().await.unwrap(), 1);
1600 // The loser rolled back cleanly: no orphaned root row.
1601 let first = db.find_user_by_name("first").await.unwrap().unwrap();
1602 assert_eq!(db.user_roots(first.id).await.unwrap().len(), 1);
1603 }
1604
1605 #[tokio::test]
1606 async fn changing_a_password_drops_that_users_sessions() {
1607 let (db, admin) = db_with_admin().await;
1608 let h = crate::auth::hash_password("bobpass1").unwrap();
1609 let bob = db.create_user("bob", &h, false, &[]).await.unwrap();
1610 db.create_session(admin.id, "admin-tok").await.unwrap();
1611 db.create_session(bob.id, "bob-tok-1").await.unwrap();
1612 db.create_session(bob.id, "bob-tok-2").await.unwrap();
1613
1614 let new_h = crate::auth::hash_password("bobpass2").unwrap();
1615 edit(&db, bob.id, Some(&new_h), None, None).await;
1616 assert!(
1617 db.session_user_with_roots("bob-tok-1")
1618 .await
1619 .unwrap()
1620 .is_none()
1621 );
1622 assert!(
1623 db.session_user_with_roots("bob-tok-2")
1624 .await
1625 .unwrap()
1626 .is_none()
1627 );
1628 // Only the reset user is signed out.
1629 assert!(
1630 db.session_user_with_roots("admin-tok")
1631 .await
1632 .unwrap()
1633 .is_some()
1634 );
1635
1636 // The admin-edit path bundles the same rule into its transaction.
1637 db.create_session(bob.id, "bob-tok-3").await.unwrap();
1638 let h3 = crate::auth::hash_password("bobpass3").unwrap();
1639 db.update_user(bob.id, Some(&h3), None, None, None)
1640 .await
1641 .unwrap();
1642 assert!(
1643 db.session_user_with_roots("bob-tok-3")
1644 .await
1645 .unwrap()
1646 .is_none()
1647 );
1648 // An edit that leaves the password alone keeps the session.
1649 db.create_session(bob.id, "bob-tok-4").await.unwrap();
1650 db.update_user(bob.id, None, Some(true), None, None)
1651 .await
1652 .unwrap();
1653 assert!(
1654 db.session_user_with_roots("bob-tok-4")
1655 .await
1656 .unwrap()
1657 .is_some()
1658 );
1659 }
1660
1661 #[tokio::test]
1662 async fn sessions_lifecycle() {
1663 let (db, admin) = db_with_admin().await;
1664 assert!(
1665 db.session_user_with_roots("ghost-token")
1666 .await
1667 .unwrap()
1668 .is_none()
1669 );
1670 db.create_session(admin.id, "tok1").await.unwrap();
1671 let (u, _) = db.session_user_with_roots("tok1").await.unwrap().unwrap();
1672 assert_eq!(u.id, admin.id);
1673 // Disabling the user invalidates existing sessions.
1674 edit(&db, admin.id, None, None, Some(false)).await;
1675 assert!(db.session_user_with_roots("tok1").await.unwrap().is_none());
1676 edit(&db, admin.id, None, None, Some(true)).await;
1677 assert!(db.session_user_with_roots("tok1").await.unwrap().is_some());
1678 db.delete_session("tok1").await.unwrap();
1679 assert!(db.session_user_with_roots("tok1").await.unwrap().is_none());
1680 }
1681
1682 #[tokio::test]
1683 async fn user_crud_and_roots() {
1684 let (db, _admin) = db_with_admin().await;
1685 let h = crate::auth::hash_password("bobpass1").unwrap();
1686 let bob = db
1687 .create_user("bob", &h, false, &[("docs".into(), Mode::Rw)])
1688 .await
1689 .unwrap();
1690 assert!(!bob.is_admin);
1691 assert!(bob.active);
1692
1693 // Duplicate name (case-insensitive) is rejected.
1694 let h2 = crate::auth::hash_password("carolpass1").unwrap();
1695 assert!(db.create_user("BOB", &h2, false, &[]).await.is_err());
1696 assert!(db.create_user("carol", &h2, false, &[]).await.is_ok());
1697
1698 // Lookup helpers.
1699 assert_eq!(
1700 db.find_user_by_name("Bob").await.unwrap().unwrap().id,
1701 bob.id
1702 );
1703 assert_eq!(
1704 db.find_user_by_id(bob.id).await.unwrap().unwrap().name,
1705 "bob"
1706 );
1707 assert!(db.find_user_by_name("dave").await.unwrap().is_none());
1708 assert_eq!(db.all_users_with_roots().await.unwrap().len(), 3);
1709
1710 // Root replacement semantics.
1711 let roots = db.user_roots(bob.id).await.unwrap();
1712 assert_eq!(roots.len(), 1);
1713 db.update_user(
1714 bob.id,
1715 None,
1716 None,
1717 None,
1718 Some(&[(".".into(), Mode::Ro), ("docs".into(), Mode::Rw)]),
1719 )
1720 .await
1721 .unwrap();
1722 let roots = db.user_roots(bob.id).await.unwrap();
1723 assert_eq!(roots.len(), 2);
1724 assert!(roots.iter().any(|r| r.path == "." && r.mode == Mode::Ro));
1725 db.update_user(bob.id, None, None, None, Some(&[]))
1726 .await
1727 .unwrap();
1728 assert!(db.user_roots(bob.id).await.unwrap().is_empty());
1729
1730 // Password update.
1731 let new_h = crate::auth::hash_password("bobpass2").unwrap();
1732 edit(&db, bob.id, Some(&new_h), None, None).await;
1733 assert!(
1734 db.verify_password("bob", "bobpass1")
1735 .await
1736 .unwrap()
1737 .is_none()
1738 );
1739 assert!(
1740 db.verify_password("bob", "bobpass2")
1741 .await
1742 .unwrap()
1743 .is_some()
1744 );
1745
1746 // Admin flag + count (only active admins count).
1747 edit(&db, bob.id, None, Some(true), None).await;
1748 assert_eq!(db.count_admins().await.unwrap(), 2);
1749 edit(&db, bob.id, None, None, Some(false)).await;
1750 assert_eq!(db.count_admins().await.unwrap(), 1);
1751 edit(&db, bob.id, None, Some(false), None).await;
1752
1753 // Deletion.
1754 assert!(db.delete_user(bob.id).await.unwrap());
1755 assert!(db.find_user_by_id(bob.id).await.unwrap().is_none());
1756 assert!(!db.delete_user(bob.id).await.unwrap());
1757 assert_eq!(db.user_count().await.unwrap(), 2);
1758 }
1759
1760 fn share_row(expires_at: Option<&str>) -> ShareRow {
1761 ShareRow {
1762 id: 1,
1763 token: "t".into(),
1764 creator_id: 1,
1765 target: "docs".into(),
1766 is_file: false,
1767 mode: Mode::Ro,
1768 created_at: "2024-01-01T00:00:00Z".into(),
1769 expires_at: expires_at.map(str::to_string),
1770 password_hash: None,
1771 }
1772 }
1773
1774 #[test]
1775 fn share_expiry_logic() {
1776 assert!(!share_row(None).is_expired());
1777 assert!(!share_row(Some("2999-01-01T00:00:00Z")).is_expired());
1778 assert!(share_row(Some("2000-01-01T00:00:00Z")).is_expired());
1779 // Unparseable expiry → treated as not expired (fail open for reads).
1780 assert!(!share_row(Some("not-a-date")).is_expired());
1781 }
1782
1783 #[tokio::test]
1784 async fn shares_crud() {
1785 let (db, admin) = db_with_admin().await;
1786 let s1 = db
1787 .create_share(admin.id, "tok-a", "docs", false, Mode::Ro, None, None)
1788 .await
1789 .unwrap();
1790 let s2 = db
1791 .create_share(
1792 admin.id,
1793 "tok-b",
1794 "file.txt",
1795 true,
1796 Mode::Rw,
1797 Some("2999-01-01T00:00:00Z"),
1798 None,
1799 )
1800 .await
1801 .unwrap();
1802 assert!(s2.id > s1.id);
1803
1804 let found = db.share_by_token("tok-b").await.unwrap().unwrap();
1805 assert!(found.is_file);
1806 assert_eq!(found.mode, Mode::Rw);
1807 assert!(db.share_by_token("nope").await.unwrap().is_none());
1808
1809 // Listed newest-first.
1810 let list = db.user_shares(admin.id).await.unwrap();
1811 assert_eq!(list.len(), 2);
1812 assert_eq!(list[0].id, s2.id);
1813 // Other users see nothing.
1814 let h = crate::auth::hash_password("bobpass1").unwrap();
1815 let bob = db.create_user("bob", &h, false, &[]).await.unwrap();
1816 assert!(db.user_shares(bob.id).await.unwrap().is_empty());
1817
1818 // Only the creator can delete.
1819 assert!(!db.delete_share(s1.id, bob.id).await.unwrap());
1820 assert!(db.delete_share(s1.id, admin.id).await.unwrap());
1821 assert!(db.share_by_token("tok-a").await.unwrap().is_none());
1822 assert!(!db.delete_share(s1.id, admin.id).await.unwrap());
1823 }
1824
1825 /// The unlock token is what a visitor's cookie carries, so an unlock
1826 /// that opened the wrong share would be a full bypass of the password.
1827 #[tokio::test]
1828 async fn share_unlocks_are_bound_to_one_share() {
1829 let (db, admin) = db_with_admin().await;
1830 let a = db
1831 .create_share(
1832 admin.id,
1833 "tok-a",
1834 "docs",
1835 false,
1836 Mode::Ro,
1837 None,
1838 Some("hash"),
1839 )
1840 .await
1841 .unwrap();
1842 let b = db
1843 .create_share(
1844 admin.id,
1845 "tok-b",
1846 "other",
1847 false,
1848 Mode::Ro,
1849 None,
1850 Some("hash"),
1851 )
1852 .await
1853 .unwrap();
1854 assert_eq!(
1855 db.share_by_token("tok-a")
1856 .await
1857 .unwrap()
1858 .unwrap()
1859 .password_hash,
1860 Some("hash".to_string())
1861 );
1862
1863 let unlock = db.create_share_unlock(a.id).await.unwrap();
1864 assert!(db.share_unlock_valid(&unlock, a.id).await.unwrap());
1865 assert!(!db.share_unlock_valid(&unlock, b.id).await.unwrap());
1866 assert!(!db.share_unlock_valid("nonsense", a.id).await.unwrap());
1867
1868 // Deleting the share takes its unlocks with it, so a re-created
1869 // share that happened to reuse the id could not inherit them.
1870 assert!(db.delete_share(a.id, admin.id).await.unwrap());
1871 assert!(!db.share_unlock_valid(&unlock, a.id).await.unwrap());
1872 }
1873
1874 #[tokio::test]
1875 async fn revoking_a_path_takes_its_descendants_only() {
1876 let (db, admin) = db_with_admin().await;
1877 let mk = async |token: &str, target: &str| {
1878 db.create_share(admin.id, token, target, false, Mode::Ro, None, None)
1879 .await
1880 .unwrap();
1881 };
1882 mk("t-self", "docs").await;
1883 mk("t-child", "docs/a.txt").await;
1884 mk("t-deep", "docs/inner/b.txt").await;
1885 // A sibling whose name merely starts with "docs" must survive.
1886 mk("t-sibling", "docs2/c.txt").await;
1887 mk("t-other", "src").await;
1888 // SQL wildcards in a path are literal characters, not patterns.
1889 mk("t-wild", "do%s/d.txt").await;
1890
1891 assert_eq!(db.revoke_shares_at("docs").await.unwrap(), 3);
1892 for gone in ["t-self", "t-child", "t-deep"] {
1893 assert!(db.share_by_token(gone).await.unwrap().is_none(), "{gone}");
1894 }
1895 for kept in ["t-sibling", "t-other", "t-wild"] {
1896 assert!(db.share_by_token(kept).await.unwrap().is_some(), "{kept}");
1897 }
1898 // Revoking a path nobody shared is a no-op, not an error.
1899 assert_eq!(db.revoke_shares_at("nothing/here").await.unwrap(), 0);
1900 }
1901
1902 #[tokio::test]
1903 async fn settings_round_trip() {
1904 let (db, _admin) = db_with_admin().await;
1905 assert!(!db.allow_writable_shares().await.unwrap());
1906 db.set_allow_writable_shares(true).await.unwrap();
1907 assert!(db.allow_writable_shares().await.unwrap());
1908 // Upsert semantics.
1909 db.set_allow_writable_shares(false).await.unwrap();
1910 assert!(!db.allow_writable_shares().await.unwrap());
1911 // Generic get/set.
1912 db.set_setting("custom", "v").await.unwrap();
1913 assert_eq!(
1914 db.get_setting("custom").await.unwrap().as_deref(),
1915 Some("v")
1916 );
1917 assert_eq!(db.get_setting("missing").await.unwrap(), None);
1918 }
1919}
1920