╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ web.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2905 2906 var storage: [4096]u8 = undefined; 2907 var reader = file.reader(self.io, &storage); 2908 const bytes = reader.interface.allocRemaining(allocator, .limited(512 * 1024)) catch return null; 2909 return std.json.parseFromSliceLeaky(ui.Session.FormFeedback, allocator, bytes, .{}) catch null; 2910 } 2911 2912 // resolve a cookie's token to its user_id. returns false (logged out) for a 2913 // missing, malformed, or unknown token. 2914 pub fn lookup(self: SessionStore, token_hex: []const u8, out: *[evt.event_id_size]u8) bool { 2915 if (!isToken(token_hex)) return false; 2916 const file = self.dir.openFile(self.io, token_hex, .{ .mode = .read_only }) catch return false; 2917 defer file.close(self.io); 2918 var storage: [evt.event_id_size]u8 = undefined; 2919 var file_reader = file.reader(self.io, &storage); 2920 file_reader.interface.readSliceAll(out) catch return false; 2921 return true; 2922 } 2923 2924 // revoke a session. a no-op for a missing or malformed token. 2925 pub fn remove(self: SessionStore, token_hex: []const u8) void { 2926 if (!isToken(token_hex)) return; 2927 self.dir.deleteFile(self.io, token_hex) catch {}; 2928 } 2929 2930 // leave `token_hex`'s session for visitors arriving without one. only the 2931 // `try` fixture writes one. the name can't collide with a session token. 2932 pub fn offerAutoLogin(self: SessionStore, token_hex: *const [token_hex_len]u8) !void { 2933 const file = try self.dir.createFile(self.io, auto_login_name, .{}); 2934 defer file.close(self.io); 2935 try file.writeStreamingAll(self.io, token_hex); 2936 } 2937 2938 pub fn autoLogin(self: SessionStore, out: *[token_hex_len]u8) bool { 2939 out.* = self.auto_login orelse return false; 2940 return true; 2941 } 2942 2943 const auto_login_name = "auto-login"; 2944 2945 // a token is exactly token_hex_len hex chars. validating before using the 2946 // value as a path keeps attacker-supplied cookies from escaping the 2947 // sessions dir (e.g. via "/" or ".." bytes). 2948 fn isToken(token_hex: []const u8) bool { 2949 if (token_hex.len != token_hex_len) return false; 2950 for (token_hex) |c| if (!std.ascii.isHex(c)) return false; 2951 return true; 2952 } 2953 }; 2954