╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ web.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
3054 dir: std.Io.Dir, 3055 flash_dir: std.Io.Dir, 3056 auto_login: ?[token_hex_len]u8, 3057 3058 pub const token_hex_len = evt.event_id_size * 2; 3059 3060 pub fn init(io: std.Io, data_dir: std.Io.Dir) !SessionStore { 3061 try data_dir.createDirPath(io, "sessions"); 3062 const dir = try data_dir.openDir(io, "sessions", .{}); 3063 errdefer dir.close(io); 3064 try dir.deleteTree(io, "flash"); 3065 try dir.createDirPath(io, "flash"); 3066 const flash_dir = try dir.openDir(io, "flash", .{}); 3067 errdefer flash_dir.close(io); 3068 const auto_login = blk: { 3069 const file = dir.openFile(io, auto_login_name, .{ .mode = .read_only }) catch break :blk null; 3070 defer file.close(io); 3071 var token: [token_hex_len]u8 = undefined; 3072 var storage: [token_hex_len]u8 = undefined; 3073 var reader = file.reader(io, &storage); 3074 reader.interface.readSliceAll(&token) catch break :blk null; 3075 break :blk token; 3076 }; 3077 return .{ .io = io, .dir = dir, .flash_dir = flash_dir, .auto_login = auto_login }; 3078 } 3079 3080 pub fn deinit(self: SessionStore) void { 3081 self.flash_dir.close(self.io); 3082 self.dir.close(self.io); 3083 } 3084 3085 // mint a new session for user_id, returning the token hex to set as the 3086 // cookie value. 3087 pub fn create(self: SessionStore, user_id: *const [evt.event_id_size]u8) ![token_hex_len]u8 { 3088 var token: [evt.event_id_size]u8 = undefined; 3089 self.io.random(&token); 3090 const token_hex = std.fmt.bytesToHex(token, .lower); 3091 const file = try self.dir.createFile(self.io, &token_hex, .{}); 3092 defer file.close(self.io); 3093 try file.writeStreamingAll(self.io, user_id); 3094 return token_hex; 3095 } 3096 3097 fn createFlash(self: SessionStore, allocator: std.mem.Allocator, value: ui.Session.FormFeedback) ![token_hex_len]u8 { 3098 var token: [evt.event_id_size]u8 = undefined; 3099 self.io.random(&token); 3100 const token_hex = std.fmt.bytesToHex(token, .lower); 3101 const file = try self.flash_dir.createFile(self.io, &token_hex, .{}); 3102 errdefer self.flash_dir.deleteFile(self.io, &token_hex) catch {}; 3103 defer file.close(self.io); 3104 3105 var json: std.Io.Writer.Allocating = .init(allocator); 3106 defer json.deinit(); 3107 try std.json.Stringify.value(value, .{}, &json.writer); 3108 try file.writeStreamingAll(self.io, json.written()); 3109 return token_hex; 3110 } 3111 3112 fn takeFlash(self: SessionStore, allocator: std.mem.Allocator, token_hex: []const u8) ?ui.Session.FormFeedback { 3113 if (!isToken(token_hex)) return null; 3114 const file = self.flash_dir.openFile(self.io, token_hex, .{ .mode = .read_only }) catch return null; 3115 defer self.flash_dir.deleteFile(self.io, token_hex) catch {}; 3116 defer file.close(self.io); 3117 3118 var storage: [4096]u8 = undefined; 3119 var reader = file.reader(self.io, &storage); 3120 const bytes = reader.interface.allocRemaining(allocator, .limited(512 * 1024)) catch return null; 3121 return std.json.parseFromSliceLeaky(ui.Session.FormFeedback, allocator, bytes, .{}) catch null; 3122 } 3123 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