╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ web.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
3043 3044 fn createFlash(self: SessionStore, allocator: std.mem.Allocator, value: ui.Session.FormFeedback) ![token_hex_len]u8 { 3045 var token: [evt.event_id_size]u8 = undefined; 3046 self.io.random(&token); 3047 const token_hex = std.fmt.bytesToHex(token, .lower); 3048 const file = try self.flash_dir.createFile(self.io, &token_hex, .{}); 3049 errdefer self.flash_dir.deleteFile(self.io, &token_hex) catch {}; 3050 defer file.close(self.io); 3051 3052 var json: std.Io.Writer.Allocating = .init(allocator); 3053 defer json.deinit(); 3054 try std.json.Stringify.value(value, .{}, &json.writer); 3055 try file.writeStreamingAll(self.io, json.written()); 3056 return token_hex; 3057 } 3058 3059 fn takeFlash(self: SessionStore, allocator: std.mem.Allocator, token_hex: []const u8) ?ui.Session.FormFeedback { 3060 if (!isToken(token_hex)) return null; 3061 const file = self.flash_dir.openFile(self.io, token_hex, .{ .mode = .read_only }) catch return null; 3062 defer self.flash_dir.deleteFile(self.io, token_hex) catch {}; 3063 defer file.close(self.io); 3064 3065 var storage: [4096]u8 = undefined; 3066 var reader = file.reader(self.io, &storage); 3067 const bytes = reader.interface.allocRemaining(allocator, .limited(512 * 1024)) catch return null; 3068 return std.json.parseFromSliceLeaky(ui.Session.FormFeedback, allocator, bytes, .{}) catch null; 3069 } 3070 3071 // resolve a cookie's token to its user_id. returns false (logged out) for a 3072 // missing, malformed, or unknown token. 3073 pub fn lookup(self: SessionStore, token_hex: []const u8, out: *[evt.event_id_size]u8) bool { 3074 if (!isToken(token_hex)) return false; 3075 const file = self.dir.openFile(self.io, token_hex, .{ .mode = .read_only }) catch return false; 3076 defer file.close(self.io); 3077 var storage: [evt.event_id_size]u8 = undefined; 3078 var file_reader = file.reader(self.io, &storage); 3079 file_reader.interface.readSliceAll(out) catch return false; 3080 return true; 3081 } 3082 3083 // revoke a session. a no-op for a missing or malformed token. 3084 pub fn remove(self: SessionStore, token_hex: []const u8) void { 3085 if (!isToken(token_hex)) return; 3086 self.dir.deleteFile(self.io, token_hex) catch {}; 3087 } 3088 3089 // leave `token_hex`'s session for visitors arriving without one. only the 3090 // `try` fixture writes one. the name can't collide with a session token. 3091 pub fn offerAutoLogin(self: SessionStore, token_hex: *const [token_hex_len]u8) !void { 3092 const file = try self.dir.createFile(self.io, auto_login_name, .{}); 3093 defer file.close(self.io); 3094 try file.writeStreamingAll(self.io, token_hex); 3095 } 3096 3097 pub fn autoLogin(self: SessionStore, out: *[token_hex_len]u8) bool { 3098 out.* = self.auto_login orelse return false; 3099 return true; 3100 } 3101 3102 const auto_login_name = "auto-login"; 3103 3104 // a token is exactly token_hex_len hex chars. validating before using the 3105 // value as a path keeps attacker-supplied cookies from escaping the 3106 // sessions dir (e.g. via "/" or ".." bytes). 3107 fn isToken(token_hex: []const u8) bool { 3108 if (token_hex.len != token_hex_len) return false; 3109 for (token_hex) |c| if (!std.ascii.isHex(c)) return false; 3110 return true; 3111 } 3112 }; 3113