╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ web.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2622 if (!isToken(token_hex)) return false; 2623 const file = self.dir.openFile(self.io, token_hex, .{ .mode = .read_only }) catch return false; 2624 defer file.close(self.io); 2625 var storage: [evt.event_id_size]u8 = undefined; 2626 var file_reader = file.reader(self.io, &storage); 2627 file_reader.interface.readSliceAll(out) catch return false; 2628 return true; 2629 } 2630 2631 // revoke a session. a no-op for a missing or malformed token. 2632 pub fn remove(self: SessionStore, token_hex: []const u8) void { 2633 if (!isToken(token_hex)) return; 2634 self.dir.deleteFile(self.io, token_hex) catch {}; 2635 } 2636 2637 // leave `token_hex`'s session for visitors arriving without one. only the 2638 // `try` fixture writes one. the name can't collide with a session token. 2639 pub fn offerAutoLogin(self: SessionStore, token_hex: *const [token_hex_len]u8) !void { 2640 const file = try self.dir.createFile(self.io, auto_login_name, .{}); 2641 defer file.close(self.io); 2642 try file.writeStreamingAll(self.io, token_hex); 2643 } 2644 2645 pub fn autoLogin(self: SessionStore, out: *[token_hex_len]u8) bool { 2646 out.* = self.auto_login orelse return false; 2647 return true; 2648 } 2649 2650 const auto_login_name = "auto-login"; 2651 2652 // a token is exactly token_hex_len hex chars. validating before using the 2653 // value as a path keeps attacker-supplied cookies from escaping the 2654 // sessions dir (e.g. via "/" or ".." bytes). 2655 fn isToken(token_hex: []const u8) bool { 2656 if (token_hex.len != token_hex_len) return false; 2657 for (token_hex) |c| if (!std.ascii.isHex(c)) return false; 2658 return true; 2659 } 2660 }; 2661