im
Clone
https://git.tahinli.com/Tahinli/im.git
0 watching0 forksRustAGPL-3.0542.8 KiBb9f0726Tahinli27 days ago34 commits
| 1 | //! Sessions and the signed-in person: the session cookie, the guards every page calls, the CSRF |
| 2 | //! checks, the token that protects pre-session forms, the sealed pending-login cookie between the |
| 3 | //! password and the second factor, and the guard on every `next` redirect. |
| 4 | //! |
| 5 | //! The browser holds the session token in `__Host-im_session`. The database holds only its SHA-256, |
| 6 | //! so a leaked row cannot be replayed as a cookie. Every guard here answers before any side effect, |
| 7 | //! and a state-changing handler calls [`require_csrf`] or [`check_pre_csrf`] first. |
| 8 | |
| 9 | use ; |
| 10 | |
| 11 | use ; |
| 12 | use secret; |
| 13 | use Store; |
| 14 | use EndReason; |
| 15 | use ; |
| 16 | use ; |
| 17 | use totp; |
| 18 | use Result; |
| 19 | use ; |
| 20 | use ; |
| 21 | use ; |
| 22 | use ; |
| 23 | |
| 24 | use crateclient_addr; |
| 25 | use crateTopic; |
| 26 | use crateAppState; |
| 27 | |
| 28 | /// Logical name of the session cookie. `override_prefix_host()` stores it as `__Host-im_session`. |
| 29 | pub const SESSION_COOKIE: &str = "im_session"; |
| 30 | |
| 31 | /// Seconds in a day, for the policy's day counts. |
| 32 | const DAY: u64 = 24 * 60 * 60; |
| 33 | |
| 34 | /// Logical name of the pre-session CSRF cookie, used by the pages that run before a session exists. |
| 35 | pub const PRE_CSRF_COOKIE: &str = "im_precsrf"; |
| 36 | |
| 37 | /// How long the pre-session CSRF token stays valid in the browser. |
| 38 | const PRE_CSRF_TTL: Duration = from_secs; |
| 39 | |
| 40 | /// Logical name of the pending-login cookie, set between the password and the second factor. |
| 41 | pub const PENDING_COOKIE: &str = "im_pending"; |
| 42 | |
| 43 | /// How long a password-verified sign-in may wait for its second factor. Five minutes is long enough |
| 44 | /// to fetch a code from an app, and short enough that a stolen cookie is of little use. |
| 45 | pub const PENDING_TTL: Duration = from_secs; |
| 46 | |
| 47 | /// The longest `next` value accepted, in bytes. |
| 48 | const MAX_NEXT_LEN: usize = 2048; |
| 49 | |
| 50 | /// The signed-in person and the session they signed in with. |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | /// The cookie jar that forces the `__Host-` prefix: `Secure`, `Path=/` and no `Domain`. |
| 60 | + '_ |
| 61 | |
| 62 | |
| 63 | |
| 64 | /// The session the request presented, if it is still live. |
| 65 | async |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | /// The signed-in person, or `None` when there is no live session. Refreshes `last_seen` (at most |
| 76 | /// once in five minutes; the store decides). |
| 77 | pub async |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | /// The signed-in person. Anyone else is sent to `/login`, with the page they asked for as `next`. |
| 94 | pub async |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | /// The signed-in administrator. A visitor without a session, and a person who is not an administrator, |
| 106 | /// get 404, the same answer as a page that does not exist. An administrator without two-factor sign-in, while the policy requires |
| 107 | /// it, is sent to the security page instead. |
| 108 | pub async |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | /// The signed-in person, when the submitted `csrf` field matches their session. Returns 403 for a |
| 125 | /// wrong or missing token. Call it first in every state-changing handler. |
| 126 | pub async |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | /// The signed-in administrator, when the submitted `csrf` field matches their session. A member gets 404 |
| 136 | /// first, before the token is looked at, so a form of the admin area gives no hint to a member. |
| 137 | pub async |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | /// True only when `submitted` is the session's CSRF token, compared in constant time. An empty |
| 147 | /// stored token never matches, not even an empty submission. |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | /// Creates a session for `user`, authenticated by `amr`, and sets the cookie. Returns `false`, with no |
| 154 | /// session and no cookie, when `decided_on` no longer holds for the user (see |
| 155 | /// [`im_core::store::Store::create_session`]). The caller then treats the sign-in as failed. |
| 156 | /// |
| 157 | /// A session the browser already presented is ended first, whoever it belongs to, so a sign-in always |
| 158 | /// rotates the session id and no row is left live with no cookie holding it. The session lives |
| 159 | /// [`SESSION_IDLE`] after its last use, up to the policy's `session_max_days` from now; the store keeps |
| 160 | /// both limits. The per-person cap of the policy is applied inside the store, in the same write. |
| 161 | pub async |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | |
| 220 | /// Ends the session of `viewer` (with its back-channel logouts) and clears its cookie. Other sessions of |
| 221 | /// the person are untouched. |
| 222 | pub async |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | /// The pre-session CSRF token. Login, setup and the second-factor page use it, because no session |
| 234 | /// exists yet. An existing cookie is reused, so two open tabs both keep working. |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | /// True only when the submitted `precsrf` equals the pre-session cookie. Call it before any other |
| 248 | /// work in a pre-session POST. |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | /// Removes the pre-session CSRF cookie after a successful sign-in or setup. |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | /// A password-verified sign-in that waits for its second factor. |
| 274 | |
| 275 | |
| 276 | |
| 277 | |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | |
| 286 | /// Prints every field, except that the nonce prints as `<redacted>`: spending it signs the person in. |
| 287 | |
| 288 | |
| 289 | |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | /// Parks a password-verified sign-in in the pending cookie. The payload is sealed with the instance |
| 299 | /// key, so the browser can neither change the person nor extend the expiry. It carries a random nonce, |
| 300 | /// which a successful second factor spends, so the same cookie cannot sign in twice. |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | |
| 308 | |
| 309 | |
| 310 | |
| 311 | |
| 312 | |
| 313 | |
| 314 | |
| 315 | |
| 316 | |
| 317 | |
| 318 | |
| 319 | /// The pending sign-in, or `None` when the cookie is missing, tampered with, malformed or expired. |
| 320 | /// `next` is the field as it was set; the caller applies [`safe_next`]. |
| 321 | |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | |
| 327 | |
| 328 | |
| 329 | |
| 330 | |
| 331 | |
| 332 | |
| 333 | |
| 334 | |
| 335 | |
| 336 | |
| 337 | |
| 338 | |
| 339 | |
| 340 | |
| 341 | |
| 342 | |
| 343 | /// Removes the pending-login cookie. |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | /// Logical name of the one-time secret cookie. It carries a client secret from the POST that made the secret |
| 349 | /// to the GET that shows it, so the secret never sits in a response to the POST or in a URL. |
| 350 | pub const SECRET_FLASH_COOKIE: &str = "im_secret"; |
| 351 | |
| 352 | /// How long a one-time secret waits in its cookie. Long enough for the redirect to be followed; a secret that |
| 353 | /// is not shown in that time is not kept. |
| 354 | pub const SECRET_FLASH_TTL: Duration = from_secs; |
| 355 | |
| 356 | /// Parks a client secret for the page that shows it once. The payload is sealed with the instance key, names |
| 357 | /// the client it belongs to, and expires after [`SECRET_FLASH_TTL`]. Same cookie attributes as the pending |
| 358 | /// sign-in: `__Host-`, Secure, HttpOnly, SameSite=Strict, Path=/. |
| 359 | |
| 360 | |
| 361 | |
| 362 | |
| 363 | |
| 364 | |
| 365 | |
| 366 | |
| 367 | |
| 368 | |
| 369 | |
| 370 | |
| 371 | |
| 372 | |
| 373 | |
| 374 | |
| 375 | |
| 376 | |
| 377 | |
| 378 | |
| 379 | /// Takes the secret parked for `client_id`. The cookie is removed whether or not it matches, so a secret is |
| 380 | /// shown at most once. Returns `None` when the cookie is missing, tampered with, expired or for another client. |
| 381 | |
| 382 | |
| 383 | |
| 384 | |
| 385 | |
| 386 | |
| 387 | |
| 388 | |
| 389 | |
| 390 | |
| 391 | |
| 392 | |
| 393 | /// Removes the one-time secret cookie. |
| 394 | |
| 395 | |
| 396 | |
| 397 | |
| 398 | /// Checks one six-digit TOTP code for `user`, with the replay guard. True only when the code is |
| 399 | /// valid and its step was not used before. Returns false when two-factor sign-in is off. |
| 400 | pub async |
| 401 | |
| 402 | |
| 403 | |
| 404 | |
| 405 | |
| 406 | |
| 407 | |
| 408 | |
| 409 | |
| 410 | |
| 411 | |
| 412 | |
| 413 | |
| 414 | |
| 415 | |
| 416 | |
| 417 | /// Checks a second factor: six digits are a TOTP code, anything else is a recovery code, which is |
| 418 | /// spent on success. Returns the authentication method that was proved, or `None`. |
| 419 | pub async |
| 420 | |
| 421 | |
| 422 | |
| 423 | |
| 424 | |
| 425 | |
| 426 | |
| 427 | |
| 428 | |
| 429 | |
| 430 | |
| 431 | |
| 432 | |
| 433 | |
| 434 | |
| 435 | |
| 436 | |
| 437 | |
| 438 | |
| 439 | /// Where a person goes after signing in. An administrator without two-factor sign-in goes to the |
| 440 | /// security page to enrol it, while the policy requires that. Anyone else goes to `next`. |
| 441 | |
| 442 | |
| 443 | |
| 444 | |
| 445 | |
| 446 | |
| 447 | |
| 448 | |
| 449 | /// Returns `next` only when it is a same-site path: one leading `/`, then no `/` or `\` that would |
| 450 | /// make a browser treat it as another host, no control characters, at most 2048 bytes. Anything else |
| 451 | /// is `/`. |
| 452 | |
| 453 | |
| 454 | |
| 455 | |
| 456 | |
| 457 | |
| 458 | |
| 459 | |
| 460 | |
| 461 | |
| 462 | |
| 463 | |
| 464 | |
| 465 | |
| 466 | |
| 467 | /// Percent-encodes everything except RFC 3986 unreserved characters and `/`. |
| 468 | |
| 469 | |
| 470 | |
| 471 | |
| 472 | |
| 473 | |
| 474 | |
| 475 | |
| 476 | |
| 477 | |
| 478 | |
| 479 | |
| 480 | |
| 481 | /// The `User-Agent` of the request, truncated to 512 characters. |
| 482 | |
| 483 | |
| 484 | |
| 485 | |
| 486 | |
| 487 | |
| 488 | |
| 489 | /// Seconds since the Unix epoch. A clock before the epoch reads as 0, which only makes every |
| 490 | /// pending sign-in expire at once. |
| 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 | |
| 561 | |
| 562 | |
| 563 | |
| 564 | |
| 565 | |
| 566 | |
| 567 | |
| 568 | |
| 569 | |
| 570 | |
| 571 | |
| 572 | |
| 573 | |
| 574 | |
| 575 | |
| 576 | |
| 577 | |
| 578 | |
| 579 | |
| 580 | |
| 581 | |
| 582 | |
| 583 |