╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ web.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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