╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ web.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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