╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ web.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2065 return true; 2066 } 2067 2068 // revoke a session. a no-op for a missing or malformed token. 2069 pub fn remove(self: SessionStore, token_hex: []const u8) void { 2070 if (!isToken(token_hex)) return; 2071 self.dir.deleteFile(self.io, token_hex) catch {}; 2072 } 2073 2074 // leave `token_hex`'s session for visitors arriving without one. only the 2075 // `try` fixture writes one. the name can't collide with a session token. 2076 pub fn offerAutoLogin(self: SessionStore, token_hex: *const [token_hex_len]u8) !void { 2077 const file = try self.dir.createFile(self.io, auto_login_name, .{}); 2078 defer file.close(self.io); 2079 try file.writeStreamingAll(self.io, token_hex); 2080 } 2081 2082 pub fn autoLogin(self: SessionStore, out: *[token_hex_len]u8) bool { 2083 out.* = self.auto_login orelse return false; 2084 return true; 2085 } 2086 2087 const auto_login_name = "auto-login"; 2088 2089 // a token is exactly token_hex_len hex chars. validating before using the 2090 // value as a path keeps attacker-supplied cookies from escaping the 2091 // sessions dir (e.g. via "/" or ".." bytes). 2092 fn isToken(token_hex: []const u8) bool { 2093 if (token_hex.len != token_hex_len) return false; 2094 for (token_hex) |c| if (!std.ascii.isHex(c)) return false; 2095 return true; 2096 } 2097 }; 2098