iz
Clone
https://git.tahinli.com/Tahinli/iz.git
0 watching0 forksRustAGPL-3.0567.3 KiB3950aceTahinli27 days ago34 commits
| 1 | //! Sessions and access checks for the web layer: the cookies, the current member, the role and CSRF |
| 2 | //! checks that every handler calls first, and the `next` guard for redirects. |
| 3 | //! |
| 4 | //! The browser holds the raw session token in `__Host-iz_session`. The store keeps only its SHA-256 |
| 5 | //! (`iz_core::store::sessions`). The sign-in flight (state, nonce, PKCE verifier, `next`) lives in |
| 6 | //! `__Host-iz_flight`, sealed with the instance key under the `sign-in-flight` purpose, so the browser cannot change it. |
| 7 | |
| 8 | use fmt; |
| 9 | use Duration; |
| 10 | |
| 11 | use ; |
| 12 | use secret; |
| 13 | use Store; |
| 14 | use Member; |
| 15 | use SessionRecord; |
| 16 | use constant_time_eq; |
| 17 | use Url; |
| 18 | use ; |
| 19 | use Result; |
| 20 | use ; |
| 21 | use ; |
| 22 | use ; |
| 23 | use ; |
| 24 | |
| 25 | use crateunix_now; |
| 26 | use crateAppState; |
| 27 | |
| 28 | /// Logical name of the session cookie. `override_prefix_host()` stores it as `__Host-iz_session`. |
| 29 | pub const SESSION_COOKIE: &str = "iz_session"; |
| 30 | /// Logical name of the sign-in flight cookie. Stored as `__Host-iz_flight`. |
| 31 | pub const STATE_COOKIE: &str = "iz_flight"; |
| 32 | /// Lifetime of a local session, in seconds of absolute age (contracts.md 2.4: at most 12 hours). |
| 33 | pub const SESSION_LIFETIME: Duration = from_secs; |
| 34 | /// How long a sign-in may take from the start to the callback (contracts.md 2.3 and this task). |
| 35 | pub const STATE_LIFETIME: Duration = from_secs; |
| 36 | /// Longest `next` target accepted, in bytes. Reason: a return path is a path, not a document. |
| 37 | const MAX_NEXT_LEN: usize = 2048; |
| 38 | /// Longest user agent stored with a session, in bytes. Reason: the value is only shown to admins. |
| 39 | const MAX_USER_AGENT_LEN: usize = 256; |
| 40 | |
| 41 | /// The level a page or a write needs. Maps to one authorization action. The administrator level is |
| 42 | /// added with the admin pages. |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | /// A signed-in member and the session that carries them. |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | /// The state of one sign-in, sealed into the flight cookie. |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | // Hand-written so that a debug print never shows the state, the nonce or the PKCE verifier. |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | /// The cookie jar with the `__Host-` prefix, so the browser enforces `Secure`, `Path=/` and no domain. |
| 110 | + '_ |
| 111 | |
| 112 | |
| 113 | |
| 114 | /// The signed-in member, or `None` when there is no session, the session has expired, or its member |
| 115 | /// is disabled or has no role. The store answers all of these the same way. |
| 116 | pub async |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | /// The value of the session cookie, or an empty string when there is none. A live stream keeps it, so |
| 124 | /// it can check the session again after the request has ended. |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | /// The member of a session cookie `token`, under the same rules as [`current_member`]. |
| 133 | pub async |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | /// Whether the session `token` still belongs to a member who may read the board. A live stream asks |
| 144 | /// this before each event, so a signed-out or disabled viewer stops receiving them. |
| 145 | pub async |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | /// The signed-in member. Without a session, redirects to `/login?next=<this page>`. |
| 153 | pub async |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | /// The signed-in member with the level `need`. A viewer or member who tries a write gets `403`; a |
| 165 | /// member with no access, or a disabled one, gets `404`, so the page does not reveal that it exists. |
| 166 | /// Administrator pages answer `404` to everyone who is not an administrator, anonymous visitors |
| 167 | /// included: an admin page does not reveal that it exists, so it never redirects to sign-in. |
| 168 | pub async |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | /// The signed-in member with the level `need`, after the CSRF check. The level is checked first, so a |
| 191 | /// viewer gets `403` whatever token they send. A missing or wrong `csrf` is `403`. Both run before any |
| 192 | /// side effect of the handler. |
| 193 | pub async |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | /// Stores a new session cookie. `token` is the value from [`store::sessions::IssuedSession`]. |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | /// Removes the session cookie. |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | /// Stores the sealed sign-in flight. `SameSite=Lax` is needed: the callback is a top-level |
| 223 | /// navigation that comes back from `im`. |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | /// The sealed sign-in flight cookie, if the browser sent one. |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | /// Removes the sign-in flight cookie. Called on every callback, whatever the outcome, so a flight |
| 244 | /// can be used only once. |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | /// Seals a sign-in flight for the cookie. |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | /// Opens a sealed sign-in flight. `None` when it is malformed, tampered with, or past its expiry. |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | /// The User-Agent of the request, shortened, for the session row. |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | /// The raw query string of the request, without the `?`. `None` when the request has none. |
| 272 | |
| 273 | |
| 274 | |
| 275 | |
| 276 | /// One query parameter of the request, decoded. `None` when it is absent. |
| 277 | |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | /// The return path, if it is safe to redirect to; otherwise `/`. A safe path starts with one `/`, |
| 286 | /// is ASCII without control characters, and is at most 2048 bytes. Anything that could name another |
| 287 | /// host (`//`, `/\`, a scheme) is refused. |
| 288 | |
| 289 | |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | /// Percent-encodes everything except unreserved characters and `/`, for a `next` value in a URL. |
| 304 | |
| 305 | |
| 306 | |
| 307 | |
| 308 | |
| 309 | |
| 310 | |
| 311 | |
| 312 | |
| 313 | |
| 314 | |
| 315 | |
| 316 | |
| 317 | |
| 318 | |
| 319 | |
| 320 | |
| 321 | |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | |
| 327 | |
| 328 | |
| 329 | |
| 330 | |
| 331 | |
| 332 | |
| 333 | |
| 334 | |
| 335 | |
| 336 | |
| 337 | |
| 338 | |
| 339 | |
| 340 | |
| 341 | |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | |
| 350 | |
| 351 | |
| 352 | |
| 353 | |
| 354 | |
| 355 | |
| 356 | |
| 357 | |
| 358 | |
| 359 | |
| 360 | |
| 361 | |
| 362 | |
| 363 | |
| 364 | |
| 365 | |
| 366 | |
| 367 | |
| 368 | |
| 369 | |
| 370 | |
| 371 | |
| 372 | |
| 373 | |
| 374 | |
| 375 | |
| 376 |