╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ web.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2141 pub const SessionStore = struct { 2142 io: std.Io, 2143 dir: std.Io.Dir, 2144 auto_login: ?[token_hex_len]u8, 2145 2146 pub const token_hex_len = evt.event_id_size * 2; 2147 2148 pub fn init(io: std.Io, data_dir: std.Io.Dir) !SessionStore { 2149 try data_dir.createDirPath(io, "sessions"); 2150 const dir = try data_dir.openDir(io, "sessions", .{}); 2151 const auto_login = blk: { 2152 const file = dir.openFile(io, auto_login_name, .{ .mode = .read_only }) catch break :blk null; 2153 defer file.close(io); 2154 var token: [token_hex_len]u8 = undefined; 2155 var storage: [token_hex_len]u8 = undefined; 2156 var reader = file.reader(io, &storage); 2157 reader.interface.readSliceAll(&token) catch break :blk null; 2158 break :blk token; 2159 }; 2160 return .{ .io = io, .dir = dir, .auto_login = auto_login }; 2161 } 2162 2163 pub fn deinit(self: SessionStore) void { 2164 self.dir.close(self.io); 2165 } 2166 2167 // mint a new session for user_id, returning the token hex to set as the 2168 // cookie value. 2169 pub fn create(self: SessionStore, user_id: *const [evt.event_id_size]u8) ![token_hex_len]u8 { 2170 var token: [evt.event_id_size]u8 = undefined; 2171 self.io.random(&token); 2172 const token_hex = std.fmt.bytesToHex(token, .lower); 2173 const file = try self.dir.createFile(self.io, &token_hex, .{}); 2174 defer file.close(self.io); 2175 try file.writeStreamingAll(self.io, user_id); 2176 return token_hex; 2177 } 2178 2179 // resolve a cookie's token to its user_id. returns false (logged out) for a 2180 // missing, malformed, or unknown token. 2181 pub fn lookup(self: SessionStore, token_hex: []const u8, out: *[evt.event_id_size]u8) bool { 2182 if (!isToken(token_hex)) return false; 2183 const file = self.dir.openFile(self.io, token_hex, .{ .mode = .read_only }) catch return false; 2184 defer file.close(self.io); 2185 var storage: [evt.event_id_size]u8 = undefined; 2186 var file_reader = file.reader(self.io, &storage); 2187 file_reader.interface.readSliceAll(out) catch return false; 2188 return true; 2189 } 2190 2191 // revoke a session. a no-op for a missing or malformed token. 2192 pub fn remove(self: SessionStore, token_hex: []const u8) void { 2193 if (!isToken(token_hex)) return; 2194 self.dir.deleteFile(self.io, token_hex) catch {}; 2195 } 2196 2197 // leave `token_hex`'s session for visitors arriving without one. only the 2198 // `try` fixture writes one. the name can't collide with a session token. 2199 pub fn offerAutoLogin(self: SessionStore, token_hex: *const [token_hex_len]u8) !void { 2200 const file = try self.dir.createFile(self.io, auto_login_name, .{}); 2201 defer file.close(self.io); 2202 try file.writeStreamingAll(self.io, token_hex); 2203 } 2204 2205 pub fn autoLogin(self: SessionStore, out: *[token_hex_len]u8) bool { 2206 out.* = self.auto_login orelse return false; 2207 return true; 2208 } 2209 2210 const auto_login_name = "auto-login"; 2211 2212 // a token is exactly token_hex_len hex chars. validating before using the 2213 // value as a path keeps attacker-supplied cookies from escaping the 2214 // sessions dir (e.g. via "/" or ".." bytes). 2215 fn isToken(token_hex: []const u8) bool { 2216 if (token_hex.len != token_hex_len) return false; 2217 for (token_hex) |c| if (!std.ascii.isHex(c)) return false; 2218 return true; 2219 } 2220 }; 2221