╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ web.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
2958 // missing, malformed, or unknown token. 2959 pub fn lookup(self: SessionStore, token_hex: []const u8, out: *[evt.event_id_size]u8) bool { 2960 if (!isToken(token_hex)) return false; 2961 const file = self.dir.openFile(self.io, token_hex, .{ .mode = .read_only }) catch return false; 2962 defer file.close(self.io); 2963 var storage: [evt.event_id_size]u8 = undefined; 2964 var file_reader = file.reader(self.io, &storage); 2965 file_reader.interface.readSliceAll(out) catch return false; 2966 return true; 2967 } 2968 2969 // revoke a session. a no-op for a missing or malformed token. 2970 pub fn remove(self: SessionStore, token_hex: []const u8) void { 2971 if (!isToken(token_hex)) return; 2972 self.dir.deleteFile(self.io, token_hex) catch {}; 2973 } 2974 2975 // leave `token_hex`'s session for visitors arriving without one. only the 2976 // `try` fixture writes one. the name can't collide with a session token. 2977 pub fn offerAutoLogin(self: SessionStore, token_hex: *const [token_hex_len]u8) !void { 2978 const file = try self.dir.createFile(self.io, auto_login_name, .{}); 2979 defer file.close(self.io); 2980 try file.writeStreamingAll(self.io, token_hex); 2981 } 2982 2983 pub fn autoLogin(self: SessionStore, out: *[token_hex_len]u8) bool { 2984 out.* = self.auto_login orelse return false; 2985 return true; 2986 } 2987 2988 const auto_login_name = "auto-login"; 2989 2990 // a token is exactly token_hex_len hex chars. validating before using the 2991 // value as a path keeps attacker-supplied cookies from escaping the 2992 // sessions dir (e.g. via "/" or ".." bytes). 2993 fn isToken(token_hex: []const u8) bool { 2994 if (token_hex.len != token_hex_len) return false; 2995 for (token_hex) |c| if (!std.ascii.isHex(c)) return false; 2996 return true; 2997 } 2998 }; 2999