in_old
Clone
https://git.tahinli.com/Tahinli/in_old.git
0 watching0 forksRustGPL-3.01.1 MiB218b775Tahinli2 months ago90 commits
| 1 | //! Server-side plumbing: the application context, the person behind the |
| 2 | //! current request, and the role guards. |
| 3 | //! |
| 4 | //! Every guard here is the real one. The UI hides what a role may not do, but |
| 5 | //! the answer that matters is the one given in this module, on the server. |
| 6 | //! |
| 7 | //! There are no local passwords in v1 — im holds the central session over |
| 8 | //! OIDC and `in-client` (vendored from `im-client`) holds this app's side of |
| 9 | //! it. This module's `current_user` maps im's claims onto the local user row, |
| 10 | //! provisioning it on first sight. |
| 11 | |
| 12 | use HashMap; |
| 13 | |
| 14 | use ; |
| 15 | |
| 16 | use Config; |
| 17 | use ; |
| 18 | use ; |
| 19 | use Digest; |
| 20 | use AssetBundle; |
| 21 | use ; |
| 22 | use ; |
| 23 | use ; |
| 24 | |
| 25 | /// The whole application context, put into the router by `main.rs`. One |
| 26 | /// struct rather than three separate contexts so a route needing two of them |
| 27 | /// reads both off the same value. The OIDC client state lives in the router |
| 28 | /// separately — `in_client::mount` puts it there — because its login routes |
| 29 | /// read it without going through this struct. |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | /// The live state of the identity-directory connection, as the Connection |
| 57 | /// card displays it. Cloned freely; every clone sees the same atomics. |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | /// The application context this request runs under. |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | /// The origin the public links carry: the `base_url` an admin saved on the |
| 148 | /// running install when one is set, else the config chain — the file's |
| 149 | /// `base_url`, else the address bound. Every public URL construction (the |
| 150 | /// share links and their copy-once banners) asks this; the settings |
| 151 | /// server-address panel is what edits the setting the lookup reads. |
| 152 | pub async |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | /// The full public address of a share link — `{origin}/s/{token}` — |
| 160 | /// re-derived from the token its creation sealed onto the |
| 161 | /// `share_token:{id}` setting row. |
| 162 | /// |
| 163 | /// `None` when the address cannot be told again: a link minted before the |
| 164 | /// sealing carries no row, and a key lost since minting opens nothing. Both |
| 165 | /// callers degrade the same way — the masked value plus the legacy note — |
| 166 | /// so the option is the whole contract. |
| 167 | pub async |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | /// One proxy hop in front of the process. `in` is reached through that hop, |
| 179 | /// so [`client_ip`] reads the address it appended rather than the hop's own. |
| 180 | /// A direct connection, with no hop in front, is its own address. |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | /// A stable-enough label for the client. |
| 186 | /// |
| 187 | /// [`client_ip`] under [`trusted_proxies`]: the address the one trusted hop |
| 188 | /// reported, or the peer when the request did not come through it. `unknown` |
| 189 | /// when neither is there. A client-supplied `x-forwarded-for` is not read |
| 190 | /// unless that hop appended it. |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | /// The stylesheet this binary serves must be the one compiled into it. |
| 198 | /// |
| 199 | /// `asset!` embeds an asset's declaration — its id and source path — into |
| 200 | /// the binary, but the served bytes live in the bundle directory beside the |
| 201 | /// executable, and topcoat loads whatever manifest it finds there. A bundle |
| 202 | /// left behind by another deploy sits beside a newer binary without a word |
| 203 | /// of complaint, and the pages then reference a stylesheet whose bytes are |
| 204 | /// from another generation — the mixed generation a browser once caught on |
| 205 | /// production. `build.rs` stamps the compiled stylesheet's SHA-256 into the |
| 206 | /// binary; this hashes the bundle's bytes against it, so a foreign bundle |
| 207 | /// refuses the boot instead of serving under it. |
| 208 | /// |
| 209 | /// Returns the startup log line naming the served fingerprint, or the |
| 210 | /// reason the boot must not proceed. |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | /// The person behind this request, or nobody, or the store failing to say. |
| 255 | /// |
| 256 | /// im is asked on every request — the cookie holds an opaque session token, |
| 257 | /// introspected per call — so an admin revoking the person in im signs them |
| 258 | /// out here at once. The first sight of a `sub` inserts the local row (and |
| 259 | /// the first user ever becomes the admin); every later sight refreshes the |
| 260 | /// address and name and stamps the last-seen time. |
| 261 | /// |
| 262 | /// A store error is not "nobody" — a busy database mid-drag is a fact about |
| 263 | /// the database, not about whether this browser is signed in, and a caller |
| 264 | /// that folded the two together would send a signed-in person to a sign-in |
| 265 | /// screen because a write elsewhere held a lock for a moment. |
| 266 | /// |
| 267 | /// A disabled account reads as nobody: the cookie is im's business and is |
| 268 | /// left untouched, but every guard below refuses as if signed out. |
| 269 | |
| 270 | pub async |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | |
| 276 | |
| 277 | |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | |
| 289 | |
| 290 | |
| 291 | |
| 292 | /// The person behind this request, or a refusal the caller can return as-is. |
| 293 | /// |
| 294 | /// A store error becomes `Refusal::Unavailable` rather than `SignInFirst`, |
| 295 | /// which would tell a signed-in person to sign in again over what was only |
| 296 | /// a database hiccup. |
| 297 | pub async |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | /// The sign-in probe the live channel's error path polls: 204 while this |
| 306 | /// browser carries a live session, 401 the moment it does not — no HTML, |
| 307 | /// no body, nothing but the answer. It exists for the one question an open |
| 308 | /// tab can ask mid-flight ("am I still signed in?") without dragging a |
| 309 | /// page render behind it. A store failure answers 500 rather than 401: |
| 310 | /// the probe's whole contract is that only a real sign-out draws the 401, |
| 311 | /// and a broken database must never fake one. |
| 312 | |
| 313 | pub async |
| 314 | |
| 315 | |
| 316 | |
| 317 | |
| 318 | |
| 319 | |
| 320 | |
| 321 | /// The admin behind this request. Anyone else is refused *here*, not |
| 322 | /// merely hidden from in the UI. |
| 323 | pub async |
| 324 | |
| 325 | |
| 326 | |
| 327 | |
| 328 | |
| 329 | |
| 330 | |
| 331 | |
| 332 | /// The service behind this request, by bearer key — the machine half of the |
| 333 | /// auth surface. A human browser's session cookie means nothing here and a |
| 334 | /// service key means nothing to [`current_user`]: the two never meet. A |
| 335 | /// disabled account reads as no key at all, the same way a disabled person |
| 336 | /// reads as signed out. |
| 337 | pub async |
| 338 | |
| 339 | |
| 340 | |
| 341 | |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | /// How long a freshly minted service key waits on the shelf for its one |
| 350 | /// showing: ten minutes, im's own ticket lifetime. |
| 351 | const SHOWN_TTL: Duration = from_secs; |
| 352 | |
| 353 | /// Where a freshly minted service key waits between its POST and the one |
| 354 | /// GET that shows it. The panel's write answers a 303 — the house idiom — |
| 355 | /// but the key itself must never ride a URL or a log line, so the query |
| 356 | /// carries only a random claim ticket: the shelf holds the plaintext, the |
| 357 | /// page's read takes it out ([`take_shown_secret`]), and a replayed or |
| 358 | /// reloaded URL finds the shelf empty and renders no key at all. A restart |
| 359 | /// drops the shelf — the admin mints another. |
| 360 | static SHOWN_SECRETS: = |
| 361 | new; |
| 362 | |
| 363 | /// Parks a fresh service key on the shelf and returns the claim ticket for |
| 364 | /// its URL. |
| 365 | |
| 366 | |
| 367 | |
| 368 | |
| 369 | |
| 370 | |
| 371 | |
| 372 | |
| 373 | /// Takes a stashed key out — exactly once; the second reader of the same |
| 374 | /// ticket gets nothing. |
| 375 | |
| 376 | |
| 377 | |
| 378 | |
| 379 | |
| 380 | |
| 381 | |
| 382 | |
| 383 | |
| 384 | |
| 385 | |
| 386 | |
| 387 | |
| 388 | |
| 389 | /// Everything a refused call is allowed to say. |
| 390 | /// |
| 391 | /// The codes are the contract's: a browser without script only ever sees |
| 392 | /// them through the address bar, so each one has a short word form |
| 393 | /// ([`Refusal::code`]) beside its sentence. |
| 394 | |
| 395 | |
| 396 | |
| 397 | |
| 398 | |
| 399 | |
| 400 | |
| 401 | |
| 402 | |
| 403 | |
| 404 | |
| 405 | |
| 406 | |
| 407 | |
| 408 | |
| 409 | |
| 410 | |
| 411 | |
| 412 | |
| 413 | |
| 414 | |
| 415 | |
| 416 | |
| 417 | |
| 418 | |
| 419 | |
| 420 | |
| 421 | |
| 422 | |
| 423 | |
| 424 | |
| 425 | |
| 426 | |
| 427 | |
| 428 | |
| 429 | |
| 430 | |
| 431 | |
| 432 | |
| 433 | |
| 434 | |
| 435 | |
| 436 | |
| 437 | |
| 438 | |
| 439 | |
| 440 | |
| 441 | |
| 442 | |
| 443 | |
| 444 | |
| 445 | |
| 446 | |
| 447 | |
| 448 | |
| 449 | |
| 450 | |
| 451 | |
| 452 | |
| 453 | |
| 454 | |
| 455 | |
| 456 | |
| 457 | |
| 458 | |
| 459 | |
| 460 | |
| 461 | |
| 462 | |
| 463 | |
| 464 | |
| 465 | |
| 466 | |
| 467 | |
| 468 | |
| 469 | |
| 470 | |
| 471 | |
| 472 | |
| 473 | |
| 474 | |
| 475 | |
| 476 | |
| 477 | |
| 478 | |
| 479 | |
| 480 | |
| 481 | |
| 482 | |
| 483 | |
| 484 | |
| 485 | |
| 486 | |
| 487 | |
| 488 | |
| 489 | |
| 490 | |
| 491 | |
| 492 | |
| 493 | |
| 494 | |
| 495 | |
| 496 | |
| 497 | |
| 498 | |
| 499 | |
| 500 | |
| 501 | |
| 502 | |
| 503 | |
| 504 | |
| 505 | |
| 506 | |
| 507 | |
| 508 | |
| 509 | |
| 510 | |
| 511 | |
| 512 | |
| 513 | |
| 514 | |
| 515 | |
| 516 | |
| 517 | |
| 518 | |
| 519 | |
| 520 | |
| 521 | |
| 522 | |
| 523 | |
| 524 | |
| 525 | |
| 526 | |
| 527 | |
| 528 | |
| 529 | |
| 530 | |
| 531 | |
| 532 | |
| 533 | |
| 534 | |
| 535 | |
| 536 | |
| 537 | |
| 538 | |
| 539 | |
| 540 | |
| 541 | |
| 542 | |
| 543 | |
| 544 | |
| 545 | |
| 546 | |
| 547 | |
| 548 | |
| 549 | |
| 550 | |
| 551 | |
| 552 | |
| 553 | |
| 554 | |
| 555 | |
| 556 | |
| 557 | |
| 558 | |
| 559 | |
| 560 | /// Which call a carried refusal belongs to. Two forms on one page must not both |
| 561 | /// claim the same sentence, so the redirect names the call and this is the name: |
| 562 | /// the last piece of the server function's path, with anything that is not a |
| 563 | /// plain word dropped so it can only ever be compared, never rendered. |
| 564 | |
| 565 | |
| 566 | |
| 567 | |
| 568 | |
| 569 | |
| 570 | |
| 571 | |
| 572 | |
| 573 | /// What a call refused with, however the browser without script carried it |
| 574 | /// back — read straight off the query, since there is no client-side action |
| 575 | /// value to check first the way a hydrated page in the old UI had. |
| 576 | /// |
| 577 | /// Every topcoat page is rendered server-side on every request, so there is |
| 578 | /// only ever the query to read. |
| 579 | |
| 580 | |
| 581 | |
| 582 | |
| 583 | |
| 584 | |
| 585 | |
| 586 | |
| 587 | |
| 588 | |
| 589 | |
| 590 | |
| 591 | |
| 592 | |
| 593 | |
| 594 | |
| 595 | |
| 596 | |
| 597 | |
| 598 | /// The page a form was posted from: the `Referer`, with the answer any |
| 599 | /// earlier post left on its query dropped, or `nowhere` when no `Referer` |
| 600 | /// came with the request. |
| 601 | /// |
| 602 | /// The feedback pairs — `refusal=`, `on=`, `why=`, `saved=` — are how a page |
| 603 | /// renders what the last post did, and `move=` opens the drive's one-shot |
| 604 | /// destination picker, which no post should return to. Sending the browser |
| 605 | /// on the query re-renders that old answer under the new post's own: a change |
| 606 | /// that succeeded right after one that was refused announces the refusal |
| 607 | /// again, and reads as having failed. The answer this redirect carries — the |
| 608 | /// pairs it was built with, or the body [`carry_refusal_on_redirect`] copies |
| 609 | /// onto the query — is the one that shows; an earlier one never survives the |
| 610 | /// trip. |
| 611 | |
| 612 | |
| 613 | |
| 614 | |
| 615 | |
| 616 | |
| 617 | |
| 618 | |
| 619 | |
| 620 | |
| 621 | |
| 622 | |
| 623 | |
| 624 | |
| 625 | |
| 626 | |
| 627 | |
| 628 | |
| 629 | |
| 630 | |
| 631 | |
| 632 | |
| 633 | |
| 634 | |
| 635 | |
| 636 | |
| 637 | |
| 638 | |
| 639 | |
| 640 | |
| 641 | /// Puts a refusal on the redirect a browser without script follows. |
| 642 | /// |
| 643 | /// A hydrated page reads the call's return value straight off the action. A |
| 644 | /// browser without script has no such thing: it posts the form, the server |
| 645 | /// function handler answers with a redirect back to the page it came from, and |
| 646 | /// the value — the whole refusal — sits in a body nobody will ever look at. |
| 647 | /// The click then looks like nothing happening, which is the worst answer |
| 648 | /// in can give. |
| 649 | /// |
| 650 | /// So the refusal is copied onto the `Location`, as `?refusal=<code>&on=<call>`, |
| 651 | /// and the page renders it from the query. This is one place rather than a |
| 652 | /// dozen because the shape is the same for every refusing call, present |
| 653 | /// and future: nothing here knows what any of them do. |
| 654 | /// |
| 655 | /// Requests carrying script are untouched — they are answered with the value |
| 656 | /// itself and never see a redirect. |
| 657 | /// |
| 658 | /// Ported from axum's `middleware::from_fn` onto a topcoat `#[layer]`. topcoat's |
| 659 | /// Post/Redirect/Get helper (`error::see_other`) answers with `303 See Other`, |
| 660 | /// not the `302 Found` axum's server-function redirect used, so the status this |
| 661 | /// checks for is `SEE_OTHER` — the guard's three conditions are otherwise |
| 662 | /// unchanged. |
| 663 | |
| 664 | async |
| 665 | |
| 666 | |
| 667 | |
| 668 | |
| 669 | |
| 670 | |
| 671 | |
| 672 | |
| 673 | |
| 674 | |
| 675 | |
| 676 | |
| 677 | |
| 678 | |
| 679 | |
| 680 | |
| 681 | |
| 682 | |
| 683 | |
| 684 | |
| 685 | |
| 686 | |
| 687 | |
| 688 | |
| 689 | |
| 690 | |
| 691 | |
| 692 | |
| 693 | |
| 694 | |
| 695 | |
| 696 | |
| 697 | |
| 698 | |
| 699 | |
| 700 | |
| 701 | |
| 702 | |
| 703 | |
| 704 | |
| 705 | |
| 706 | |
| 707 | |
| 708 | |
| 709 | |
| 710 | |
| 711 | |
| 712 | |
| 713 | |
| 714 | /// `location` with the refusal in its query. |
| 715 | /// |
| 716 | /// The redirect goes back to the page the form was posted from, and that page |
| 717 | /// may already carry a query — `?folder=<id>` is how a browser without script |
| 718 | /// opens a folder at all — so the two pairs are merged in, and the pair from |
| 719 | /// any earlier refusal is dropped rather than stacked on top of. |
| 720 | |
| 721 | |
| 722 | |
| 723 | |
| 724 | |
| 725 | |
| 726 | |
| 727 | |
| 728 | |
| 729 | |
| 730 | |
| 731 | |
| 732 | |
| 733 | |
| 734 | |
| 735 | |
| 736 | |
| 737 | |
| 738 | |
| 739 | |
| 740 | |
| 741 | |
| 742 | |
| 743 | |
| 744 | |
| 745 | /// The path and query of `location`, with scheme and authority dropped. A |
| 746 | /// protocol-relative address (`//elsewhere.example/`) is another host wearing a |
| 747 | /// path's clothes, and a browser reads a backslash there as a slash, so both |
| 748 | /// are answered with the drive rather than trusted. |
| 749 | |
| 750 | |
| 751 | |
| 752 | |
| 753 | |
| 754 | |
| 755 | |
| 756 | |
| 757 | |
| 758 | |
| 759 | |
| 760 | |
| 761 | |
| 762 | |
| 763 | |
| 764 | |
| 765 | /// The cache directives a response cannot choose for itself. |
| 766 | /// |
| 767 | /// HTML is revalidated on every load: a page the browser kept from before a |
| 768 | /// deploy is the old app answering under the new one's address, which is how a |
| 769 | /// fixed bug once came back after shipping. So every response whose body is a |
| 770 | /// document gets `no-cache`. Responses that already carry a directive keep it — |
| 771 | /// topcoat stamps the fingerprinted assets under `/_topcoat/assets` with a year |
| 772 | /// of `immutable` and the live stream with `no-cache`, and the file and |
| 773 | /// thumbnail handlers stamp their own — and everything else (redirects, JSON |
| 774 | /// answers) ships none, the idiom they already ride on: no directive, nothing |
| 775 | /// cached. One layer at `/`, because the decision is about what the bytes are, |
| 776 | /// not which route built them. |
| 777 | |
| 778 | async |
| 779 | |
| 780 | |
| 781 | |
| 782 | |
| 783 | |
| 784 | |
| 785 | |
| 786 | |
| 787 | |
| 788 | |
| 789 | |
| 790 | |
| 791 | |
| 792 | |
| 793 | |
| 794 | |
| 795 | |
| 796 | |
| 797 | |
| 798 | |
| 799 | |
| 800 | |
| 801 | |
| 802 | |
| 803 | |
| 804 | |
| 805 | |
| 806 | |
| 807 | |
| 808 | |
| 809 | |
| 810 | |
| 811 | |
| 812 | |
| 813 | |
| 814 | |
| 815 | |
| 816 | |
| 817 | |
| 818 | |
| 819 | |
| 820 | |
| 821 | |
| 822 | |
| 823 | |
| 824 | |
| 825 | |
| 826 | |
| 827 | |
| 828 | |
| 829 | |
| 830 | |
| 831 | |
| 832 | |
| 833 | |
| 834 | |
| 835 | |
| 836 | |
| 837 | |
| 838 | |
| 839 | |
| 840 | |
| 841 | |
| 842 | |
| 843 | |
| 844 | |
| 845 | |
| 846 | |
| 847 | |
| 848 | |
| 849 | |
| 850 |