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