╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ web.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2540 // disk-backed web session store. each login mints a random, opaque token 2541 // (not the user's id) that is stored as a file named by the token hex, whose 2542 // contents are the raw user_id bytes. authenticating a request is a lookup of 2543 // the cookie's token; logout deletes the file, revoking the session. there is 2544 // deliberately no expiry — a session lives until logout. form failures use 2545 // files in the flash subdirectory that the redirected GET consumes. the 2546 // directory is cleared whenever the server starts. 2547 pub const SessionStore = struct { 2548 io: std.Io, 2549 dir: std.Io.Dir, 2550 flash_dir: std.Io.Dir, 2551 auto_login: ?[token_hex_len]u8, 2552 2553 pub const token_hex_len = evt.event_id_size * 2; 2554 2555 pub fn init(io: std.Io, data_dir: std.Io.Dir) !SessionStore { 2556 try data_dir.createDirPath(io, "sessions"); 2557 const dir = try data_dir.openDir(io, "sessions", .{}); 2558 errdefer dir.close(io); 2559 try dir.deleteTree(io, "flash"); 2560 try dir.createDirPath(io, "flash"); 2561 const flash_dir = try dir.openDir(io, "flash", .{}); 2562 errdefer flash_dir.close(io); 2563 const auto_login = blk: { 2564 const file = dir.openFile(io, auto_login_name, .{ .mode = .read_only }) catch break :blk null; 2565 defer file.close(io); 2566 var token: [token_hex_len]u8 = undefined; 2567 var storage: [token_hex_len]u8 = undefined; 2568 var reader = file.reader(io, &storage); 2569 reader.interface.readSliceAll(&token) catch break :blk null; 2570 break :blk token; 2571 }; 2572 return .{ .io = io, .dir = dir, .flash_dir = flash_dir, .auto_login = auto_login }; 2573 } 2574 2575 pub fn deinit(self: SessionStore) void { 2576 self.flash_dir.close(self.io); 2577 self.dir.close(self.io); 2578 } 2579 2580 // mint a new session for user_id, returning the token hex to set as the 2581 // cookie value. 2582 pub fn create(self: SessionStore, user_id: *const [evt.event_id_size]u8) ![token_hex_len]u8 { 2583 var token: [evt.event_id_size]u8 = undefined; 2584 self.io.random(&token); 2585 const token_hex = std.fmt.bytesToHex(token, .lower); 2586 const file = try self.dir.createFile(self.io, &token_hex, .{}); 2587 defer file.close(self.io); 2588 try file.writeStreamingAll(self.io, user_id); 2589 return token_hex; 2590 } 2591 2592 fn createFlash(self: SessionStore, allocator: std.mem.Allocator, value: ui.Session.FormFeedback) ![token_hex_len]u8 { 2593 var token: [evt.event_id_size]u8 = undefined; 2594 self.io.random(&token); 2595 const token_hex = std.fmt.bytesToHex(token, .lower); 2596 const file = try self.flash_dir.createFile(self.io, &token_hex, .{}); 2597 errdefer self.flash_dir.deleteFile(self.io, &token_hex) catch {}; 2598 defer file.close(self.io); 2599 2600 var json: std.Io.Writer.Allocating = .init(allocator); 2601 defer json.deinit(); 2602 try std.json.Stringify.value(value, .{}, &json.writer); 2603 try file.writeStreamingAll(self.io, json.written()); 2604 return token_hex; 2605 } 2606 2607 fn takeFlash(self: SessionStore, allocator: std.mem.Allocator, token_hex: []const u8) ?ui.Session.FormFeedback { 2608 if (!isToken(token_hex)) return null; 2609 const file = self.flash_dir.openFile(self.io, token_hex, .{ .mode = .read_only }) catch return null; 2610 defer self.flash_dir.deleteFile(self.io, token_hex) catch {}; 2611 defer file.close(self.io); 2612 2613 var storage: [4096]u8 = undefined; 2614 var reader = file.reader(self.io, &storage); 2615 const bytes = reader.interface.allocRemaining(allocator, .limited(512 * 1024)) catch return null; 2616 return std.json.parseFromSliceLeaky(ui.Session.FormFeedback, allocator, bytes, .{}) catch null; 2617 } 2618 2619 // resolve a cookie's token to its user_id. returns false (logged out) for a 2620 // missing, malformed, or unknown token. 2621 pub fn lookup(self: SessionStore, token_hex: []const u8, out: *[evt.event_id_size]u8) bool { 2622 if (!isToken(token_hex)) return false; 2623 const file = self.dir.openFile(self.io, token_hex, .{ .mode = .read_only }) catch return false; 2624 defer file.close(self.io); 2625 var storage: [evt.event_id_size]u8 = undefined; 2626 var file_reader = file.reader(self.io, &storage); 2627 file_reader.interface.readSliceAll(out) catch return false; 2628 return true; 2629 } 2630 2631 // revoke a session. a no-op for a missing or malformed token. 2632 pub fn remove(self: SessionStore, token_hex: []const u8) void { 2633 if (!isToken(token_hex)) return; 2634 self.dir.deleteFile(self.io, token_hex) catch {}; 2635 } 2636 2637 // leave `token_hex`'s session for visitors arriving without one. only the 2638 // `try` fixture writes one. the name can't collide with a session token. 2639 pub fn offerAutoLogin(self: SessionStore, token_hex: *const [token_hex_len]u8) !void { 2640 const file = try self.dir.createFile(self.io, auto_login_name, .{}); 2641 defer file.close(self.io); 2642 try file.writeStreamingAll(self.io, token_hex); 2643 } 2644 2645 pub fn autoLogin(self: SessionStore, out: *[token_hex_len]u8) bool { 2646 out.* = self.auto_login orelse return false; 2647 return true; 2648 } 2649 2650 const auto_login_name = "auto-login"; 2651 2652 // a token is exactly token_hex_len hex chars. validating before using the 2653 // value as a path keeps attacker-supplied cookies from escaping the 2654 // sessions dir (e.g. via "/" or ".." bytes). 2655 fn isToken(token_hex: []const u8) bool { 2656 if (token_hex.len != token_hex_len) return false; 2657 for (token_hex) |c| if (!std.ascii.isHex(c)) return false; 2658 return true; 2659 } 2660 }; 2661