╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ web.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2027 defer file.close(io); 2028 var token: [token_hex_len]u8 = undefined; 2029 var storage: [token_hex_len]u8 = undefined; 2030 var reader = file.reader(io, &storage); 2031 reader.interface.readSliceAll(&token) catch break :blk null; 2032 break :blk token; 2033 }; 2034 return .{ .io = io, .dir = dir, .auto_login = auto_login }; 2035 } 2036 2037 pub fn deinit(self: SessionStore) void { 2038 self.dir.close(self.io); 2039 } 2040 2041 // mint a new session for user_id, returning the token hex to set as the 2042 // cookie value. 2043 pub fn create(self: SessionStore, user_id: *const [evt.event_id_size]u8) ![token_hex_len]u8 { 2044 var token: [evt.event_id_size]u8 = undefined; 2045 self.io.random(&token); 2046 const token_hex = std.fmt.bytesToHex(token, .lower); 2047 const file = try self.dir.createFile(self.io, &token_hex, .{}); 2048 defer file.close(self.io); 2049 try file.writeStreamingAll(self.io, user_id); 2050 return token_hex; 2051 } 2052 2053 // resolve a cookie's token to its user_id. returns false (logged out) for a 2054 // missing, malformed, or unknown token. 2055 pub fn lookup(self: SessionStore, token_hex: []const u8, out: *[evt.event_id_size]u8) bool { 2056 if (!isToken(token_hex)) return false; 2057 const file = self.dir.openFile(self.io, token_hex, .{ .mode = .read_only }) catch return false; 2058 defer file.close(self.io); 2059 var storage: [evt.event_id_size]u8 = undefined; 2060 var file_reader = file.reader(self.io, &storage); 2061 file_reader.interface.readSliceAll(out) catch return false; 2062 return true; 2063 } 2064 2065 // revoke a session. a no-op for a missing or malformed token. 2066 pub fn remove(self: SessionStore, token_hex: []const u8) void { 2067 if (!isToken(token_hex)) return; 2068 self.dir.deleteFile(self.io, token_hex) catch {}; 2069 } 2070 2071 // leave `token_hex`'s session for visitors arriving without one. only the 2072 // `try` fixture writes one. the name can't collide with a session token. 2073 pub fn offerAutoLogin(self: SessionStore, token_hex: *const [token_hex_len]u8) !void { 2074 const file = try self.dir.createFile(self.io, auto_login_name, .{}); 2075 defer file.close(self.io); 2076 try file.writeStreamingAll(self.io, token_hex); 2077 } 2078 2079 pub fn autoLogin(self: SessionStore, out: *[token_hex_len]u8) bool { 2080 out.* = self.auto_login orelse return false; 2081 return true; 2082 } 2083 2084 const auto_login_name = "auto-login"; 2085 2086 // a token is exactly token_hex_len hex chars. validating before using the 2087 // value as a path keeps attacker-supplied cookies from escaping the 2088 // sessions dir (e.g. via "/" or ".." bytes). 2089 fn isToken(token_hex: []const u8) bool { 2090 if (token_hex.len != token_hex_len) return false; 2091 for (token_hex) |c| if (!std.ascii.isHex(c)) return false; 2092 return true; 2093 } 2094 }; 2095