╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ web.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
800 801 // disk-backed web session store. each login mints a random, opaque token 802 // (not the user's id) that is stored as a file named by the token hex, whose 803 // contents are the raw user_id bytes. authenticating a request is a lookup of 804 // the cookie's token; logout deletes the file, revoking the session. there is 805 // deliberately no expiry — a session lives until logout. 806 pub const SessionStore = struct { 807 io: std.Io, 808 dir: std.Io.Dir, 809 810 pub const token_hex_len = evt.event_id_size * 2; 811 812 pub fn init(io: std.Io, data_dir: std.Io.Dir) !SessionStore { 813 try data_dir.createDirPath(io, "sessions"); 814 const dir = try data_dir.openDir(io, "sessions", .{}); 815 return .{ .io = io, .dir = dir }; 816 } 817 818 pub fn deinit(self: SessionStore) void { 819 self.dir.close(self.io); 820 } 821 822 // mint a new session for user_id, returning the token hex to set as the 823 // cookie value. 824 pub fn create(self: SessionStore, user_id: *const [evt.event_id_size]u8) ![token_hex_len]u8 { 825 var token: [evt.event_id_size]u8 = undefined; 826 self.io.random(&token); 827 const token_hex = std.fmt.bytesToHex(token, .lower); 828 const file = try self.dir.createFile(self.io, &token_hex, .{}); 829 defer file.close(self.io); 830 try file.writeStreamingAll(self.io, user_id); 831 return token_hex; 832 } 833 834 // resolve a cookie's token to its user_id. returns false (logged out) for a 835 // missing, malformed, or unknown token. 836 pub fn lookup(self: SessionStore, token_hex: []const u8, out: *[evt.event_id_size]u8) bool { 837 if (!isToken(token_hex)) return false; 838 const file = self.dir.openFile(self.io, token_hex, .{ .mode = .read_only }) catch return false; 839 defer file.close(self.io); 840 var storage: [evt.event_id_size]u8 = undefined; 841 var file_reader = file.reader(self.io, &storage); 842 file_reader.interface.readSliceAll(out) catch return false; 843 return true; 844 } 845 846 // revoke a session. a no-op for a missing or malformed token. 847 pub fn remove(self: SessionStore, token_hex: []const u8) void { 848 if (!isToken(token_hex)) return; 849 self.dir.deleteFile(self.io, token_hex) catch {}; 850 } 851 852 // a token is exactly token_hex_len hex chars. validating before using the 853 // value as a path keeps attacker-supplied cookies from escaping the 854 // sessions dir (e.g. via "/" or ".." bytes). 855 fn isToken(token_hex: []const u8) bool { 856 if (token_hex.len != token_hex_len) return false; 857 for (token_hex) |c| if (!std.ascii.isHex(c)) return false; 858 return true; 859 } 860 }; 861