╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ web.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2399 const auto_login = blk: { 2400 const file = dir.openFile(io, auto_login_name, .{ .mode = .read_only }) catch break :blk null; 2401 defer file.close(io); 2402 var token: [token_hex_len]u8 = undefined; 2403 var storage: [token_hex_len]u8 = undefined; 2404 var reader = file.reader(io, &storage); 2405 reader.interface.readSliceAll(&token) catch break :blk null; 2406 break :blk token; 2407 }; 2408 return .{ .io = io, .dir = dir, .flash_dir = flash_dir, .auto_login = auto_login }; 2409 } 2410 2411 pub fn deinit(self: SessionStore) void { 2412 self.flash_dir.close(self.io); 2413 self.dir.close(self.io); 2414 } 2415 2416 // mint a new session for user_id, returning the token hex to set as the 2417 // cookie value. 2418 pub fn create(self: SessionStore, user_id: *const [evt.event_id_size]u8) ![token_hex_len]u8 { 2419 var token: [evt.event_id_size]u8 = undefined; 2420 self.io.random(&token); 2421 const token_hex = std.fmt.bytesToHex(token, .lower); 2422 const file = try self.dir.createFile(self.io, &token_hex, .{}); 2423 defer file.close(self.io); 2424 try file.writeStreamingAll(self.io, user_id); 2425 return token_hex; 2426 } 2427 2428 fn createFlash(self: SessionStore, allocator: std.mem.Allocator, value: ui.Session.FormFeedback) ![token_hex_len]u8 { 2429 var token: [evt.event_id_size]u8 = undefined; 2430 self.io.random(&token); 2431 const token_hex = std.fmt.bytesToHex(token, .lower); 2432 const file = try self.flash_dir.createFile(self.io, &token_hex, .{}); 2433 errdefer self.flash_dir.deleteFile(self.io, &token_hex) catch {}; 2434 defer file.close(self.io); 2435 2436 var json: std.Io.Writer.Allocating = .init(allocator); 2437 defer json.deinit(); 2438 try std.json.Stringify.value(value, .{}, &json.writer); 2439 try file.writeStreamingAll(self.io, json.written()); 2440 return token_hex; 2441 } 2442 2443 fn takeFlash(self: SessionStore, allocator: std.mem.Allocator, token_hex: []const u8) ?ui.Session.FormFeedback { 2444 if (!isToken(token_hex)) return null; 2445 const file = self.flash_dir.openFile(self.io, token_hex, .{ .mode = .read_only }) catch return null; 2446 defer self.flash_dir.deleteFile(self.io, token_hex) catch {}; 2447 defer file.close(self.io); 2448 2449 var storage: [4096]u8 = undefined; 2450 var reader = file.reader(self.io, &storage); 2451 const bytes = reader.interface.allocRemaining(allocator, .limited(512 * 1024)) catch return null; 2452 return std.json.parseFromSliceLeaky(ui.Session.FormFeedback, allocator, bytes, .{}) catch null; 2453 } 2454 2455 // resolve a cookie's token to its user_id. returns false (logged out) for a 2456 // missing, malformed, or unknown token. 2457 pub fn lookup(self: SessionStore, token_hex: []const u8, out: *[evt.event_id_size]u8) bool { 2458 if (!isToken(token_hex)) return false; 2459 const file = self.dir.openFile(self.io, token_hex, .{ .mode = .read_only }) catch return false; 2460 defer file.close(self.io); 2461 var storage: [evt.event_id_size]u8 = undefined; 2462 var file_reader = file.reader(self.io, &storage); 2463 file_reader.interface.readSliceAll(out) catch return false; 2464 return true; 2465 } 2466 2467 // revoke a session. a no-op for a missing or malformed token. 2468 pub fn remove(self: SessionStore, token_hex: []const u8) void { 2469 if (!isToken(token_hex)) return; 2470 self.dir.deleteFile(self.io, token_hex) catch {}; 2471 } 2472 2473 // leave `token_hex`'s session for visitors arriving without one. only the 2474 // `try` fixture writes one. the name can't collide with a session token. 2475 pub fn offerAutoLogin(self: SessionStore, token_hex: *const [token_hex_len]u8) !void { 2476 const file = try self.dir.createFile(self.io, auto_login_name, .{}); 2477 defer file.close(self.io); 2478 try file.writeStreamingAll(self.io, token_hex); 2479 } 2480 2481 pub fn autoLogin(self: SessionStore, out: *[token_hex_len]u8) bool { 2482 out.* = self.auto_login orelse return false; 2483 return true; 2484 } 2485 2486 const auto_login_name = "auto-login"; 2487 2488 // a token is exactly token_hex_len hex chars. validating before using the 2489 // value as a path keeps attacker-supplied cookies from escaping the 2490 // sessions dir (e.g. via "/" or ".." bytes). 2491 fn isToken(token_hex: []const u8) bool { 2492 if (token_hex.len != token_hex_len) return false; 2493 for (token_hex) |c| if (!std.ascii.isHex(c)) return false; 2494 return true; 2495 } 2496 }; 2497