╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ web.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1551 defer file.close(self.io); 1552 try file.writeStreamingAll(self.io, user_id); 1553 return token_hex; 1554 } 1555 1556 // resolve a cookie's token to its user_id. returns false (logged out) for a 1557 // missing, malformed, or unknown token. 1558 pub fn lookup(self: SessionStore, token_hex: []const u8, out: *[evt.event_id_size]u8) bool { 1559 if (!isToken(token_hex)) return false; 1560 const file = self.dir.openFile(self.io, token_hex, .{ .mode = .read_only }) catch return false; 1561 defer file.close(self.io); 1562 var storage: [evt.event_id_size]u8 = undefined; 1563 var file_reader = file.reader(self.io, &storage); 1564 file_reader.interface.readSliceAll(out) catch return false; 1565 return true; 1566 } 1567 1568 // revoke a session. a no-op for a missing or malformed token. 1569 pub fn remove(self: SessionStore, token_hex: []const u8) void { 1570 if (!isToken(token_hex)) return; 1571 self.dir.deleteFile(self.io, token_hex) catch {}; 1572 } 1573 1574 // leave `token_hex`'s session for the next visitor arriving without one. 1575 // only the `try` fixture writes one, and only a debug build honors it. the 1576 // name isn't token-shaped, so it can't collide with a session file. 1577 pub fn offerAutoLogin(self: SessionStore, token_hex: *const [token_hex_len]u8) !void { 1578 const file = try self.dir.createFile(self.io, auto_login_name, .{}); 1579 defer file.close(self.io); 1580 try file.writeStreamingAll(self.io, token_hex); 1581 } 1582 1583 // claim the offered session, if one is there. the delete picks the winner 1584 // of a race, so the offer is only ever taken once. a release build never 1585 // looks, because that would be an extra syscall for every GET request. 1586 pub fn claimAutoLogin(self: SessionStore, out: *[token_hex_len]u8) bool { 1587 if (builtin.mode != .Debug) return false; 1588 { 1589 const file = self.dir.openFile(self.io, auto_login_name, .{ .mode = .read_only }) catch return false; 1590 defer file.close(self.io); 1591 var storage: [token_hex_len]u8 = undefined; 1592 var file_reader = file.reader(self.io, &storage); 1593 file_reader.interface.readSliceAll(out) catch return false; 1594 } 1595 self.dir.deleteFile(self.io, auto_login_name) catch return false; 1596 return true; 1597 } 1598 1599 const auto_login_name = "auto-login"; 1600 1601 // a token is exactly token_hex_len hex chars. validating before using the 1602 // value as a path keeps attacker-supplied cookies from escaping the 1603 // sessions dir (e.g. via "/" or ".." bytes). 1604 fn isToken(token_hex: []const u8) bool { 1605 if (token_hex.len != token_hex_len) return false; 1606 for (token_hex) |c| if (!std.ascii.isHex(c)) return false; 1607 return true; 1608 } 1609 }; 1610