╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ web.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2109 var storage: [token_hex_len]u8 = undefined; 2110 var reader = file.reader(io, &storage); 2111 reader.interface.readSliceAll(&token) catch break :blk null; 2112 break :blk token; 2113 }; 2114 return .{ .io = io, .dir = dir, .auto_login = auto_login }; 2115 } 2116 2117 pub fn deinit(self: SessionStore) void { 2118 self.dir.close(self.io); 2119 } 2120 2121 // mint a new session for user_id, returning the token hex to set as the 2122 // cookie value. 2123 pub fn create(self: SessionStore, user_id: *const [evt.event_id_size]u8) ![token_hex_len]u8 { 2124 var token: [evt.event_id_size]u8 = undefined; 2125 self.io.random(&token); 2126 const token_hex = std.fmt.bytesToHex(token, .lower); 2127 const file = try self.dir.createFile(self.io, &token_hex, .{}); 2128 defer file.close(self.io); 2129 try file.writeStreamingAll(self.io, user_id); 2130 return token_hex; 2131 } 2132 2133 // resolve a cookie's token to its user_id. returns false (logged out) for a 2134 // missing, malformed, or unknown token. 2135 pub fn lookup(self: SessionStore, token_hex: []const u8, out: *[evt.event_id_size]u8) bool { 2136 if (!isToken(token_hex)) return false; 2137 const file = self.dir.openFile(self.io, token_hex, .{ .mode = .read_only }) catch return false; 2138 defer file.close(self.io); 2139 var storage: [evt.event_id_size]u8 = undefined; 2140 var file_reader = file.reader(self.io, &storage); 2141 file_reader.interface.readSliceAll(out) catch return false; 2142 return true; 2143 } 2144 2145 // revoke a session. a no-op for a missing or malformed token. 2146 pub fn remove(self: SessionStore, token_hex: []const u8) void { 2147 if (!isToken(token_hex)) return; 2148 self.dir.deleteFile(self.io, token_hex) catch {}; 2149 } 2150 2151 // leave `token_hex`'s session for visitors arriving without one. only the 2152 // `try` fixture writes one. the name can't collide with a session token. 2153 pub fn offerAutoLogin(self: SessionStore, token_hex: *const [token_hex_len]u8) !void { 2154 const file = try self.dir.createFile(self.io, auto_login_name, .{}); 2155 defer file.close(self.io); 2156 try file.writeStreamingAll(self.io, token_hex); 2157 } 2158 2159 pub fn autoLogin(self: SessionStore, out: *[token_hex_len]u8) bool { 2160 out.* = self.auto_login orelse return false; 2161 return true; 2162 } 2163 2164 const auto_login_name = "auto-login"; 2165 2166 // a token is exactly token_hex_len hex chars. validating before using the 2167 // value as a path keeps attacker-supplied cookies from escaping the 2168 // sessions dir (e.g. via "/" or ".." bytes). 2169 fn isToken(token_hex: []const u8) bool { 2170 if (token_hex.len != token_hex_len) return false; 2171 for (token_hex) |c| if (!std.ascii.isHex(c)) return false; 2172 return true; 2173 } 2174 }; 2175