╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ web.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2240 2241 pub fn init(io: std.Io, data_dir: std.Io.Dir) !SessionStore { 2242 try data_dir.createDirPath(io, "sessions"); 2243 const dir = try data_dir.openDir(io, "sessions", .{}); 2244 errdefer dir.close(io); 2245 try dir.deleteTree(io, "flash"); 2246 try dir.createDirPath(io, "flash"); 2247 const flash_dir = try dir.openDir(io, "flash", .{}); 2248 errdefer flash_dir.close(io); 2249 const auto_login = blk: { 2250 const file = dir.openFile(io, auto_login_name, .{ .mode = .read_only }) catch break :blk null; 2251 defer file.close(io); 2252 var token: [token_hex_len]u8 = undefined; 2253 var storage: [token_hex_len]u8 = undefined; 2254 var reader = file.reader(io, &storage); 2255 reader.interface.readSliceAll(&token) catch break :blk null; 2256 break :blk token; 2257 }; 2258 return .{ .io = io, .dir = dir, .flash_dir = flash_dir, .auto_login = auto_login }; 2259 } 2260 2261 pub fn deinit(self: SessionStore) void { 2262 self.flash_dir.close(self.io); 2263 self.dir.close(self.io); 2264 } 2265 2266 // mint a new session for user_id, returning the token hex to set as the 2267 // cookie value. 2268 pub fn create(self: SessionStore, user_id: *const [evt.event_id_size]u8) ![token_hex_len]u8 { 2269 var token: [evt.event_id_size]u8 = undefined; 2270 self.io.random(&token); 2271 const token_hex = std.fmt.bytesToHex(token, .lower); 2272 const file = try self.dir.createFile(self.io, &token_hex, .{}); 2273 defer file.close(self.io); 2274 try file.writeStreamingAll(self.io, user_id); 2275 return token_hex; 2276 } 2277 2278 fn createFlash(self: SessionStore, allocator: std.mem.Allocator, value: ui.Session.FormFeedback) ![token_hex_len]u8 { 2279 var token: [evt.event_id_size]u8 = undefined; 2280 self.io.random(&token); 2281 const token_hex = std.fmt.bytesToHex(token, .lower); 2282 const file = try self.flash_dir.createFile(self.io, &token_hex, .{}); 2283 errdefer self.flash_dir.deleteFile(self.io, &token_hex) catch {}; 2284 defer file.close(self.io); 2285 2286 var json: std.Io.Writer.Allocating = .init(allocator); 2287 defer json.deinit(); 2288 try std.json.Stringify.value(value, .{}, &json.writer); 2289 try file.writeStreamingAll(self.io, json.written()); 2290 return token_hex; 2291 } 2292 2293 fn takeFlash(self: SessionStore, allocator: std.mem.Allocator, token_hex: []const u8) ?ui.Session.FormFeedback { 2294 if (!isToken(token_hex)) return null; 2295 const file = self.flash_dir.openFile(self.io, token_hex, .{ .mode = .read_only }) catch return null; 2296 defer self.flash_dir.deleteFile(self.io, token_hex) catch {}; 2297 defer file.close(self.io); 2298 2299 var storage: [4096]u8 = undefined; 2300 var reader = file.reader(self.io, &storage); 2301 const bytes = reader.interface.allocRemaining(allocator, .limited(512 * 1024)) catch return null; 2302 return std.json.parseFromSliceLeaky(ui.Session.FormFeedback, allocator, bytes, .{}) catch null; 2303 } 2304 2305 // resolve a cookie's token to its user_id. returns false (logged out) for a 2306 // missing, malformed, or unknown token. 2307 pub fn lookup(self: SessionStore, token_hex: []const u8, out: *[evt.event_id_size]u8) bool { 2308 if (!isToken(token_hex)) return false; 2309 const file = self.dir.openFile(self.io, token_hex, .{ .mode = .read_only }) catch return false; 2310 defer file.close(self.io); 2311 var storage: [evt.event_id_size]u8 = undefined; 2312 var file_reader = file.reader(self.io, &storage); 2313 file_reader.interface.readSliceAll(out) catch return false; 2314 return true; 2315 } 2316 2317 // revoke a session. a no-op for a missing or malformed token. 2318 pub fn remove(self: SessionStore, token_hex: []const u8) void { 2319 if (!isToken(token_hex)) return; 2320 self.dir.deleteFile(self.io, token_hex) catch {}; 2321 } 2322 2323 // leave `token_hex`'s session for visitors arriving without one. only the 2324 // `try` fixture writes one. the name can't collide with a session token. 2325 pub fn offerAutoLogin(self: SessionStore, token_hex: *const [token_hex_len]u8) !void { 2326 const file = try self.dir.createFile(self.io, auto_login_name, .{}); 2327 defer file.close(self.io); 2328 try file.writeStreamingAll(self.io, token_hex); 2329 } 2330 2331 pub fn autoLogin(self: SessionStore, out: *[token_hex_len]u8) bool { 2332 out.* = self.auto_login orelse return false; 2333 return true; 2334 } 2335 2336 const auto_login_name = "auto-login"; 2337 2338 // a token is exactly token_hex_len hex chars. validating before using the 2339 // value as a path keeps attacker-supplied cookies from escaping the 2340 // sessions dir (e.g. via "/" or ".." bytes). 2341 fn isToken(token_hex: []const u8) bool { 2342 if (token_hex.len != token_hex_len) return false; 2343 for (token_hex) |c| if (!std.ascii.isHex(c)) return false; 2344 return true; 2345 } 2346 }; 2347