╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ web.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2005 i += 2; 2006 }, 2007 else => try out.append(allocator, encoded[i]), 2008 } 2009 } 2010 return try out.toOwnedSlice(allocator); 2011 } 2012 2013 // disk-backed web session store. each login mints a random, opaque token 2014 // (not the user's id) that is stored as a file named by the token hex, whose 2015 // contents are the raw user_id bytes. authenticating a request is a lookup of 2016 // the cookie's token; logout deletes the file, revoking the session. there is 2017 // deliberately no expiry — a session lives until logout. 2018 pub const SessionStore = struct { 2019 io: std.Io, 2020 dir: std.Io.Dir, 2021 auto_login: ?[token_hex_len]u8, 2022 2023 pub const token_hex_len = evt.event_id_size * 2; 2024 2025 pub fn init(io: std.Io, data_dir: std.Io.Dir) !SessionStore { 2026 try data_dir.createDirPath(io, "sessions"); 2027 const dir = try data_dir.openDir(io, "sessions", .{}); 2028 const auto_login = blk: { 2029 const file = dir.openFile(io, auto_login_name, .{ .mode = .read_only }) catch break :blk null; 2030 defer file.close(io); 2031 var token: [token_hex_len]u8 = undefined; 2032 var storage: [token_hex_len]u8 = undefined; 2033 var reader = file.reader(io, &storage); 2034 reader.interface.readSliceAll(&token) catch break :blk null; 2035 break :blk token; 2036 }; 2037 return .{ .io = io, .dir = dir, .auto_login = auto_login }; 2038 } 2039 2040 pub fn deinit(self: SessionStore) void { 2041 self.dir.close(self.io); 2042 } 2043 2044 // mint a new session for user_id, returning the token hex to set as the 2045 // cookie value. 2046 pub fn create(self: SessionStore, user_id: *const [evt.event_id_size]u8) ![token_hex_len]u8 { 2047 var token: [evt.event_id_size]u8 = undefined; 2048 self.io.random(&token); 2049 const token_hex = std.fmt.bytesToHex(token, .lower); 2050 const file = try self.dir.createFile(self.io, &token_hex, .{}); 2051 defer file.close(self.io); 2052 try file.writeStreamingAll(self.io, user_id); 2053 return token_hex; 2054 } 2055 2056 // resolve a cookie's token to its user_id. returns false (logged out) for a 2057 // missing, malformed, or unknown token. 2058 pub fn lookup(self: SessionStore, token_hex: []const u8, out: *[evt.event_id_size]u8) bool { 2059 if (!isToken(token_hex)) return false; 2060 const file = self.dir.openFile(self.io, token_hex, .{ .mode = .read_only }) catch return false; 2061 defer file.close(self.io); 2062 var storage: [evt.event_id_size]u8 = undefined; 2063 var file_reader = file.reader(self.io, &storage); 2064 file_reader.interface.readSliceAll(out) catch return false; 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