╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ web.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2375 }, 2376 else => try out.append(allocator, encoded[i]), 2377 } 2378 } 2379 return try out.toOwnedSlice(allocator); 2380 } 2381 2382 // disk-backed web session store. each login mints a random, opaque token 2383 // (not the user's id) that is stored as a file named by the token hex, whose 2384 // contents are the raw user_id bytes. authenticating a request is a lookup of 2385 // the cookie's token; logout deletes the file, revoking the session. there is 2386 // deliberately no expiry — a session lives until logout. form failures use 2387 // files in the flash subdirectory that the redirected GET consumes. the 2388 // directory is cleared whenever the server starts. 2389 pub const SessionStore = struct { 2390 io: std.Io, 2391 dir: std.Io.Dir, 2392 flash_dir: std.Io.Dir, 2393 auto_login: ?[token_hex_len]u8, 2394 2395 pub const token_hex_len = evt.event_id_size * 2; 2396 2397 pub fn init(io: std.Io, data_dir: std.Io.Dir) !SessionStore { 2398 try data_dir.createDirPath(io, "sessions"); 2399 const dir = try data_dir.openDir(io, "sessions", .{}); 2400 errdefer dir.close(io); 2401 try dir.deleteTree(io, "flash"); 2402 try dir.createDirPath(io, "flash"); 2403 const flash_dir = try dir.openDir(io, "flash", .{}); 2404 errdefer flash_dir.close(io); 2405 const auto_login = blk: { 2406 const file = dir.openFile(io, auto_login_name, .{ .mode = .read_only }) catch break :blk null; 2407 defer file.close(io); 2408 var token: [token_hex_len]u8 = undefined; 2409 var storage: [token_hex_len]u8 = undefined; 2410 var reader = file.reader(io, &storage); 2411 reader.interface.readSliceAll(&token) catch break :blk null; 2412 break :blk token; 2413 }; 2414 return .{ .io = io, .dir = dir, .flash_dir = flash_dir, .auto_login = auto_login }; 2415 } 2416 2417 pub fn deinit(self: SessionStore) void { 2418 self.flash_dir.close(self.io); 2419 self.dir.close(self.io); 2420 } 2421 2422 // mint a new session for user_id, returning the token hex to set as the 2423 // cookie value. 2424 pub fn create(self: SessionStore, user_id: *const [evt.event_id_size]u8) ![token_hex_len]u8 { 2425 var token: [evt.event_id_size]u8 = undefined; 2426 self.io.random(&token); 2427 const token_hex = std.fmt.bytesToHex(token, .lower); 2428 const file = try self.dir.createFile(self.io, &token_hex, .{}); 2429 defer file.close(self.io); 2430 try file.writeStreamingAll(self.io, user_id); 2431 return token_hex; 2432 } 2433 2434 fn createFlash(self: SessionStore, allocator: std.mem.Allocator, value: ui.Session.FormFeedback) ![token_hex_len]u8 { 2435 var token: [evt.event_id_size]u8 = undefined; 2436 self.io.random(&token); 2437 const token_hex = std.fmt.bytesToHex(token, .lower); 2438 const file = try self.flash_dir.createFile(self.io, &token_hex, .{}); 2439 errdefer self.flash_dir.deleteFile(self.io, &token_hex) catch {}; 2440 defer file.close(self.io); 2441 2442 var json: std.Io.Writer.Allocating = .init(allocator); 2443 defer json.deinit(); 2444 try std.json.Stringify.value(value, .{}, &json.writer); 2445 try file.writeStreamingAll(self.io, json.written()); 2446 return token_hex; 2447 } 2448 2449 fn takeFlash(self: SessionStore, allocator: std.mem.Allocator, token_hex: []const u8) ?ui.Session.FormFeedback { 2450 if (!isToken(token_hex)) return null; 2451 const file = self.flash_dir.openFile(self.io, token_hex, .{ .mode = .read_only }) catch return null; 2452 defer self.flash_dir.deleteFile(self.io, token_hex) catch {}; 2453 defer file.close(self.io); 2454 2455 var storage: [4096]u8 = undefined; 2456 var reader = file.reader(self.io, &storage); 2457 const bytes = reader.interface.allocRemaining(allocator, .limited(512 * 1024)) catch return null; 2458 return std.json.parseFromSliceLeaky(ui.Session.FormFeedback, allocator, bytes, .{}) catch null; 2459 } 2460 2461 // resolve a cookie's token to its user_id. returns false (logged out) for a 2462 // missing, malformed, or unknown token. 2463 pub fn lookup(self: SessionStore, token_hex: []const u8, out: *[evt.event_id_size]u8) bool { 2464 if (!isToken(token_hex)) return false; 2465 const file = self.dir.openFile(self.io, token_hex, .{ .mode = .read_only }) catch return false; 2466 defer file.close(self.io); 2467 var storage: [evt.event_id_size]u8 = undefined; 2468 var file_reader = file.reader(self.io, &storage); 2469 file_reader.interface.readSliceAll(out) catch return false; 2470 return true; 2471 } 2472 2473 // revoke a session. a no-op for a missing or malformed token. 2474 pub fn remove(self: SessionStore, token_hex: []const u8) void { 2475 if (!isToken(token_hex)) return; 2476 self.dir.deleteFile(self.io, token_hex) catch {}; 2477 } 2478 2479 // leave `token_hex`'s session for visitors arriving without one. only the 2480 // `try` fixture writes one. the name can't collide with a session token. 2481 pub fn offerAutoLogin(self: SessionStore, token_hex: *const [token_hex_len]u8) !void { 2482 const file = try self.dir.createFile(self.io, auto_login_name, .{}); 2483 defer file.close(self.io); 2484 try file.writeStreamingAll(self.io, token_hex); 2485 } 2486 2487 pub fn autoLogin(self: SessionStore, out: *[token_hex_len]u8) bool { 2488 out.* = self.auto_login orelse return false; 2489 return true; 2490 } 2491 2492 const auto_login_name = "auto-login"; 2493 2494 // a token is exactly token_hex_len hex chars. validating before using the 2495 // value as a path keeps attacker-supplied cookies from escaping the 2496 // sessions dir (e.g. via "/" or ".." bytes). 2497 fn isToken(token_hex: []const u8) bool { 2498 if (token_hex.len != token_hex_len) return false; 2499 for (token_hex) |c| if (!std.ascii.isHex(c)) return false; 2500 return true; 2501 } 2502 }; 2503