╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ web.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2671 // deliberately no expiry — a session lives until logout. form failures use 2672 // files in the flash subdirectory that the redirected GET consumes. the 2673 // directory is cleared whenever the server starts. 2674 pub const SessionStore = struct { 2675 io: std.Io, 2676 dir: std.Io.Dir, 2677 flash_dir: std.Io.Dir, 2678 auto_login: ?[token_hex_len]u8, 2679 2680 pub const token_hex_len = evt.event_id_size * 2; 2681 2682 pub fn init(io: std.Io, data_dir: std.Io.Dir) !SessionStore { 2683 try data_dir.createDirPath(io, "sessions"); 2684 const dir = try data_dir.openDir(io, "sessions", .{}); 2685 errdefer dir.close(io); 2686 try dir.deleteTree(io, "flash"); 2687 try dir.createDirPath(io, "flash"); 2688 const flash_dir = try dir.openDir(io, "flash", .{}); 2689 errdefer flash_dir.close(io); 2690 const auto_login = blk: { 2691 const file = dir.openFile(io, auto_login_name, .{ .mode = .read_only }) catch break :blk null; 2692 defer file.close(io); 2693 var token: [token_hex_len]u8 = undefined; 2694 var storage: [token_hex_len]u8 = undefined; 2695 var reader = file.reader(io, &storage); 2696 reader.interface.readSliceAll(&token) catch break :blk null; 2697 break :blk token; 2698 }; 2699 return .{ .io = io, .dir = dir, .flash_dir = flash_dir, .auto_login = auto_login }; 2700 } 2701 2702 pub fn deinit(self: SessionStore) void { 2703 self.flash_dir.close(self.io); 2704 self.dir.close(self.io); 2705 } 2706 2707 // mint a new session for user_id, returning the token hex to set as the 2708 // cookie value. 2709 pub fn create(self: SessionStore, user_id: *const [evt.event_id_size]u8) ![token_hex_len]u8 { 2710 var token: [evt.event_id_size]u8 = undefined; 2711 self.io.random(&token); 2712 const token_hex = std.fmt.bytesToHex(token, .lower); 2713 const file = try self.dir.createFile(self.io, &token_hex, .{}); 2714 defer file.close(self.io); 2715 try file.writeStreamingAll(self.io, user_id); 2716 return token_hex; 2717 } 2718 2719 fn createFlash(self: SessionStore, allocator: std.mem.Allocator, value: ui.Session.FormFeedback) ![token_hex_len]u8 { 2720 var token: [evt.event_id_size]u8 = undefined; 2721 self.io.random(&token); 2722 const token_hex = std.fmt.bytesToHex(token, .lower); 2723 const file = try self.flash_dir.createFile(self.io, &token_hex, .{}); 2724 errdefer self.flash_dir.deleteFile(self.io, &token_hex) catch {}; 2725 defer file.close(self.io); 2726 2727 var json: std.Io.Writer.Allocating = .init(allocator); 2728 defer json.deinit(); 2729 try std.json.Stringify.value(value, .{}, &json.writer); 2730 try file.writeStreamingAll(self.io, json.written()); 2731 return token_hex; 2732 } 2733 2734 fn takeFlash(self: SessionStore, allocator: std.mem.Allocator, token_hex: []const u8) ?ui.Session.FormFeedback { 2735 if (!isToken(token_hex)) return null; 2736 const file = self.flash_dir.openFile(self.io, token_hex, .{ .mode = .read_only }) catch return null; 2737 defer self.flash_dir.deleteFile(self.io, token_hex) catch {}; 2738 defer file.close(self.io); 2739 2740 var storage: [4096]u8 = undefined; 2741 var reader = file.reader(self.io, &storage); 2742 const bytes = reader.interface.allocRemaining(allocator, .limited(512 * 1024)) catch return null; 2743 return std.json.parseFromSliceLeaky(ui.Session.FormFeedback, allocator, bytes, .{}) catch null; 2744 } 2745 2746 // resolve a cookie's token to its user_id. returns false (logged out) for a 2747 // missing, malformed, or unknown token. 2748 pub fn lookup(self: SessionStore, token_hex: []const u8, out: *[evt.event_id_size]u8) bool { 2749 if (!isToken(token_hex)) return false; 2750 const file = self.dir.openFile(self.io, token_hex, .{ .mode = .read_only }) catch return false; 2751 defer file.close(self.io); 2752 var storage: [evt.event_id_size]u8 = undefined; 2753 var file_reader = file.reader(self.io, &storage); 2754 file_reader.interface.readSliceAll(out) catch return false; 2755 return true; 2756 } 2757 2758 // revoke a session. a no-op for a missing or malformed token. 2759 pub fn remove(self: SessionStore, token_hex: []const u8) void { 2760 if (!isToken(token_hex)) return; 2761 self.dir.deleteFile(self.io, token_hex) catch {}; 2762 } 2763 2764 // leave `token_hex`'s session for visitors arriving without one. only the 2765 // `try` fixture writes one. the name can't collide with a session token. 2766 pub fn offerAutoLogin(self: SessionStore, token_hex: *const [token_hex_len]u8) !void { 2767 const file = try self.dir.createFile(self.io, auto_login_name, .{}); 2768 defer file.close(self.io); 2769 try file.writeStreamingAll(self.io, token_hex); 2770 } 2771 2772 pub fn autoLogin(self: SessionStore, out: *[token_hex_len]u8) bool { 2773 out.* = self.auto_login orelse return false; 2774 return true; 2775 } 2776 2777 const auto_login_name = "auto-login"; 2778 2779 // a token is exactly token_hex_len hex chars. validating before using the 2780 // value as a path keeps attacker-supplied cookies from escaping the 2781 // sessions dir (e.g. via "/" or ".." bytes). 2782 fn isToken(token_hex: []const u8) bool { 2783 if (token_hex.len != token_hex_len) return false; 2784 for (token_hex) |c| if (!std.ascii.isHex(c)) return false; 2785 return true; 2786 } 2787 }; 2788