im_old
Clone
https://git.tahinli.com/Tahinli/im_old.git
0 watching0 forksRustGPL-3.0755.6 KiB84318dcTahinli2 months ago77 commits
| 1 | //! Shared request plumbing: the app context, the two cookies (`im_session` |
| 2 | //! for a completed login, `im_pending` for the ten minutes between password |
| 3 | //! and second factor), and the refusal codes a redirect carries back to the |
| 4 | //! page that posted the form. |
| 5 | //! |
| 6 | //! Every form post answers a plain 303 whose query names the refusal, and |
| 7 | //! the page reads it back on render. The soft-nav script replays posts over |
| 8 | //! fetch with `accept: text/html`, so the 303's target document is what |
| 9 | //! comes back — no refusal-carrying layer as in İzlek is needed. |
| 10 | |
| 11 | use HashMap; |
| 12 | use ; |
| 13 | use ; |
| 14 | |
| 15 | use User; |
| 16 | use Store; |
| 17 | use ; |
| 18 | use ; |
| 19 | use client_ip; |
| 20 | |
| 21 | use crateConfig; |
| 22 | |
| 23 | pub const SESSION_COOKIE: &str = "im_session"; |
| 24 | pub const PENDING_COOKIE: &str = "im_pending"; |
| 25 | |
| 26 | /// The pending marker's lifetime: long enough to find the authenticator, |
| 27 | /// short enough to not be a session. |
| 28 | pub const PENDING_MINUTES: i64 = 10; |
| 29 | |
| 30 | /// One proxy hop in front of the process. `im` is reached through that hop, |
| 31 | /// so `client_ip` reads the address it appended rather than the hop's own. |
| 32 | /// A direct connection, with no hop in front, is its own address. |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | /// A stable-enough label for the client. |
| 38 | /// |
| 39 | /// [`client_ip`] under [`trusted_proxies`]: the address the one trusted hop |
| 40 | /// reported, or the peer when the request did not come through it. `unknown` |
| 41 | /// when neither is there. A client-supplied `x-forwarded-for` is not read |
| 42 | /// unless that hop appended it. |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | /// What travels the live channel. `Tick` is the panel's "something moved — |
| 61 | /// re-read"; `Profile` is the directory's own news: this member's row, as it |
| 62 | /// now stands, serialized exactly as `/directory` would have answered it; |
| 63 | /// `Revoked` is a sign-out in motion: this user's session(s) just died, the |
| 64 | /// one named by hash when the revocation was session-targeted. |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | /// Announce that the panel's data moved. Sends are lossy on purpose: nobody |
| 76 | /// listening is not an error, and a lagging tab gets a resync tick. |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | /// Announce that one member's row changed. Re-reads the row — the callers |
| 82 | /// hold the pre-write copy — and broadcasts it as the member `/directory` |
| 83 | /// would answer. A row that no longer resolves (deleted) has no member to |
| 84 | /// announce; the next full pass is where a removal surfaces. |
| 85 | pub async |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | /// Announce that `user`'s sessions died — one of them when `session_hash` |
| 95 | /// names a session, all of them when it does not. Every write path that ends |
| 96 | /// sessions speaks here, so the dead sessions' open tabs hear their own |
| 97 | /// eviction on the live channel and leave without waiting out the stream |
| 98 | /// window. Sent right after the write commits and before the tick, so a |
| 99 | /// connection that swallows the tick has already missed nothing. |
| 100 | pub async |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | /// Log the event, then tick the live channel — the two travel together so |
| 108 | /// the Logs page (and any watching panel) catches up without a reload. |
| 109 | pub async |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | // --------------------------------------------------------------------------- |
| 115 | // The show-once shelf |
| 116 | // --------------------------------------------------------------------------- |
| 117 | |
| 118 | /// How long a stashed secret waits for its one reader. |
| 119 | const SHOWN_TTL: Duration = from_secs; |
| 120 | |
| 121 | /// Where a freshly minted client secret waits between its POST and the one |
| 122 | /// GET that shows it. The panel's write answers a 303 — the house idiom — |
| 123 | /// but the secret itself must never ride a URL or a log line, so the query |
| 124 | /// carries only a random claim ticket: the shelf holds the plaintext, the |
| 125 | /// page's read takes it out (`take_shown_secret`), and a replayed or |
| 126 | /// reloaded URL finds the shelf empty and renders no secret at all. A |
| 127 | /// restart drops the shelf — the admin mints another, as with the CLI. |
| 128 | type ShownShelf = ; |
| 129 | static SHOWN_SECRETS: = new; |
| 130 | |
| 131 | /// Parks a client's fresh pair on the shelf and returns the claim ticket |
| 132 | /// for its URL. The id travels too — it is the public half of the pair and |
| 133 | /// the one render shows it beside the secret. |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | /// Takes a stashed pair out — exactly once; the second reader of the same |
| 143 | /// ticket gets nothing. |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | /// The application cookie jar, with the attributes every im cookie wants. |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | /// The session cookie value this request presented, if it presented one. |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | /// Writes the session cookie. `HttpOnly` so script cannot read it, `Secure` |
| 179 | /// (on https issuers) so it never crosses plain HTTP, `SameSite=Lax` so |
| 180 | /// another site's form cannot post with it — and so the top-level redirect |
| 181 | /// back from `/authorize` still carries it. |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | pub async |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | /// Removes the session cookie from this browser. The server-side revocation |
| 206 | /// is what actually ends the session; this only tidies the client. |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | /// The person behind this request, resolved through the central session. |
| 216 | pub async |
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | // --------------------------------------------------------------------------- |
| 225 | // The pending marker: between password and TOTP |
| 226 | // --------------------------------------------------------------------------- |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | pub async |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | /// Opens the pending cookie this request presented. `None` for absent, |
| 265 | /// forged, or expired — all three mean "start the login over". |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | |
| 276 | /// Whether this request carries a registered OIDC app's credentials: HTTP |
| 277 | /// Basic over `client_id:client_secret`, checked against the client |
| 278 | /// registry. Anything unparseable, unknown, or wrong is simply false — the |
| 279 | /// caller answers its one refusal and never says which. |
| 280 | pub async |
| 281 | |
| 282 | |
| 283 | |
| 284 | /// The same check, answering *which* app it is: the routes that write on a |
| 285 | /// caller's behalf (`/family/register`) need the client id the pair |
| 286 | /// authenticated, and the ones that only read do not. |
| 287 | pub async |
| 288 | |
| 289 | |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | |
| 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 |