╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ web.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭ find file ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ ╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │ │ │http│ ssh │ │ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
3133 } 3134 3135 fn takeFlash(self: SessionStore, allocator: std.mem.Allocator, token_hex: []const u8) ?ui.Session.FormFeedback { 3136 if (!isToken(token_hex)) return null; 3137 const file = self.flash_dir.openFile(self.io, token_hex, .{ .mode = .read_only }) catch return null; 3138 defer self.flash_dir.deleteFile(self.io, token_hex) catch {}; 3139 defer file.close(self.io); 3140 3141 var storage: [4096]u8 = undefined; 3142 var reader = file.reader(self.io, &storage); 3143 const bytes = reader.interface.allocRemaining(allocator, .limited(512 * 1024)) catch return null; 3144 return std.json.parseFromSliceLeaky(ui.Session.FormFeedback, allocator, bytes, .{}) catch null; 3145 } 3146 3147 // resolve a cookie's token to its user_id. returns false (logged out) for a 3148 // missing, malformed, or unknown token. 3149 pub fn lookup(self: SessionStore, token_hex: []const u8, out: *[evt.event_id_size]u8) bool { 3150 if (!isToken(token_hex)) return false; 3151 const file = self.dir.openFile(self.io, token_hex, .{ .mode = .read_only }) catch return false; 3152 defer file.close(self.io); 3153 var storage: [evt.event_id_size]u8 = undefined; 3154 var file_reader = file.reader(self.io, &storage); 3155 file_reader.interface.readSliceAll(out) catch return false; 3156 return true; 3157 } 3158 3159 // revoke a session. a no-op for a missing or malformed token. 3160 pub fn remove(self: SessionStore, token_hex: []const u8) void { 3161 if (!isToken(token_hex)) return; 3162 self.dir.deleteFile(self.io, token_hex) catch {}; 3163 } 3164 3165 // leave `token_hex`'s session for visitors arriving without one. only the 3166 // `try` fixture writes one. the name can't collide with a session token. 3167 pub fn offerAutoLogin(self: SessionStore, token_hex: *const [token_hex_len]u8) !void { 3168 const file = try self.dir.createFile(self.io, auto_login_name, .{}); 3169 defer file.close(self.io); 3170 try file.writeStreamingAll(self.io, token_hex); 3171 } 3172 3173 pub fn autoLogin(self: SessionStore, out: *[token_hex_len]u8) bool { 3174 out.* = self.auto_login orelse return false; 3175 return true; 3176 } 3177 3178 const auto_login_name = "auto-login"; 3179 3180 // a token is exactly token_hex_len hex chars. validating before using the 3181 // value as a path keeps attacker-supplied cookies from escaping the 3182 // sessions dir (e.g. via "/" or ".." bytes). 3183 fn isToken(token_hex: []const u8) bool { 3184 if (token_hex.len != token_hex_len) return false; 3185 for (token_hex) |c| if (!std.ascii.isHex(c)) return false; 3186 return true; 3187 } 3188 }; 3189