╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ web.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2506 // revoke a session. a no-op for a missing or malformed token. 2507 pub fn remove(self: SessionStore, token_hex: []const u8) void { 2508 if (!isToken(token_hex)) return; 2509 self.dir.deleteFile(self.io, token_hex) catch {}; 2510 } 2511 2512 // leave `token_hex`'s session for visitors arriving without one. only the 2513 // `try` fixture writes one. the name can't collide with a session token. 2514 pub fn offerAutoLogin(self: SessionStore, token_hex: *const [token_hex_len]u8) !void { 2515 const file = try self.dir.createFile(self.io, auto_login_name, .{}); 2516 defer file.close(self.io); 2517 try file.writeStreamingAll(self.io, token_hex); 2518 } 2519 2520 pub fn autoLogin(self: SessionStore, out: *[token_hex_len]u8) bool { 2521 out.* = self.auto_login orelse return false; 2522 return true; 2523 } 2524 2525 const auto_login_name = "auto-login"; 2526 2527 // a token is exactly token_hex_len hex chars. validating before using the 2528 // value as a path keeps attacker-supplied cookies from escaping the 2529 // sessions dir (e.g. via "/" or ".." bytes). 2530 fn isToken(token_hex: []const u8) bool { 2531 if (token_hex.len != token_hex_len) return false; 2532 for (token_hex) |c| if (!std.ascii.isHex(c)) return false; 2533 return true; 2534 } 2535 }; 2536