╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ web.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2662 } 2663 2664 // resolve a cookie's token to its user_id. returns false (logged out) for a 2665 // missing, malformed, or unknown token. 2666 pub fn lookup(self: SessionStore, token_hex: []const u8, out: *[evt.event_id_size]u8) bool { 2667 if (!isToken(token_hex)) return false; 2668 const file = self.dir.openFile(self.io, token_hex, .{ .mode = .read_only }) catch return false; 2669 defer file.close(self.io); 2670 var storage: [evt.event_id_size]u8 = undefined; 2671 var file_reader = file.reader(self.io, &storage); 2672 file_reader.interface.readSliceAll(out) catch return false; 2673 return true; 2674 } 2675 2676 // revoke a session. a no-op for a missing or malformed token. 2677 pub fn remove(self: SessionStore, token_hex: []const u8) void { 2678 if (!isToken(token_hex)) return; 2679 self.dir.deleteFile(self.io, token_hex) catch {}; 2680 } 2681 2682 // leave `token_hex`'s session for visitors arriving without one. only the 2683 // `try` fixture writes one. the name can't collide with a session token. 2684 pub fn offerAutoLogin(self: SessionStore, token_hex: *const [token_hex_len]u8) !void { 2685 const file = try self.dir.createFile(self.io, auto_login_name, .{}); 2686 defer file.close(self.io); 2687 try file.writeStreamingAll(self.io, token_hex); 2688 } 2689 2690 pub fn autoLogin(self: SessionStore, out: *[token_hex_len]u8) bool { 2691 out.* = self.auto_login orelse return false; 2692 return true; 2693 } 2694 2695 const auto_login_name = "auto-login"; 2696 2697 // a token is exactly token_hex_len hex chars. validating before using the 2698 // value as a path keeps attacker-supplied cookies from escaping the 2699 // sessions dir (e.g. via "/" or ".." bytes). 2700 fn isToken(token_hex: []const u8) bool { 2701 if (token_hex.len != token_hex_len) return false; 2702 for (token_hex) |c| if (!std.ascii.isHex(c)) return false; 2703 return true; 2704 } 2705 }; 2706