╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ web.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2465 const file = self.dir.openFile(self.io, token_hex, .{ .mode = .read_only }) catch return false; 2466 defer file.close(self.io); 2467 var storage: [evt.event_id_size]u8 = undefined; 2468 var file_reader = file.reader(self.io, &storage); 2469 file_reader.interface.readSliceAll(out) catch return false; 2470 return true; 2471 } 2472 2473 // revoke a session. a no-op for a missing or malformed token. 2474 pub fn remove(self: SessionStore, token_hex: []const u8) void { 2475 if (!isToken(token_hex)) return; 2476 self.dir.deleteFile(self.io, token_hex) catch {}; 2477 } 2478 2479 // leave `token_hex`'s session for visitors arriving without one. only the 2480 // `try` fixture writes one. the name can't collide with a session token. 2481 pub fn offerAutoLogin(self: SessionStore, token_hex: *const [token_hex_len]u8) !void { 2482 const file = try self.dir.createFile(self.io, auto_login_name, .{}); 2483 defer file.close(self.io); 2484 try file.writeStreamingAll(self.io, token_hex); 2485 } 2486 2487 pub fn autoLogin(self: SessionStore, out: *[token_hex_len]u8) bool { 2488 out.* = self.auto_login orelse return false; 2489 return true; 2490 } 2491 2492 const auto_login_name = "auto-login"; 2493 2494 // a token is exactly token_hex_len hex chars. validating before using the 2495 // value as a path keeps attacker-supplied cookies from escaping the 2496 // sessions dir (e.g. via "/" or ".." bytes). 2497 fn isToken(token_hex: []const u8) bool { 2498 if (token_hex.len != token_hex_len) return false; 2499 for (token_hex) |c| if (!std.ascii.isHex(c)) return false; 2500 return true; 2501 } 2502 }; 2503