╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ web.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
777 self.dir.close(self.io); 778 } 779 780 // mint a new session for user_id, returning the token hex to set as the 781 // cookie value. 782 pub fn create(self: SessionStore, user_id: *const [evt.event_id_size]u8) ![token_hex_len]u8 { 783 var token: [evt.event_id_size]u8 = undefined; 784 self.io.random(&token); 785 const token_hex = std.fmt.bytesToHex(token, .lower); 786 const file = try self.dir.createFile(self.io, &token_hex, .{}); 787 defer file.close(self.io); 788 try file.writeStreamingAll(self.io, user_id); 789 return token_hex; 790 } 791 792 // resolve a cookie's token to its user_id. returns false (logged out) for a 793 // missing, malformed, or unknown token. 794 pub fn lookup(self: SessionStore, token_hex: []const u8, out: *[evt.event_id_size]u8) bool { 795 if (!isToken(token_hex)) return false; 796 const file = self.dir.openFile(self.io, token_hex, .{ .mode = .read_only }) catch return false; 797 defer file.close(self.io); 798 var storage: [evt.event_id_size]u8 = undefined; 799 var file_reader = file.reader(self.io, &storage); 800 file_reader.interface.readSliceAll(out) catch return false; 801 return true; 802 } 803 804 // revoke a session. a no-op for a missing or malformed token. 805 pub fn remove(self: SessionStore, token_hex: []const u8) void { 806 if (!isToken(token_hex)) return; 807 self.dir.deleteFile(self.io, token_hex) catch {}; 808 } 809 810 // a token is exactly token_hex_len hex chars. validating before using the 811 // value as a path keeps attacker-supplied cookies from escaping the 812 // sessions dir (e.g. via "/" or ".." bytes). 813 fn isToken(token_hex: []const u8) bool { 814 if (token_hex.len != token_hex_len) return false; 815 for (token_hex) |c| if (!std.ascii.isHex(c)) return false; 816 return true; 817 } 818 }; 819