╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ web.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
721 } 722 return null; 723 } 724 725 fn decodeFormValue(allocator: std.mem.Allocator, encoded: []const u8) ![]u8 { 726 var out: std.ArrayList(u8) = .empty; 727 errdefer out.deinit(allocator); 728 var i: usize = 0; 729 while (i < encoded.len) : (i += 1) { 730 switch (encoded[i]) { 731 '+' => try out.append(allocator, ' '), 732 '%' => { 733 if (i + 2 >= encoded.len) return error.InvalidPercentEncoding; 734 const hi = std.fmt.charToDigit(encoded[i + 1], 16) catch return error.InvalidPercentEncoding; 735 const lo = std.fmt.charToDigit(encoded[i + 2], 16) catch return error.InvalidPercentEncoding; 736 try out.append(allocator, hi * 16 + lo); 737 i += 2; 738 }, 739 else => try out.append(allocator, encoded[i]), 740 } 741 } 742 return try out.toOwnedSlice(allocator); 743 } 744 745 // disk-backed web session store. each login mints a random, opaque token 746 // (not the user's id) that is stored as a file named by the token hex, whose 747 // contents are the raw user_id bytes. authenticating a request is a lookup of 748 // the cookie's token; logout deletes the file, revoking the session. there is 749 // deliberately no expiry — a session lives until logout. 750 pub const SessionStore = struct { 751 io: std.Io, 752 dir: std.Io.Dir, 753 754 pub const token_hex_len = evt.event_id_size * 2; 755 756 pub fn init(io: std.Io, data_dir: std.Io.Dir) !SessionStore { 757 try data_dir.createDirPath(io, "sessions"); 758 const dir = try data_dir.openDir(io, "sessions", .{}); 759 return .{ .io = io, .dir = dir }; 760 } 761 762 pub fn deinit(self: SessionStore) void { 763 self.dir.close(self.io); 764 } 765 766 // mint a new session for user_id, returning the token hex to set as the 767 // cookie value. 768 pub fn create(self: SessionStore, user_id: *const [evt.event_id_size]u8) ![token_hex_len]u8 { 769 var token: [evt.event_id_size]u8 = undefined; 770 self.io.random(&token); 771 const token_hex = std.fmt.bytesToHex(token, .lower); 772 const file = try self.dir.createFile(self.io, &token_hex, .{}); 773 defer file.close(self.io); 774 try file.writeStreamingAll(self.io, user_id); 775 return token_hex; 776 } 777 778 // resolve a cookie's token to its user_id. returns false (logged out) for a 779 // missing, malformed, or unknown token. 780 pub fn lookup(self: SessionStore, token_hex: []const u8, out: *[evt.event_id_size]u8) bool { 781 if (!isToken(token_hex)) return false; 782 const file = self.dir.openFile(self.io, token_hex, .{ .mode = .read_only }) catch return false; 783 defer file.close(self.io); 784 var storage: [evt.event_id_size]u8 = undefined; 785 var file_reader = file.reader(self.io, &storage); 786 file_reader.interface.readSliceAll(out) catch return false; 787 return true; 788 } 789 790 // revoke a session. a no-op for a missing or malformed token. 791 pub fn remove(self: SessionStore, token_hex: []const u8) void { 792 if (!isToken(token_hex)) return; 793 self.dir.deleteFile(self.io, token_hex) catch {}; 794 } 795 796 // a token is exactly token_hex_len hex chars. validating before using the 797 // value as a path keeps attacker-supplied cookies from escaping the 798 // sessions dir (e.g. via "/" or ".." bytes). 799 fn isToken(token_hex: []const u8) bool { 800 if (token_hex.len != token_hex_len) return false; 801 for (token_hex) |c| if (!std.ascii.isHex(c)) return false; 802 return true; 803 } 804 }; 805