╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ web.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2448 2449 // disk-backed web session store. each login mints a random, opaque token 2450 // (not the user's id) that is stored as a file named by the token hex, whose 2451 // contents are the raw user_id bytes. authenticating a request is a lookup of 2452 // the cookie's token; logout deletes the file, revoking the session. there is 2453 // deliberately no expiry — a session lives until logout. form failures use 2454 // files in the flash subdirectory that the redirected GET consumes. the 2455 // directory is cleared whenever the server starts. 2456 pub const SessionStore = struct { 2457 io: std.Io, 2458 dir: std.Io.Dir, 2459 flash_dir: std.Io.Dir, 2460 auto_login: ?[token_hex_len]u8, 2461 2462 pub const token_hex_len = evt.event_id_size * 2; 2463 2464 pub fn init(io: std.Io, data_dir: std.Io.Dir) !SessionStore { 2465 try data_dir.createDirPath(io, "sessions"); 2466 const dir = try data_dir.openDir(io, "sessions", .{}); 2467 errdefer dir.close(io); 2468 try dir.deleteTree(io, "flash"); 2469 try dir.createDirPath(io, "flash"); 2470 const flash_dir = try dir.openDir(io, "flash", .{}); 2471 errdefer flash_dir.close(io); 2472 const auto_login = blk: { 2473 const file = dir.openFile(io, auto_login_name, .{ .mode = .read_only }) catch break :blk null; 2474 defer file.close(io); 2475 var token: [token_hex_len]u8 = undefined; 2476 var storage: [token_hex_len]u8 = undefined; 2477 var reader = file.reader(io, &storage); 2478 reader.interface.readSliceAll(&token) catch break :blk null; 2479 break :blk token; 2480 }; 2481 return .{ .io = io, .dir = dir, .flash_dir = flash_dir, .auto_login = auto_login }; 2482 } 2483 2484 pub fn deinit(self: SessionStore) void { 2485 self.flash_dir.close(self.io); 2486 self.dir.close(self.io); 2487 } 2488 2489 // mint a new session for user_id, returning the token hex to set as the 2490 // cookie value. 2491 pub fn create(self: SessionStore, user_id: *const [evt.event_id_size]u8) ![token_hex_len]u8 { 2492 var token: [evt.event_id_size]u8 = undefined; 2493 self.io.random(&token); 2494 const token_hex = std.fmt.bytesToHex(token, .lower); 2495 const file = try self.dir.createFile(self.io, &token_hex, .{}); 2496 defer file.close(self.io); 2497 try file.writeStreamingAll(self.io, user_id); 2498 return token_hex; 2499 } 2500 2501 fn createFlash(self: SessionStore, allocator: std.mem.Allocator, value: ui.Session.FormFeedback) ![token_hex_len]u8 { 2502 var token: [evt.event_id_size]u8 = undefined; 2503 self.io.random(&token); 2504 const token_hex = std.fmt.bytesToHex(token, .lower); 2505 const file = try self.flash_dir.createFile(self.io, &token_hex, .{}); 2506 errdefer self.flash_dir.deleteFile(self.io, &token_hex) catch {}; 2507 defer file.close(self.io); 2508 2509 var json: std.Io.Writer.Allocating = .init(allocator); 2510 defer json.deinit(); 2511 try std.json.Stringify.value(value, .{}, &json.writer); 2512 try file.writeStreamingAll(self.io, json.written()); 2513 return token_hex; 2514 } 2515 2516 fn takeFlash(self: SessionStore, allocator: std.mem.Allocator, token_hex: []const u8) ?ui.Session.FormFeedback { 2517 if (!isToken(token_hex)) return null; 2518 const file = self.flash_dir.openFile(self.io, token_hex, .{ .mode = .read_only }) catch return null; 2519 defer self.flash_dir.deleteFile(self.io, token_hex) catch {}; 2520 defer file.close(self.io); 2521 2522 var storage: [4096]u8 = undefined; 2523 var reader = file.reader(self.io, &storage); 2524 const bytes = reader.interface.allocRemaining(allocator, .limited(512 * 1024)) catch return null; 2525 return std.json.parseFromSliceLeaky(ui.Session.FormFeedback, allocator, bytes, .{}) catch null; 2526 } 2527 2528 // resolve a cookie's token to its user_id. returns false (logged out) for a 2529 // missing, malformed, or unknown token. 2530 pub fn lookup(self: SessionStore, token_hex: []const u8, out: *[evt.event_id_size]u8) bool { 2531 if (!isToken(token_hex)) return false; 2532 const file = self.dir.openFile(self.io, token_hex, .{ .mode = .read_only }) catch return false; 2533 defer file.close(self.io); 2534 var storage: [evt.event_id_size]u8 = undefined; 2535 var file_reader = file.reader(self.io, &storage); 2536 file_reader.interface.readSliceAll(out) catch return false; 2537 return true; 2538 } 2539 2540 // revoke a session. a no-op for a missing or malformed token. 2541 pub fn remove(self: SessionStore, token_hex: []const u8) void { 2542 if (!isToken(token_hex)) return; 2543 self.dir.deleteFile(self.io, token_hex) catch {}; 2544 } 2545 2546 // leave `token_hex`'s session for visitors arriving without one. only the 2547 // `try` fixture writes one. the name can't collide with a session token. 2548 pub fn offerAutoLogin(self: SessionStore, token_hex: *const [token_hex_len]u8) !void { 2549 const file = try self.dir.createFile(self.io, auto_login_name, .{}); 2550 defer file.close(self.io); 2551 try file.writeStreamingAll(self.io, token_hex); 2552 } 2553 2554 pub fn autoLogin(self: SessionStore, out: *[token_hex_len]u8) bool { 2555 out.* = self.auto_login orelse return false; 2556 return true; 2557 } 2558 2559 const auto_login_name = "auto-login"; 2560 2561 // a token is exactly token_hex_len hex chars. validating before using the 2562 // value as a path keeps attacker-supplied cookies from escaping the 2563 // sessions dir (e.g. via "/" or ".." bytes). 2564 fn isToken(token_hex: []const u8) bool { 2565 if (token_hex.len != token_hex_len) return false; 2566 for (token_hex) |c| if (!std.ascii.isHex(c)) return false; 2567 return true; 2568 } 2569 }; 2570