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