lib.rs
⎇
Raw
1//! The HTTP wire contract of filebrowser-ng in one place.
2//!
3//! Both the server (axum) and the web frontend (wasm `fetch`) import these
4//! endpoint paths, query params and serde types, so the two sides cannot
5//! drift apart. Serde only — no axum, no wasm dependencies.
6
7use serde::{Deserialize, Serialize};
8
9// ---------------------------------------------------------------------------
10// Endpoint paths (single source of truth for the route table and the client)
11// ---------------------------------------------------------------------------
12
13pub const AUTH_LOGIN: &str = "/api/auth/login";
14pub const AUTH_LOGOUT: &str = "/api/auth/logout";
15pub const AUTH_ME: &str = "/api/auth/me";
16pub const AUTH_SETUP: &str = "/api/auth/setup";
17/// Change or set the signed-in user's password (`POST`), or remove it
18/// (`DELETE`, passkey-only accounts).
19pub const AUTH_PASSWORD: &str = "/api/auth/password";
20/// `PUT` the signed-in user's sign-in requirement ([`AuthMode`]).
21pub const AUTH_MODE: &str = "/api/auth/mode";
22/// The signed-in user's passkeys: `GET {AUTH_PASSKEYS}` lists them,
23/// `DELETE {AUTH_PASSKEYS}/{id}` removes one.
24pub const AUTH_PASSKEYS: &str = "/api/auth/passkeys";
25/// Start registering a new passkey (`POST`). Finished at
26/// `{AUTH_PASSKEYS_REGISTER}{FINISH_SUFFIX}`.
27pub const AUTH_PASSKEYS_REGISTER: &str = "/api/auth/passkeys/register";
28/// Start a passkey sign-in (`POST`, no session needed). Finished at
29/// `{AUTH_PASSKEY_LOGIN}{FINISH_SUFFIX}`.
30pub const AUTH_PASSKEY_LOGIN: &str = "/api/auth/passkey/login";
31/// The signed-in user's WebDAV app passwords: `GET {AUTH_APP_PASSWORDS}`
32/// lists them, `POST` creates one, `DELETE {AUTH_APP_PASSWORDS}/{id}` revokes
33/// one.
34pub const AUTH_APP_PASSWORDS: &str = "/api/auth/app-passwords";
35/// Second leg of both WebAuthn ceremonies: the browser's answer goes to the
36/// begin path plus this suffix.
37pub const FINISH_SUFFIX: &str = "/finish";
38/// File operations: `{FILES}/{root_id}` and `{FILES}/{root_id}/{path...}`.
39pub const FILES: &str = "/api/files";
40/// Share management (authenticated): `{SHARES}` and `{SHARES}/{id}`.
41pub const SHARES: &str = "/api/shares";
42/// Public share resolve (no login): `{SHARE}/{token}`.
43pub const SHARE: &str = "/api/share";
44/// Suffix on `{SHARE}/{token}`: submit the password of a protected share.
45pub const SHARE_UNLOCK_SUFFIX: &str = "/unlock";
46/// `GET /api/search` — name and/or content search, streamed as SSE.
47pub const SEARCH: &str = "/api/search";
48
49/// WebDAV mount of the signed-in user's roots: `{DAV}` and `{DAV}/{path...}`.
50pub const DAV: &str = "/dav";
51/// WebDAV mount of one public share: `{DAV_SHARE}/{token}/{path...}`.
52///
53/// A separate top-level path, not a segment under [`DAV`]: there, the first
54/// segment is a root's display name, which a reserved word could collide with.
55pub const DAV_SHARE: &str = "/dav-share";
56
57/// CalDAV and CardDAV: principals, calendars and address books.
58///
59/// Not under [`DAV`] for the same reason as [`DAV_SHARE`].
60pub const PIM: &str = "/pim";
61/// RFC 6764 discovery. Both redirect to [`PIM`].
62pub const WELL_KNOWN_CALDAV: &str = "/.well-known/caldav";
63pub const WELL_KNOWN_CARDDAV: &str = "/.well-known/carddav";
64
65/// Admin user management: `{ADMIN_USERS}` and `{ADMIN_USERS}/{id}`.
66pub const ADMIN_USERS: &str = "/api/admin/users";
67/// Admin view of every share on the server: `{ADMIN_SHARES}` and
68/// `{ADMIN_SHARES}/{id}`. [`SHARES`] is the same data scoped to the caller.
69pub const ADMIN_SHARES: &str = "/api/admin/shares";
70pub const ADMIN_SETTINGS: &str = "/api/admin/settings";
71/// Admin management of rooms and resources: `{ADMIN_ROOMS}` and
72/// `{ADMIN_ROOMS}/{id}`.
73pub const ADMIN_ROOMS: &str = "/api/admin/rooms";
74/// The signed-in user's calendars and address books, own and lent to them
75/// (`GET`). `{PIM_COLLECTIONS}/{id}{SHARES_SUFFIX}` lists (`GET`) and lends
76/// (`POST`) an own one; `DELETE` on `.../{user_id}` below it ends a loan.
77pub const PIM_COLLECTIONS: &str = "/api/pim/collections";
78pub const SHARES_SUFFIX: &str = "/shares";
79/// Pseudo root id every signed-in admin has on the files API: the whole
80/// server root, read-only (the admin folder picker browses it). Real root
81/// ids are positive database ids. Not listed in `/me`.
82pub const ADMIN_ROOT: i64 = -1;
83
84// ---------------------------------------------------------------------------
85// Query params
86// ---------------------------------------------------------------------------
87
88/// `?action=...` on file URLs; without it the route lists the directory.
89pub const P_ACTION: &str = "action";
90pub const ACTION_DOWNLOAD: &str = "download";
91pub const ACTION_PREVIEW: &str = "preview";
92pub const ACTION_CONTENT: &str = "content";
93pub const ACTION_THUMB: &str = "thumb";
94/// `POST {FILES}/...?action=mkdir` — create a folder. Explicit, because the
95/// POST route also carries uploads and mutations.
96pub const ACTION_MKDIR: &str = "mkdir";
97/// `POST {FILES}/...?action=create-file` — create an empty file. Explicit
98/// like `mkdir`, for the same reason.
99pub const ACTION_CREATE_FILE: &str = "create-file";
100/// `POST {FILES}/...?action=exists` with an [`ExistsReq`] body — read-only
101/// pre-check for an upload: which of the given targets already exist.
102pub const ACTION_EXISTS: &str = "exists";
103/// `?format=...` for folder downloads (values: see `server::archive::ArchiveFormat`).
104pub const P_FORMAT: &str = "format";
105/// `?share=<token>` — authenticate file calls with a public share token.
106pub const P_SHARE: &str = "share";
107/// Search query text (`GET {SEARCH}`).
108pub const P_Q: &str = "q";
109/// Which index to search: `name`, `content` or `both`.
110pub const P_SCOPE: &str = "scope";
111/// Root id to search; omitted = the caller's first root.
112pub const P_ROOT: &str = "root";
113/// Folder inside the root to start a search in (relative to the root);
114/// omitted or empty = the whole root.
115pub const P_PATH: &str = "path";
116/// `?overwrite=true|1` on mutations and uploads.
117pub const P_OVERWRITE: &str = "overwrite";
118/// Listing order ([`SortKey`]); omitted = by name.
119pub const P_SORT: &str = "sort";
120/// `?desc=true` reverses the listing order. Folders still come first.
121pub const P_DESC: &str = "desc";
122/// First listing entry to return, in the sorted order.
123pub const P_OFFSET: &str = "offset";
124/// Listing entries to return, capped at [`MAX_LIST_ENTRIES`]; omitted = the cap.
125pub const P_LIMIT: &str = "limit";
126/// `?dirs=true` lists only the subfolders (the folder picker).
127pub const P_DIRS: &str = "dirs";
128/// `?around=name` returns the page that holds this entry, instead of the
129/// one at the offset. A missing name falls back to the offset.
130pub const P_AROUND: &str = "around";
131
132// ---------------------------------------------------------------------------
133// Wire enums
134// ---------------------------------------------------------------------------
135
136/// Access mode of a root or a share.
137///
138/// The serde names are also the values stored in the SQLite `mode` columns,
139/// so renaming a variant would break existing databases. The round-trip test
140/// below pins them.
141#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq)]
142#[serde(rename_all = "lowercase")]
143pub enum Mode {
144 Rw,
145 Ro,
146}
147
148impl Mode {
149 /// The only question callers ask: may this root be written to?
150 pub fn is_writable(self) -> bool {
151 matches!(self, Mode::Rw)
152 }
153
154 /// The wire/database spelling, for `<select>` values and SQL params.
155 pub fn as_str(self) -> &'static str {
156 match self {
157 Mode::Rw => "rw",
158 Mode::Ro => "ro",
159 }
160 }
161
162 /// Parse the wire spelling. `None` for anything else.
163 pub fn from_wire(s: &str) -> Option<Self> {
164 match s {
165 "rw" => Some(Mode::Rw),
166 "ro" => Some(Mode::Ro),
167 _ => None,
168 }
169 }
170}
171
172/// Which mutation [`Mutation`] asks for.
173#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq)]
174#[serde(rename_all = "lowercase")]
175pub enum Op {
176 Rename,
177 Move,
178 Copy,
179}
180
181/// What a listing is ordered by ([`P_SORT`]). Folders always sort before
182/// files, so a size or date order does not scatter them through the listing.
183#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq, Default)]
184#[serde(rename_all = "lowercase")]
185pub enum SortKey {
186 #[default]
187 Name,
188 Size,
189 Modified,
190}
191
192impl SortKey {
193 pub fn as_str(self) -> &'static str {
194 match self {
195 SortKey::Name => "name",
196 SortKey::Size => "size",
197 SortKey::Modified => "modified",
198 }
199 }
200
201 pub fn parse(s: &str) -> Option<Self> {
202 match s {
203 "name" => Some(SortKey::Name),
204 "size" => Some(SortKey::Size),
205 "modified" => Some(SortKey::Modified),
206 _ => None,
207 }
208 }
209}
210
211// ---------------------------------------------------------------------------
212// Request bodies (client → server)
213// ---------------------------------------------------------------------------
214
215#[derive(Serialize, Deserialize)]
216pub struct Credentials {
217 pub name: String,
218 pub password: String,
219}
220
221/// Rename / move / copy (one body for all file mutations).
222#[derive(Serialize, Deserialize)]
223pub struct Mutation {
224 pub op: Op,
225 #[serde(default, skip_serializing_if = "Option::is_none")]
226 pub new_name: Option<String>,
227 #[serde(default, skip_serializing_if = "Option::is_none")]
228 pub dst_root_id: Option<i64>,
229 /// Destination directory, relative to `dst_root_id`.
230 #[serde(default, skip_serializing_if = "Option::is_none")]
231 pub dst: Option<String>,
232 #[serde(default)]
233 pub overwrite: bool,
234}
235
236/// A user folder: path relative to the server root + access mode.
237#[derive(Serialize, Deserialize)]
238pub struct Root {
239 /// Path relative to the server root; "." means the whole root.
240 pub path: String,
241 #[serde(default = "default_rw")]
242 pub mode: Mode,
243}
244
245fn default_rw() -> Mode {
246 Mode::Rw
247}
248
249#[derive(Serialize, Deserialize)]
250pub struct CreateUser {
251 pub name: String,
252 pub password: String,
253 #[serde(default)]
254 pub is_admin: bool,
255 #[serde(default)]
256 pub roots: Vec<Root>,
257}
258
259#[derive(Serialize, Deserialize)]
260pub struct UpdateUser {
261 /// Setting one is also the recovery path for a locked-out account: it
262 /// deletes every passkey and puts the account back on
263 /// [`AuthMode::Either`], leaving the new password as the one way in.
264 #[serde(default, skip_serializing_if = "Option::is_none")]
265 pub password: Option<String>,
266 #[serde(default, skip_serializing_if = "Option::is_none")]
267 pub is_admin: Option<bool>,
268 #[serde(default, skip_serializing_if = "Option::is_none")]
269 pub active: Option<bool>,
270 #[serde(default, skip_serializing_if = "Option::is_none")]
271 pub roots: Option<Vec<Root>>,
272}
273
274/// Server settings (GET/PUT `{ADMIN_SETTINGS}`).
275#[derive(Serialize, Deserialize, Clone)]
276pub struct Settings {
277 pub allow_writable_shares: bool,
278 /// Folders left out of every search, as paths relative to the server
279 /// root. A path covers everything beneath it.
280 #[serde(default)]
281 pub search_excludes: Vec<String>,
282}
283
284#[derive(Serialize, Deserialize)]
285pub struct CreateShare {
286 pub root_id: i64,
287 /// Item path relative to the root ("" or "." for the root itself).
288 pub path: String,
289 #[serde(default)]
290 pub writable: bool,
291 /// Absolute expiry as RFC 3339; absent = never.
292 #[serde(default, skip_serializing_if = "Option::is_none")]
293 pub expires_at: Option<String>,
294 /// Password the visitor must enter before the share opens; absent = none.
295 #[serde(default, skip_serializing_if = "Option::is_none")]
296 pub password: Option<String>,
297}
298
299/// POST `{SHARE}/{token}/unlock` — the password for a protected share.
300#[derive(Serialize, Deserialize)]
301pub struct UnlockShare {
302 pub password: String,
303}
304
305// ---------------------------------------------------------------------------
306// Responses (server → client)
307// ---------------------------------------------------------------------------
308
309/// What a listing entry actually is, decided by the server from the file's
310/// leading bytes (magic numbers via `infer`, plus a text/binary heuristic) —
311/// not from its name. Drives the icon and the preview the client offers.
312///
313/// Deliberately coarse: this answers "which viewer opens this", not "what
314/// exact format is it". Syntax highlighting still keys off the extension,
315/// because `.h` is C or C++ and no amount of sniffing decides that.
316#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq)]
317#[serde(rename_all = "lowercase")]
318pub enum FileKind {
319 Dir,
320 Image,
321 Video,
322 Audio,
323 Pdf,
324 Archive,
325 /// Anything that decodes as text: source code, markup, config, plain text.
326 Text,
327 /// Recognized-but-not-viewable, or undecodable bytes.
328 Binary,
329}
330
331#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
332pub struct Entry {
333 pub name: String,
334 pub is_dir: bool,
335 pub size: u64,
336 /// RFC 3339 UTC modification time.
337 pub mtime: String,
338 /// Content-sniffed kind (see [`FileKind`]).
339 pub kind: FileKind,
340}
341
342/// One page of a folder listing.
343#[derive(Serialize, Deserialize)]
344pub struct FilesResp {
345 pub entries: Vec<Entry>,
346 /// Entries in the whole folder, across all pages.
347 pub total: usize,
348 /// Position of `entries[0]` in the sorted folder. An offset past the end
349 /// comes back as the start of the last page.
350 pub offset: usize,
351}
352
353/// Cap on entries in one listing page.
354pub const MAX_LIST_ENTRIES: usize = 10_000;
355
356/// One streamed search result. Each is sent as one SSE event
357/// (`data: <json>`), in the order found; the `done` event always ends the
358/// stream.
359#[derive(Serialize, Deserialize, Clone)]
360#[serde(tag = "type", rename_all = "snake_case")]
361pub enum SearchEvent {
362 /// A file or folder whose name matched (scope `name`/`both`).
363 File {
364 root_id: i64,
365 /// Path relative to the root, `/`-separated.
366 path: String,
367 size: u64,
368 is_dir: bool,
369 /// Sniffed the same way as a directory listing's, so the client can
370 /// pick an icon and a viewer without a second guess at the name.
371 kind: FileKind,
372 },
373 /// One matching line (scope `content`/`both`). `path` is relative to the
374 /// root; `text` is the matched line, truncated to a fixed length.
375 Match {
376 root_id: i64,
377 path: String,
378 line: u64,
379 text: String,
380 },
381 /// Stream finished. `stopped` is true when the client aborted before the
382 /// search completed.
383 Done {
384 stopped: bool,
385 /// Files examined (walked) before the stream ended.
386 scanned: usize,
387 /// Files skipped for content search (over the size cap).
388 skipped: usize,
389 elapsed_ms: u64,
390 },
391}
392
393#[derive(Serialize, Deserialize, Clone, PartialEq)]
394pub struct UserInfo {
395 pub id: i64,
396 pub name: String,
397 pub is_admin: bool,
398 /// Profile setting: single click opens entries (off = click selects,
399 /// double click opens).
400 pub single_click_open: bool,
401 /// Profile setting: show image and video thumbnails in the grid.
402 pub thumbnails: bool,
403 /// Preferred UI language tag ("en", "de", "fr"); None = follow the
404 /// browser.
405 pub language: Option<String>,
406 /// Profile setting: the root the UI opens on page load and on the home
407 /// link. Always one of `Me::roots` (the server drops a stale id), or
408 /// None for the root picker.
409 pub default_root_id: Option<i64>,
410 /// What this account needs to sign in.
411 pub auth_mode: AuthMode,
412 /// Whether a password is set at all. False means passkeys only.
413 pub has_password: bool,
414}
415
416#[derive(Serialize, Deserialize, Clone)]
417pub struct RootInfo {
418 pub id: i64,
419 pub name: String,
420 pub path: String,
421 pub mode: Mode,
422}
423
424/// GET `{AUTH_ME}`.
425#[derive(Serialize, Deserialize, Clone)]
426pub struct Me {
427 /// True while no users exist yet (first-boot setup).
428 pub first_boot: bool,
429 /// None on first boot.
430 pub user: Option<UserInfo>,
431 pub roots: Vec<RootInfo>,
432 pub allow_writable_shares: bool,
433 /// Whether the server can make thumbnails at all (`--cache` is set).
434 /// The profile setting is only offered when this is true.
435 pub thumbnails_available: bool,
436 /// `--public-url`, if set. The UI builds share links from it instead of
437 /// the page origin.
438 pub public_url: Option<String>,
439}
440
441/// GET/POST `{SHARES}`, GET `{SHARE}/{token}`.
442#[derive(Serialize, Deserialize, Clone)]
443pub struct ShareInfo {
444 /// Also the share's synthetic root id in file API calls.
445 pub id: i64,
446 pub token: String,
447 /// Display name (file/folder name, or the root's name for ".").
448 pub name: String,
449 pub is_file: bool,
450 pub writable: bool,
451 /// Path relative to the server root.
452 pub target: String,
453 /// RFC 3339 UTC creation time.
454 pub created_at: String,
455 /// RFC 3339 UTC expiry; None = never.
456 pub expires_at: Option<String>,
457 /// The file's kind for file shares (None for folder shares, and when
458 /// not sniffed — the public resolve endpoint fills it in).
459 pub kind: Option<FileKind>,
460 /// Whether the share asks for a password. Never the password itself.
461 pub has_password: bool,
462}
463
464/// One share plus who owns it: GET `{ADMIN_SHARES}`.
465///
466/// Admin-only: it carries the full [`ShareInfo::token`], and a token is access.
467/// Kept separate from [`ShareInfo`] because the public resolve route answers
468/// with a `ShareInfo` to anonymous visitors.
469#[derive(Serialize, Deserialize, Clone)]
470pub struct AdminShare {
471 #[serde(flatten)]
472 pub share: ShareInfo,
473 pub creator_id: i64,
474 pub creator_name: String,
475 /// Whether the creator's account can still sign in. Deactivating an account
476 /// does not revoke its shares, so `false` marks a live link its owner can no
477 /// longer manage.
478 pub creator_active: bool,
479}
480
481/// GET/POST `{ADMIN_USERS}`, PUT `{ADMIN_USERS}/{id}`.
482#[derive(Serialize, Deserialize, Clone)]
483pub struct AdminUser {
484 pub id: i64,
485 pub name: String,
486 pub is_admin: bool,
487 pub active: bool,
488 pub roots: Vec<RootInfo>,
489}
490
491/// Acknowledges a successful mutation. Carries nothing: the 2xx status is
492/// the acknowledgement, so the body is the empty object.
493#[derive(Serialize, Deserialize)]
494pub struct OkResp {}
495
496#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq)]
497#[serde(rename_all = "lowercase")]
498pub enum PimCollectionKind {
499 Calendar,
500 Addressbook,
501}
502
503/// One entry of `GET {PIM_COLLECTIONS}`.
504#[derive(Serialize, Deserialize, Clone, Debug)]
505pub struct PimCollectionInfo {
506 pub id: i64,
507 pub kind: PimCollectionKind,
508 pub name: String,
509 /// The CalDAV or CardDAV URL, as seen by the signed-in user.
510 pub url: String,
511 pub owner: String,
512 /// `None` for an own collection, the loan's mode for a lent one.
513 pub mode: Option<Mode>,
514}
515
516/// `GET {PIM_COLLECTIONS}/{id}{SHARES_SUFFIX}`.
517#[derive(Serialize, Deserialize, Clone, Debug)]
518pub struct PimShareInfo {
519 pub user_id: i64,
520 pub user_name: String,
521 pub mode: Mode,
522}
523
524/// `POST {PIM_COLLECTIONS}/{id}{SHARES_SUFFIX}`: lend to an account, or
525/// change the mode of an existing loan.
526#[derive(Serialize, Deserialize)]
527pub struct CreatePimShare {
528 pub user: String,
529 pub mode: Mode,
530}
531
532#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq)]
533#[serde(rename_all = "lowercase")]
534pub enum RoomKind {
535 Room,
536 Resource,
537}
538
539/// A room or resource: `GET {ADMIN_ROOMS}`.
540#[derive(Serialize, Deserialize, Clone, Debug)]
541pub struct RoomInfo {
542 pub id: i64,
543 /// The URL segment. Fixed, since it is also the scheduling address.
544 pub name: String,
545 pub display_name: String,
546 pub kind: RoomKind,
547 /// The principal URL.
548 pub url: String,
549}
550
551/// `POST {ADMIN_ROOMS}`.
552#[derive(Serialize, Deserialize)]
553pub struct CreateRoom {
554 pub name: String,
555 /// Defaults to `name`.
556 pub display_name: Option<String>,
557 pub kind: RoomKind,
558}
559
560/// `PUT {ADMIN_ROOMS}/{id}`.
561#[derive(Serialize, Deserialize)]
562pub struct UpdateRoom {
563 pub display_name: String,
564}
565
566/// `POST ...?action=exists` body: upload targets relative to the request
567/// directory (may contain subfolders, like upload part names).
568#[derive(Serialize, Deserialize)]
569pub struct ExistsReq {
570 pub paths: Vec<String>,
571}
572
573/// One existing upload target.
574#[derive(Serialize, Deserialize, Clone, PartialEq, Debug)]
575pub struct Existing {
576 pub path: String,
577 pub is_dir: bool,
578}
579
580/// `POST ...?action=exists` response: the subset of the requested paths
581/// that exist, in request order.
582#[derive(Serialize, Deserialize)]
583pub struct ExistsResp {
584 pub existing: Vec<Existing>,
585}
586
587/// PUT `?action=content` (editor save): the file's new mtime (unix seconds).
588#[derive(Serialize, Deserialize)]
589pub struct SaveResp {
590 pub mtime: i64,
591}
592
593#[cfg(test)]
594mod tests {
595 use super::*;
596
597 /// The serde spellings are the database values too, so they are pinned.
598 #[test]
599 fn mode_wire_format_is_rw_ro() {
600 assert_eq!(serde_json::to_string(&Mode::Rw).unwrap(), "\"rw\"");
601 assert_eq!(serde_json::to_string(&Mode::Ro).unwrap(), "\"ro\"");
602 for m in [Mode::Rw, Mode::Ro] {
603 let s = serde_json::to_string(&m).unwrap();
604 assert_eq!(serde_json::from_str::<Mode>(&s).unwrap(), m);
605 // `as_str`/`from_wire` must agree with serde.
606 assert_eq!(s, format!("\"{}\"", m.as_str()));
607 assert_eq!(Mode::from_wire(m.as_str()), Some(m));
608 }
609 assert_eq!(Mode::from_wire("both"), None);
610 assert!(serde_json::from_str::<Mode>("\"both\"").is_err());
611 assert!(Mode::Rw.is_writable());
612 assert!(!Mode::Ro.is_writable());
613 }
614
615 #[test]
616 fn op_wire_format() {
617 assert_eq!(serde_json::to_string(&Op::Rename).unwrap(), "\"rename\"");
618 assert_eq!(serde_json::to_string(&Op::Move).unwrap(), "\"move\"");
619 assert_eq!(serde_json::to_string(&Op::Copy).unwrap(), "\"copy\"");
620 for op in [Op::Rename, Op::Move, Op::Copy] {
621 let s = serde_json::to_string(&op).unwrap();
622 assert_eq!(serde_json::from_str::<Op>(&s).unwrap(), op);
623 }
624 assert!(serde_json::from_str::<Op>("\"explode\"").is_err());
625 }
626
627 #[test]
628 fn mutation_round_trip_skips_absent_fields() {
629 let m = Mutation {
630 op: Op::Move,
631 new_name: None,
632 dst_root_id: Some(3),
633 dst: Some("docs".into()),
634 overwrite: true,
635 };
636 let s = serde_json::to_string(&m).unwrap();
637 assert!(!s.contains("new_name"));
638 let back: Mutation = serde_json::from_str(&s).unwrap();
639 assert_eq!(back.dst_root_id, Some(3));
640 assert_eq!(back.op, Op::Move);
641 }
642
643 #[test]
644 fn mutation_defaults_missing_fields() {
645 let m: Mutation = serde_json::from_str(r#"{"op":"rename","new_name":"a.txt"}"#).unwrap();
646 assert!(!m.overwrite);
647 assert_eq!(m.dst, None);
648 }
649
650 #[test]
651 fn root_defaults_mode_to_rw() {
652 let r: Root = serde_json::from_str(r#"{"path":"docs"}"#).unwrap();
653 assert_eq!(r.mode, Mode::Rw);
654 }
655
656 #[test]
657 fn me_round_trip() {
658 let me = Me {
659 first_boot: false,
660 user: Some(UserInfo {
661 id: 1,
662 name: "admin".into(),
663 is_admin: true,
664 single_click_open: false,
665 thumbnails: true,
666 language: None,
667 default_root_id: None,
668 auth_mode: AuthMode::Either,
669 has_password: true,
670 }),
671 roots: vec![RootInfo {
672 id: 1,
673 name: "root".into(),
674 path: ".".into(),
675 mode: Mode::Rw,
676 }],
677 allow_writable_shares: false,
678 thumbnails_available: true,
679 public_url: None,
680 };
681 let s = serde_json::to_string(&me).unwrap();
682 let back: Me = serde_json::from_str(&s).unwrap();
683 assert_eq!(back.roots.len(), 1);
684 }
685}
686
687// ---------------------------------------------------------------------------
688// Sign-in methods: password, passkeys, and how they combine
689// ---------------------------------------------------------------------------
690
691/// What an account needs to sign in.
692///
693/// Not a "2FA on/off" flag: [`AuthMode::Either`] with no password is a
694/// passkey-only account, which is still two factors when the authenticator
695/// does user verification (the server always asks for it).
696#[derive(Serialize, Deserialize, Clone, Copy, Debug, Default, PartialEq, Eq)]
697#[serde(rename_all = "lowercase")]
698pub enum AuthMode {
699 /// Password *or* passkey. Either one alone signs the user in.
700 #[default]
701 Either,
702 /// Password *and* passkey. Both legs must pass, in either order.
703 Both,
704}
705
706impl AuthMode {
707 pub fn as_str(self) -> &'static str {
708 match self {
709 AuthMode::Either => "either",
710 AuthMode::Both => "both",
711 }
712 }
713
714 pub fn from_wire(s: &str) -> Option<Self> {
715 match s {
716 "either" => Some(AuthMode::Either),
717 "both" => Some(AuthMode::Both),
718 _ => None,
719 }
720 }
721}
722
723/// One registered passkey, as shown in profile settings. Never carries key
724/// material.
725#[derive(Serialize, Deserialize, Clone)]
726pub struct PasskeyInfo {
727 pub id: i64,
728 /// User-chosen label ("YubiKey", "Work laptop").
729 pub name: String,
730 /// RFC 3339 UTC.
731 pub created_at: String,
732 pub last_used_at: Option<String>,
733 /// Whether the browser reported this credential as discoverable, so it
734 /// can sign in without the account name. `None` when the browser did not
735 /// say — the `credProps` extension is optional and unsigned, so absence
736 /// means "unknown", never "no".
737 pub discoverable: Option<bool>,
738}
739
740/// One app password, as shown in profile settings.
741///
742/// WebDAV-only: it never signs in to the web UI. Never carries the secret,
743/// which exists only in [`NewAppPassword`].
744#[derive(Serialize, Deserialize, Clone)]
745pub struct AppPasswordInfo {
746 pub id: i64,
747 /// User-chosen label ("Laptop mount", "phone").
748 pub name: String,
749 /// RFC 3339 UTC.
750 pub created_at: String,
751 /// Only tracked to the hour.
752 pub last_used_at: Option<String>,
753}
754
755/// `POST {AUTH_APP_PASSWORDS}`.
756#[derive(Serialize, Deserialize)]
757pub struct CreateAppPassword {
758 pub name: String,
759}
760
761/// The answer to `POST {AUTH_APP_PASSWORDS}`.
762///
763/// The only time `secret` is readable. The server keeps a hash of it.
764#[derive(Serialize, Deserialize)]
765pub struct NewAppPassword {
766 #[serde(flatten)]
767 pub info: AppPasswordInfo,
768 pub secret: String,
769}
770
771/// `POST {AUTH_PASSWORD}` — set or change the password.
772///
773/// No current password to confirm: a passkey-only account has none to give.
774/// The session is the gate, and the server drops the account's other
775/// sessions on every change.
776#[derive(Serialize, Deserialize)]
777pub struct ChangePassword {
778 pub new_password: String,
779}
780
781/// `PUT {AUTH_MODE}`.
782#[derive(Serialize, Deserialize)]
783pub struct SetAuthMode {
784 pub mode: AuthMode,
785}
786
787/// A WebAuthn challenge on its way to the browser.
788///
789/// `options` is the raw JSON the browser's `parseCreationOptionsFromJSON` /
790/// `parseRequestOptionsFromJSON` expects, carried as a string rather than a
791/// nested object. Neither side has to re-parse it: the server serializes the
792/// `webauthn-rs` type straight into it, and the client hands it to
793/// `JSON.parse` in the browser shim.
794#[derive(Serialize, Deserialize)]
795pub struct PasskeyChallenge {
796 /// Opaque handle for the server-side ceremony state. Echoed back on
797 /// finish. Not a credential, and useless on its own.
798 pub state_id: String,
799 pub options: String,
800}
801
802/// `POST {AUTH_PASSKEYS_REGISTER}{FINISH_SUFFIX}`.
803#[derive(Serialize, Deserialize)]
804pub struct PasskeyRegisterFinish {
805 pub state_id: String,
806 /// Label for the new passkey.
807 pub name: String,
808 /// The browser's `PublicKeyCredential.toJSON()` output, verbatim.
809 pub credential: String,
810}
811
812/// `POST {AUTH_PASSKEY_LOGIN}` — begin a passkey sign-in.
813#[derive(Serialize, Deserialize)]
814pub struct PasskeyLoginBegin {
815 /// Account name, when the user typed one. Without it the server issues a
816 /// discoverable challenge, which only finds passkeys the authenticator
817 /// stores itself.
818 #[serde(default, skip_serializing_if = "Option::is_none")]
819 pub name: Option<String>,
820 /// Ask for a conditional-mediation (autofill) challenge instead of a
821 /// modal one.
822 #[serde(default)]
823 pub conditional: bool,
824}
825
826/// `POST {AUTH_PASSKEY_LOGIN}{FINISH_SUFFIX}`.
827#[derive(Serialize, Deserialize)]
828pub struct PasskeyLoginFinish {
829 pub state_id: String,
830 pub credential: String,
831}
832
833/// `POST {AUTH_LOGIN}` — the password leg of a sign-in.
834#[derive(Serialize, Deserialize)]
835pub struct LoginReq {
836 /// Omitted only when `state_id` names a half-finished sign-in, which
837 /// already knows who the user is.
838 #[serde(default, skip_serializing_if = "Option::is_none")]
839 pub name: Option<String>,
840 pub password: String,
841 /// Handle from a passkey leg that still needs a password (an
842 /// [`AuthMode::Both`] account signing in passkey-first).
843 #[serde(default, skip_serializing_if = "Option::is_none")]
844 pub state_id: Option<String>,
845}
846
847/// The answer to either sign-in leg.
848///
849/// Exactly one of the three shapes: signed in, needs a passkey next, or needs
850/// a password next. The two "needs" cases are how [`AuthMode::Both`] works,
851/// and which one appears depends only on which leg the user started with.
852#[derive(Serialize, Deserialize, Default)]
853pub struct LoginResp {
854 /// True when the session cookie is set and the user is in.
855 pub ok: bool,
856 /// Present when this leg passed but a passkey is still required.
857 #[serde(default, skip_serializing_if = "Option::is_none")]
858 pub passkey_challenge: Option<PasskeyChallenge>,
859 /// Present when this leg passed but the password is still required.
860 /// Carries the account name, so the form can show whose password it
861 /// wants, and the handle to send back with it.
862 #[serde(default, skip_serializing_if = "Option::is_none")]
863 pub password_required: Option<PasswordStep>,
864}
865
866#[derive(Serialize, Deserialize, Clone)]
867pub struct PasswordStep {
868 pub name: String,
869 pub state_id: String,
870}
871