╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ web.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2020 dir: std.Io.Dir, 2021 2022 pub const token_hex_len = evt.event_id_size * 2; 2023 2024 pub fn init(io: std.Io, data_dir: std.Io.Dir) !SessionStore { 2025 try data_dir.createDirPath(io, "sessions"); 2026 const dir = try data_dir.openDir(io, "sessions", .{}); 2027 return .{ .io = io, .dir = dir }; 2028 } 2029 2030 pub fn deinit(self: SessionStore) void { 2031 self.dir.close(self.io); 2032 } 2033 2034 // mint a new session for user_id, returning the token hex to set as the 2035 // cookie value. 2036 pub fn create(self: SessionStore, user_id: *const [evt.event_id_size]u8) ![token_hex_len]u8 { 2037 var token: [evt.event_id_size]u8 = undefined; 2038 self.io.random(&token); 2039 const token_hex = std.fmt.bytesToHex(token, .lower); 2040 const file = try self.dir.createFile(self.io, &token_hex, .{}); 2041 defer file.close(self.io); 2042 try file.writeStreamingAll(self.io, user_id); 2043 return token_hex; 2044 } 2045 2046 // resolve a cookie's token to its user_id. returns false (logged out) for a 2047 // missing, malformed, or unknown token. 2048 pub fn lookup(self: SessionStore, token_hex: []const u8, out: *[evt.event_id_size]u8) bool { 2049 if (!isToken(token_hex)) return false; 2050 const file = self.dir.openFile(self.io, token_hex, .{ .mode = .read_only }) catch return false; 2051 defer file.close(self.io); 2052 var storage: [evt.event_id_size]u8 = undefined; 2053 var file_reader = file.reader(self.io, &storage); 2054 file_reader.interface.readSliceAll(out) catch return false; 2055 return true; 2056 } 2057 2058 // revoke a session. a no-op for a missing or malformed token. 2059 pub fn remove(self: SessionStore, token_hex: []const u8) void { 2060 if (!isToken(token_hex)) return; 2061 self.dir.deleteFile(self.io, token_hex) catch {}; 2062 } 2063 2064 // leave `token_hex`'s session for the next visitor arriving without one. 2065 // only the `try` fixture writes one, and only a debug build honors it. the 2066 // name isn't token-shaped, so it can't collide with a session file. 2067 pub fn offerAutoLogin(self: SessionStore, token_hex: *const [token_hex_len]u8) !void { 2068 const file = try self.dir.createFile(self.io, auto_login_name, .{}); 2069 defer file.close(self.io); 2070 try file.writeStreamingAll(self.io, token_hex); 2071 } 2072 2073 // claim the offered session, if one is there. the delete picks the winner 2074 // of a race, so the offer is only ever taken once. a release build never 2075 // looks, because that would be an extra syscall for every GET request. 2076 pub fn claimAutoLogin(self: SessionStore, out: *[token_hex_len]u8) bool { 2077 if (builtin.mode != .Debug) return false; 2078 { 2079 const file = self.dir.openFile(self.io, auto_login_name, .{ .mode = .read_only }) catch return false; 2080 defer file.close(self.io); 2081 var storage: [token_hex_len]u8 = undefined; 2082 var file_reader = file.reader(self.io, &storage); 2083 file_reader.interface.readSliceAll(out) catch return false; 2084 } 2085 self.dir.deleteFile(self.io, auto_login_name) catch return false; 2086 return true; 2087 } 2088 2089 const auto_login_name = "auto-login"; 2090 2091 // a token is exactly token_hex_len hex chars. validating before using the 2092 // value as a path keeps attacker-supplied cookies from escaping the 2093 // sessions dir (e.g. via "/" or ".." bytes). 2094 fn isToken(token_hex: []const u8) bool { 2095 if (token_hex.len != token_hex_len) return false; 2096 for (token_hex) |c| if (!std.ascii.isHex(c)) return false; 2097 return true; 2098 } 2099 }; 2100