╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ web.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1937 self.io.random(&token); 1938 const token_hex = std.fmt.bytesToHex(token, .lower); 1939 const file = try self.dir.createFile(self.io, &token_hex, .{}); 1940 defer file.close(self.io); 1941 try file.writeStreamingAll(self.io, user_id); 1942 return token_hex; 1943 } 1944 1945 // resolve a cookie's token to its user_id. returns false (logged out) for a 1946 // missing, malformed, or unknown token. 1947 pub fn lookup(self: SessionStore, token_hex: []const u8, out: *[evt.event_id_size]u8) bool { 1948 if (!isToken(token_hex)) return false; 1949 const file = self.dir.openFile(self.io, token_hex, .{ .mode = .read_only }) catch return false; 1950 defer file.close(self.io); 1951 var storage: [evt.event_id_size]u8 = undefined; 1952 var file_reader = file.reader(self.io, &storage); 1953 file_reader.interface.readSliceAll(out) catch return false; 1954 return true; 1955 } 1956 1957 // revoke a session. a no-op for a missing or malformed token. 1958 pub fn remove(self: SessionStore, token_hex: []const u8) void { 1959 if (!isToken(token_hex)) return; 1960 self.dir.deleteFile(self.io, token_hex) catch {}; 1961 } 1962 1963 // leave `token_hex`'s session for the next visitor arriving without one. 1964 // only the `try` fixture writes one, and only a debug build honors it. the 1965 // name isn't token-shaped, so it can't collide with a session file. 1966 pub fn offerAutoLogin(self: SessionStore, token_hex: *const [token_hex_len]u8) !void { 1967 const file = try self.dir.createFile(self.io, auto_login_name, .{}); 1968 defer file.close(self.io); 1969 try file.writeStreamingAll(self.io, token_hex); 1970 } 1971 1972 // claim the offered session, if one is there. the delete picks the winner 1973 // of a race, so the offer is only ever taken once. a release build never 1974 // looks, because that would be an extra syscall for every GET request. 1975 pub fn claimAutoLogin(self: SessionStore, out: *[token_hex_len]u8) bool { 1976 if (builtin.mode != .Debug) return false; 1977 { 1978 const file = self.dir.openFile(self.io, auto_login_name, .{ .mode = .read_only }) catch return false; 1979 defer file.close(self.io); 1980 var storage: [token_hex_len]u8 = undefined; 1981 var file_reader = file.reader(self.io, &storage); 1982 file_reader.interface.readSliceAll(out) catch return false; 1983 } 1984 self.dir.deleteFile(self.io, auto_login_name) catch return false; 1985 return true; 1986 } 1987 1988 const auto_login_name = "auto-login"; 1989 1990 // a token is exactly token_hex_len hex chars. validating before using the 1991 // value as a path keeps attacker-supplied cookies from escaping the 1992 // sessions dir (e.g. via "/" or ".." bytes). 1993 fn isToken(token_hex: []const u8) bool { 1994 if (token_hex.len != token_hex_len) return false; 1995 for (token_hex) |c| if (!std.ascii.isHex(c)) return false; 1996 return true; 1997 } 1998 }; 1999