in
Clone
https://git.tahinli.com/Tahinli/in.git
0 watching0 forksRustAGPL-3.0528.7 KiB9215377Tahinli27 days ago37 commits
| 1 | //! Who the viewer of a request is, and the checks that gate pages and forms. |
| 2 | //! |
| 3 | //! A signed-in viewer has a local session: a cookie with a random token, of which the database keeps only |
| 4 | //! the SHA-256 (conventions section 5). Every check reads the store, never `im`, so pages keep working |
| 5 | //! while `im` is down (contracts section 1.1). This module also owns the redirect target check, |
| 6 | //! [`safe_next`], which every return path uses. |
| 7 | //! |
| 8 | //! The helpers here are called by the sign-in pages, the logout form, the photo proxy and the home |
| 9 | //! page. The pages that come next (drive, admin) use [`require_member`], [`require_admin`] and |
| 10 | //! [`require_csrf`] too. |
| 11 | |
| 12 | use Duration; |
| 13 | |
| 14 | use Member; |
| 15 | use Session; |
| 16 | use stamp_before; |
| 17 | use ; |
| 18 | use Result; |
| 19 | use ; |
| 20 | use ; |
| 21 | use ; |
| 22 | use uri; |
| 23 | |
| 24 | use cratepercent_encode; |
| 25 | use crateAppState; |
| 26 | |
| 27 | /// Cookie of the local session. The `__Host-` prefix pins it to this origin, to path `/`, and to HTTPS. |
| 28 | pub const SESSION_COOKIE: &str = "__Host-in_session"; |
| 29 | |
| 30 | /// Cookie of a sign-in in progress: the sealed state, nonce, PKCE verifier and `next` target. |
| 31 | pub const FLIGHT_COOKIE: &str = "__Host-in_flight"; |
| 32 | |
| 33 | /// Absolute age of a local session. Contracts section 2.4 caps it at 12 hours. |
| 34 | pub const SESSION_TTL: Duration = from_secs; |
| 35 | |
| 36 | /// How long a sign-in may take from the redirect to `im` to the callback. |
| 37 | pub const FLIGHT_TTL: Duration = from_secs; |
| 38 | |
| 39 | /// Longest `next` target accepted. Longer ones are refused rather than cut. |
| 40 | pub const MAX_NEXT_BYTES: usize = 2048; |
| 41 | |
| 42 | /// How often `last_seen_at` is written. Writing it on every page view would make every read a write. |
| 43 | const TOUCH_EVERY: Duration = from_secs; |
| 44 | |
| 45 | /// A request's signed-in person: the session that was presented and the member it belongs to. |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | /// Returns `candidate` when it is a safe place to redirect to, and `None` otherwise. |
| 54 | /// |
| 55 | /// A safe target is a single absolute path on this origin: it starts with one `/`, the next character is |
| 56 | /// not `/` or `\` (browsers read `/\evil` as `//evil`), it has no control characters (no line breaks to |
| 57 | /// inject headers) and no backslash anywhere, and it is at most [`MAX_NEXT_BYTES`] long. Anything that |
| 58 | /// is not a path, such as a scheme, a percent-encoded slash or an empty string, is refused. |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | /// The redirect target for an optional `next` value: the value when it is safe, else the start page. |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | /// The sign-in page URL that returns to `next` after sign-in. |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | /// Sets a cookie that only this origin's root path can read, sent over HTTPS, and hidden from scripts. |
| 87 | /// `SameSite=Lax` lets it arrive on the redirect back from `im`. A zero `ttl` deletes the cookie. |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | /// Reads a cookie's value, when it is present. |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | /// The local session that the request presents, when it is still live. The store filters out expired |
| 108 | /// sessions and sessions of disabled members. Touches `last_seen_at` at most every [`TOUCH_EVERY`]. |
| 109 | pub async |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | /// The signed-in person of the request, if any. |
| 128 | pub async |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | /// The signed-in person, or a redirect to sign-in that returns to this request's path and query. |
| 143 | pub async |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | /// A signed-in administrator. A signed-in member who is not one gets `404`, so the admin pages do not |
| 156 | /// reveal that they exist. |
| 157 | pub async |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | /// Whether `submitted` is the CSRF token of `session`. The comparison is constant-time, and an empty |
| 166 | /// stored token never matches, not even an empty submission. |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | /// The signed-in administrator, after the CSRF check of an admin form. A member who is not an administrator |
| 173 | /// gets `404` before the token is looked at, so the admin forms do not reveal themselves either. |
| 174 | pub async |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | /// The signed-in person, after the CSRF check of a state-changing form. A missing session redirects to |
| 183 | /// sign-in; a wrong or missing token is `403`. Run it before any side effect. |
| 184 | pub async |
| 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 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | /// Helpers for the in-process router tests: a member with a live session. |
| 240 | |
| 241 | pub |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | |
| 276 | |
| 277 | |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 |