╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ web.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2288 // mint a new session for user_id, returning the token hex to set as the 2289 // cookie value. 2290 pub fn create(self: SessionStore, user_id: *const [evt.event_id_size]u8) ![token_hex_len]u8 { 2291 var token: [evt.event_id_size]u8 = undefined; 2292 self.io.random(&token); 2293 const token_hex = std.fmt.bytesToHex(token, .lower); 2294 const file = try self.dir.createFile(self.io, &token_hex, .{}); 2295 defer file.close(self.io); 2296 try file.writeStreamingAll(self.io, user_id); 2297 return token_hex; 2298 } 2299 2300 fn createFlash(self: SessionStore, allocator: std.mem.Allocator, value: ui.Session.FormFeedback) ![token_hex_len]u8 { 2301 var token: [evt.event_id_size]u8 = undefined; 2302 self.io.random(&token); 2303 const token_hex = std.fmt.bytesToHex(token, .lower); 2304 const file = try self.flash_dir.createFile(self.io, &token_hex, .{}); 2305 errdefer self.flash_dir.deleteFile(self.io, &token_hex) catch {}; 2306 defer file.close(self.io); 2307 2308 var json: std.Io.Writer.Allocating = .init(allocator); 2309 defer json.deinit(); 2310 try std.json.Stringify.value(value, .{}, &json.writer); 2311 try file.writeStreamingAll(self.io, json.written()); 2312 return token_hex; 2313 } 2314 2315 fn takeFlash(self: SessionStore, allocator: std.mem.Allocator, token_hex: []const u8) ?ui.Session.FormFeedback { 2316 if (!isToken(token_hex)) return null; 2317 const file = self.flash_dir.openFile(self.io, token_hex, .{ .mode = .read_only }) catch return null; 2318 defer self.flash_dir.deleteFile(self.io, token_hex) catch {}; 2319 defer file.close(self.io); 2320 2321 var storage: [4096]u8 = undefined; 2322 var reader = file.reader(self.io, &storage); 2323 const bytes = reader.interface.allocRemaining(allocator, .limited(512 * 1024)) catch return null; 2324 return std.json.parseFromSliceLeaky(ui.Session.FormFeedback, allocator, bytes, .{}) catch null; 2325 } 2326 2327 // resolve a cookie's token to its user_id. returns false (logged out) for a 2328 // missing, malformed, or unknown token. 2329 pub fn lookup(self: SessionStore, token_hex: []const u8, out: *[evt.event_id_size]u8) bool { 2330 if (!isToken(token_hex)) return false; 2331 const file = self.dir.openFile(self.io, token_hex, .{ .mode = .read_only }) catch return false; 2332 defer file.close(self.io); 2333 var storage: [evt.event_id_size]u8 = undefined; 2334 var file_reader = file.reader(self.io, &storage); 2335 file_reader.interface.readSliceAll(out) catch return false; 2336 return true; 2337 } 2338 2339 // revoke a session. a no-op for a missing or malformed token. 2340 pub fn remove(self: SessionStore, token_hex: []const u8) void { 2341 if (!isToken(token_hex)) return; 2342 self.dir.deleteFile(self.io, token_hex) catch {}; 2343 } 2344 2345 // leave `token_hex`'s session for visitors arriving without one. only the 2346 // `try` fixture writes one. the name can't collide with a session token. 2347 pub fn offerAutoLogin(self: SessionStore, token_hex: *const [token_hex_len]u8) !void { 2348 const file = try self.dir.createFile(self.io, auto_login_name, .{}); 2349 defer file.close(self.io); 2350 try file.writeStreamingAll(self.io, token_hex); 2351 } 2352 2353 pub fn autoLogin(self: SessionStore, out: *[token_hex_len]u8) bool { 2354 out.* = self.auto_login orelse return false; 2355 return true; 2356 } 2357 2358 const auto_login_name = "auto-login"; 2359 2360 // a token is exactly token_hex_len hex chars. validating before using the 2361 // value as a path keeps attacker-supplied cookies from escaping the 2362 // sessions dir (e.g. via "/" or ".." bytes). 2363 fn isToken(token_hex: []const u8) bool { 2364 if (token_hex.len != token_hex_len) return false; 2365 for (token_hex) |c| if (!std.ascii.isHex(c)) return false; 2366 return true; 2367 } 2368 }; 2369