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