╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ web.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2583 try std.json.Stringify.value(value, .{}, &json.writer); 2584 try file.writeStreamingAll(self.io, json.written()); 2585 return token_hex; 2586 } 2587 2588 fn takeFlash(self: SessionStore, allocator: std.mem.Allocator, token_hex: []const u8) ?ui.Session.FormFeedback { 2589 if (!isToken(token_hex)) return null; 2590 const file = self.flash_dir.openFile(self.io, token_hex, .{ .mode = .read_only }) catch return null; 2591 defer self.flash_dir.deleteFile(self.io, token_hex) catch {}; 2592 defer file.close(self.io); 2593 2594 var storage: [4096]u8 = undefined; 2595 var reader = file.reader(self.io, &storage); 2596 const bytes = reader.interface.allocRemaining(allocator, .limited(512 * 1024)) catch return null; 2597 return std.json.parseFromSliceLeaky(ui.Session.FormFeedback, allocator, bytes, .{}) catch null; 2598 } 2599 2600 // resolve a cookie's token to its user_id. returns false (logged out) for a 2601 // missing, malformed, or unknown token. 2602 pub fn lookup(self: SessionStore, token_hex: []const u8, out: *[evt.event_id_size]u8) bool { 2603 if (!isToken(token_hex)) return false; 2604 const file = self.dir.openFile(self.io, token_hex, .{ .mode = .read_only }) catch return false; 2605 defer file.close(self.io); 2606 var storage: [evt.event_id_size]u8 = undefined; 2607 var file_reader = file.reader(self.io, &storage); 2608 file_reader.interface.readSliceAll(out) catch return false; 2609 return true; 2610 } 2611 2612 // revoke a session. a no-op for a missing or malformed token. 2613 pub fn remove(self: SessionStore, token_hex: []const u8) void { 2614 if (!isToken(token_hex)) return; 2615 self.dir.deleteFile(self.io, token_hex) catch {}; 2616 } 2617 2618 // leave `token_hex`'s session for visitors arriving without one. only the 2619 // `try` fixture writes one. the name can't collide with a session token. 2620 pub fn offerAutoLogin(self: SessionStore, token_hex: *const [token_hex_len]u8) !void { 2621 const file = try self.dir.createFile(self.io, auto_login_name, .{}); 2622 defer file.close(self.io); 2623 try file.writeStreamingAll(self.io, token_hex); 2624 } 2625 2626 pub fn autoLogin(self: SessionStore, out: *[token_hex_len]u8) bool { 2627 out.* = self.auto_login orelse return false; 2628 return true; 2629 } 2630 2631 const auto_login_name = "auto-login"; 2632 2633 // a token is exactly token_hex_len hex chars. validating before using the 2634 // value as a path keeps attacker-supplied cookies from escaping the 2635 // sessions dir (e.g. via "/" or ".." bytes). 2636 fn isToken(token_hex: []const u8) bool { 2637 if (token_hex.len != token_hex_len) return false; 2638 for (token_hex) |c| if (!std.ascii.isHex(c)) return false; 2639 return true; 2640 } 2641 }; 2642