╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ web.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2255 const flash_dir = try dir.openDir(io, "flash", .{}); 2256 errdefer flash_dir.close(io); 2257 const auto_login = blk: { 2258 const file = dir.openFile(io, auto_login_name, .{ .mode = .read_only }) catch break :blk null; 2259 defer file.close(io); 2260 var token: [token_hex_len]u8 = undefined; 2261 var storage: [token_hex_len]u8 = undefined; 2262 var reader = file.reader(io, &storage); 2263 reader.interface.readSliceAll(&token) catch break :blk null; 2264 break :blk token; 2265 }; 2266 return .{ .io = io, .dir = dir, .flash_dir = flash_dir, .auto_login = auto_login }; 2267 } 2268 2269 pub fn deinit(self: SessionStore) void { 2270 self.flash_dir.close(self.io); 2271 self.dir.close(self.io); 2272 } 2273 2274 // mint a new session for user_id, returning the token hex to set as the 2275 // cookie value. 2276 pub fn create(self: SessionStore, user_id: *const [evt.event_id_size]u8) ![token_hex_len]u8 { 2277 var token: [evt.event_id_size]u8 = undefined; 2278 self.io.random(&token); 2279 const token_hex = std.fmt.bytesToHex(token, .lower); 2280 const file = try self.dir.createFile(self.io, &token_hex, .{}); 2281 defer file.close(self.io); 2282 try file.writeStreamingAll(self.io, user_id); 2283 return token_hex; 2284 } 2285 2286 fn createFlash(self: SessionStore, allocator: std.mem.Allocator, value: ui.Session.FormFeedback) ![token_hex_len]u8 { 2287 var token: [evt.event_id_size]u8 = undefined; 2288 self.io.random(&token); 2289 const token_hex = std.fmt.bytesToHex(token, .lower); 2290 const file = try self.flash_dir.createFile(self.io, &token_hex, .{}); 2291 errdefer self.flash_dir.deleteFile(self.io, &token_hex) catch {}; 2292 defer file.close(self.io); 2293 2294 var json: std.Io.Writer.Allocating = .init(allocator); 2295 defer json.deinit(); 2296 try std.json.Stringify.value(value, .{}, &json.writer); 2297 try file.writeStreamingAll(self.io, json.written()); 2298 return token_hex; 2299 } 2300 2301 fn takeFlash(self: SessionStore, allocator: std.mem.Allocator, token_hex: []const u8) ?ui.Session.FormFeedback { 2302 if (!isToken(token_hex)) return null; 2303 const file = self.flash_dir.openFile(self.io, token_hex, .{ .mode = .read_only }) catch return null; 2304 defer self.flash_dir.deleteFile(self.io, token_hex) catch {}; 2305 defer file.close(self.io); 2306 2307 var storage: [4096]u8 = undefined; 2308 var reader = file.reader(self.io, &storage); 2309 const bytes = reader.interface.allocRemaining(allocator, .limited(512 * 1024)) catch return null; 2310 return std.json.parseFromSliceLeaky(ui.Session.FormFeedback, allocator, bytes, .{}) catch null; 2311 } 2312 2313 // resolve a cookie's token to its user_id. returns false (logged out) for a 2314 // missing, malformed, or unknown token. 2315 pub fn lookup(self: SessionStore, token_hex: []const u8, out: *[evt.event_id_size]u8) bool { 2316 if (!isToken(token_hex)) return false; 2317 const file = self.dir.openFile(self.io, token_hex, .{ .mode = .read_only }) catch return false; 2318 defer file.close(self.io); 2319 var storage: [evt.event_id_size]u8 = undefined; 2320 var file_reader = file.reader(self.io, &storage); 2321 file_reader.interface.readSliceAll(out) catch return false; 2322 return true; 2323 } 2324 2325 // revoke a session. a no-op for a missing or malformed token. 2326 pub fn remove(self: SessionStore, token_hex: []const u8) void { 2327 if (!isToken(token_hex)) return; 2328 self.dir.deleteFile(self.io, token_hex) catch {}; 2329 } 2330 2331 // leave `token_hex`'s session for visitors arriving without one. only the 2332 // `try` fixture writes one. the name can't collide with a session token. 2333 pub fn offerAutoLogin(self: SessionStore, token_hex: *const [token_hex_len]u8) !void { 2334 const file = try self.dir.createFile(self.io, auto_login_name, .{}); 2335 defer file.close(self.io); 2336 try file.writeStreamingAll(self.io, token_hex); 2337 } 2338 2339 pub fn autoLogin(self: SessionStore, out: *[token_hex_len]u8) bool { 2340 out.* = self.auto_login orelse return false; 2341 return true; 2342 } 2343 2344 const auto_login_name = "auto-login"; 2345 2346 // a token is exactly token_hex_len hex chars. validating before using the 2347 // value as a path keeps attacker-supplied cookies from escaping the 2348 // sessions dir (e.g. via "/" or ".." bytes). 2349 fn isToken(token_hex: []const u8) bool { 2350 if (token_hex.len != token_hex_len) return false; 2351 for (token_hex) |c| if (!std.ascii.isHex(c)) return false; 2352 return true; 2353 } 2354 }; 2355