╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ web.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
3125 defer json.deinit(); 3126 try std.json.Stringify.value(value, .{}, &json.writer); 3127 try file.writeStreamingAll(self.io, json.written()); 3128 return token_hex; 3129 } 3130 3131 fn takeFlash(self: SessionStore, allocator: std.mem.Allocator, token_hex: []const u8) ?ui.Session.FormFeedback { 3132 if (!isToken(token_hex)) return null; 3133 const file = self.flash_dir.openFile(self.io, token_hex, .{ .mode = .read_only }) catch return null; 3134 defer self.flash_dir.deleteFile(self.io, token_hex) catch {}; 3135 defer file.close(self.io); 3136 3137 var storage: [4096]u8 = undefined; 3138 var reader = file.reader(self.io, &storage); 3139 const bytes = reader.interface.allocRemaining(allocator, .limited(512 * 1024)) catch return null; 3140 return std.json.parseFromSliceLeaky(ui.Session.FormFeedback, allocator, bytes, .{}) catch null; 3141 } 3142 3143 // resolve a cookie's token to its user_id. returns false (logged out) for a 3144 // missing, malformed, or unknown token. 3145 pub fn lookup(self: SessionStore, token_hex: []const u8, out: *[evt.event_id_size]u8) bool { 3146 if (!isToken(token_hex)) return false; 3147 const file = self.dir.openFile(self.io, token_hex, .{ .mode = .read_only }) catch return false; 3148 defer file.close(self.io); 3149 var storage: [evt.event_id_size]u8 = undefined; 3150 var file_reader = file.reader(self.io, &storage); 3151 file_reader.interface.readSliceAll(out) catch return false; 3152 return true; 3153 } 3154 3155 // revoke a session. a no-op for a missing or malformed token. 3156 pub fn remove(self: SessionStore, token_hex: []const u8) void { 3157 if (!isToken(token_hex)) return; 3158 self.dir.deleteFile(self.io, token_hex) catch {}; 3159 } 3160 3161 // leave `token_hex`'s session for visitors arriving without one. only the 3162 // `try` fixture writes one. the name can't collide with a session token. 3163 pub fn offerAutoLogin(self: SessionStore, token_hex: *const [token_hex_len]u8) !void { 3164 const file = try self.dir.createFile(self.io, auto_login_name, .{}); 3165 defer file.close(self.io); 3166 try file.writeStreamingAll(self.io, token_hex); 3167 } 3168 3169 pub fn autoLogin(self: SessionStore, out: *[token_hex_len]u8) bool { 3170 out.* = self.auto_login orelse return false; 3171 return true; 3172 } 3173 3174 const auto_login_name = "auto-login"; 3175 3176 // a token is exactly token_hex_len hex chars. validating before using the 3177 // value as a path keeps attacker-supplied cookies from escaping the 3178 // sessions dir (e.g. via "/" or ".." bytes). 3179 fn isToken(token_hex: []const u8) bool { 3180 if (token_hex.len != token_hex_len) return false; 3181 for (token_hex) |c| if (!std.ascii.isHex(c)) return false; 3182 return true; 3183 } 3184 }; 3185