╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ web.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2018 2019 fn parseFormField(allocator: std.mem.Allocator, body: []const u8, key: []const u8) !?[]u8 { 2020 var iter = std.mem.splitScalar(u8, body, '&'); 2021 while (iter.next()) |pair| { 2022 const eq = std.mem.indexOfScalar(u8, pair, '=') orelse continue; 2023 if (std.mem.eql(u8, pair[0..eq], key)) { 2024 return try decodeFormValue(allocator, pair[eq + 1 ..]); 2025 } 2026 } 2027 return null; 2028 } 2029 2030 fn readFormBody(request: *std.http.Server.Request, allocator: std.mem.Allocator) ![]u8 { 2031 var buffer: [256]u8 = undefined; 2032 return request.readerExpectNone(&buffer).allocRemaining(allocator, .limited(65536)); 2033 } 2034 2035 fn decodeFormValue(allocator: std.mem.Allocator, encoded: []const u8) ![]u8 { 2036 var out: std.ArrayList(u8) = .empty; 2037 errdefer out.deinit(allocator); 2038 var i: usize = 0; 2039 while (i < encoded.len) : (i += 1) { 2040 switch (encoded[i]) { 2041 '+' => try out.append(allocator, ' '), 2042 '%' => { 2043 if (i + 2 >= encoded.len) return error.InvalidPercentEncoding; 2044 const hi = std.fmt.charToDigit(encoded[i + 1], 16) catch return error.InvalidPercentEncoding; 2045 const lo = std.fmt.charToDigit(encoded[i + 2], 16) catch return error.InvalidPercentEncoding; 2046 try out.append(allocator, hi * 16 + lo); 2047 i += 2; 2048 }, 2049 else => try out.append(allocator, encoded[i]), 2050 } 2051 } 2052 return try out.toOwnedSlice(allocator); 2053 } 2054 2055 // disk-backed web session store. each login mints a random, opaque token 2056 // (not the user's id) that is stored as a file named by the token hex, whose 2057 // contents are the raw user_id bytes. authenticating a request is a lookup of 2058 // the cookie's token; logout deletes the file, revoking the session. there is 2059 // deliberately no expiry — a session lives until logout. 2060 pub const SessionStore = struct { 2061 io: std.Io, 2062 dir: std.Io.Dir, 2063 auto_login: ?[token_hex_len]u8, 2064 2065 pub const token_hex_len = evt.event_id_size * 2; 2066 2067 pub fn init(io: std.Io, data_dir: std.Io.Dir) !SessionStore { 2068 try data_dir.createDirPath(io, "sessions"); 2069 const dir = try data_dir.openDir(io, "sessions", .{}); 2070 const auto_login = blk: { 2071 const file = dir.openFile(io, auto_login_name, .{ .mode = .read_only }) catch break :blk null; 2072 defer file.close(io); 2073 var token: [token_hex_len]u8 = undefined; 2074 var storage: [token_hex_len]u8 = undefined; 2075 var reader = file.reader(io, &storage); 2076 reader.interface.readSliceAll(&token) catch break :blk null; 2077 break :blk token; 2078 }; 2079 return .{ .io = io, .dir = dir, .auto_login = auto_login }; 2080 } 2081 2082 pub fn deinit(self: SessionStore) void { 2083 self.dir.close(self.io); 2084 } 2085 2086 // mint a new session for user_id, returning the token hex to set as the 2087 // cookie value. 2088 pub fn create(self: SessionStore, user_id: *const [evt.event_id_size]u8) ![token_hex_len]u8 { 2089 var token: [evt.event_id_size]u8 = undefined; 2090 self.io.random(&token); 2091 const token_hex = std.fmt.bytesToHex(token, .lower); 2092 const file = try self.dir.createFile(self.io, &token_hex, .{}); 2093 defer file.close(self.io); 2094 try file.writeStreamingAll(self.io, user_id); 2095 return token_hex; 2096 } 2097 2098 // resolve a cookie's token to its user_id. returns false (logged out) for a 2099 // missing, malformed, or unknown token. 2100 pub fn lookup(self: SessionStore, token_hex: []const u8, out: *[evt.event_id_size]u8) bool { 2101 if (!isToken(token_hex)) return false; 2102 const file = self.dir.openFile(self.io, token_hex, .{ .mode = .read_only }) catch return false; 2103 defer file.close(self.io); 2104 var storage: [evt.event_id_size]u8 = undefined; 2105 var file_reader = file.reader(self.io, &storage); 2106 file_reader.interface.readSliceAll(out) catch return false; 2107 return true; 2108 } 2109 2110 // revoke a session. a no-op for a missing or malformed token. 2111 pub fn remove(self: SessionStore, token_hex: []const u8) void { 2112 if (!isToken(token_hex)) return; 2113 self.dir.deleteFile(self.io, token_hex) catch {}; 2114 } 2115 2116 // leave `token_hex`'s session for visitors arriving without one. only the 2117 // `try` fixture writes one. the name can't collide with a session token. 2118 pub fn offerAutoLogin(self: SessionStore, token_hex: *const [token_hex_len]u8) !void { 2119 const file = try self.dir.createFile(self.io, auto_login_name, .{}); 2120 defer file.close(self.io); 2121 try file.writeStreamingAll(self.io, token_hex); 2122 } 2123 2124 pub fn autoLogin(self: SessionStore, out: *[token_hex_len]u8) bool { 2125 out.* = self.auto_login orelse return false; 2126 return true; 2127 } 2128 2129 const auto_login_name = "auto-login"; 2130 2131 // a token is exactly token_hex_len hex chars. validating before using the 2132 // value as a path keeps attacker-supplied cookies from escaping the 2133 // sessions dir (e.g. via "/" or ".." bytes). 2134 fn isToken(token_hex: []const u8) bool { 2135 if (token_hex.len != token_hex_len) return false; 2136 for (token_hex) |c| if (!std.ascii.isHex(c)) return false; 2137 return true; 2138 } 2139 }; 2140