╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ UserKeys.zig ─────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭ find file ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ ╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │ │ │http│ ssh │ │ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
13 14 const wasm = builtin.target.cpu.arch == .wasm32; 15 16 // the logged-in user's ssh keys, one authorized_keys line each 17 keys: []const []const u8 = &.{}, 18 // the key whose removal the route asks to confirm, which stands in for the list 19 confirm: ?[]const u8 = null, 20 21 const Self = @This(); 22 23 pub fn init(arena: *std.heap.ArenaAllocator, session: *ui.Session, user_id: *const [evt.event_id_size]u8, tab: ui.RoutablePage.UserTab) !Self { 24 const moment = session.haxy_moment orelse return .{}; 25 const user = (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, user_id)) orelse return .{}; 26 var result: Self = .{}; 27 var keys: std.ArrayList([]const u8) = .empty; 28 var lines = std.mem.splitScalar(u8, user.event.ssh_keys, '\n'); 29 while (lines.next()) |line| { 30 const fingerprint = evt.User.fingerprintOfKey(line) orelse continue; 31 try keys.append(arena.allocator(), line); 32 if (tab.remove_key and std.mem.eql(u8, &fingerprint, tab.key.slice())) result.confirm = line; 33 } 34 result.keys = keys.items; 35 return result; 36 } 37 38 // the confirmation of removing `key`, under the user tab `route` 39 fn removeRoute(route: ui.RoutablePage, key: []const u8) ui.RoutablePage { 40 const fingerprint = evt.User.fingerprintOfKey(key) orelse unreachable; 41 const fingerprint_array = ui.RoutablePage.Array(evt.User.fingerprint_len).from(&fingerprint) orelse unreachable; 42 return route.withUserTab(.{ .view = .keys, .key = fingerprint_array, .remove_key = true }); 43 } 44 45 // a key's comment, or its type when it has none 46 fn keyLabel(key: []const u8) []const u8 { 47 var tokens = std.mem.tokenizeAny(u8, key, " \t"); 48 const key_type = tokens.next() orelse return ""; 49 _ = tokens.next(); 50 const comment = std.mem.trim(u8, tokens.rest(), " \t"); 51 return if (comment.len > 0) comment else key_type; 52 } 53 54 const add_label = " add ssh key "; 55 const input_width = 60; 56 57 pub const View = struct { 58 scroll: wgt.Scroll(ui.Widget), 59 data: *const Self, 60 session: *ui.Session, 61 62 const add_index = 0; 63 64 // `route` is the ssh keys route, which the forms post under 65 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session, route: ui.RoutablePage) !View { 66 const aa = session.page_arena.allocator(); 67 if (data.confirm) |key| { 68 const remove_url = try removeRoute(route, key).toUrl(session.page_arena); 69 var center = try ui.widget.initConfirmationForm(allocator, try aa.print("form:{s}", .{remove_url}), &.{ 70 .{ .label = "remove ssh key", .bottom_label = try aa.print(" {s} ", .{keyLabel(key)}) }, 71 }); 72 errdefer center.deinit(allocator); 73 return .{ 74 .scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .center = center }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }), 75 .data = data, 76 .session = session, 77 }; 78 } 79 80 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .vert }); 81 errdefer box.deinit(allocator); 82 const url = try route.toUrl(session.page_arena); 83 84 { 85 var add = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .horiz }); 86 errdefer add.deinit(allocator); 87 add.getFocus().kind = .{ .custom = try aa.print("form:{s}/addkey", .{url}) }; 88 var key = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = add_label }, .name = "key", .visible_width = input_width, .round_corners = true, .render_content = session.is_terminal }); 89 errdefer key.deinit(allocator); 90 key.getFocus().mode = .all; 91 if (session.formFeedback(.user_keys)) |saved| try key.setContent(allocator, saved.key); 92 add.getFocus().child_id = key.getFocus().id; 93 try add.children.put(allocator, key.getFocus().id, .{ .widget = .{ .text_input = key }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 94 try box.children.put(allocator, add.getFocus().id, .{ .widget = .{ .box = add }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 95 } 96 97 // one row per key, whose ✕ asks to confirm its removal 98 for (data.keys) |key| { 99 var row = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .horiz }); 100 errdefer row.deinit(allocator); 101 { 102 // the fingerprint ssh tools print, which an ssh refusal names too 103 const fingerprint = if (evt.User.fingerprintOfKey(key)) |value| try aa.print(" {s} ", .{&value}) else ""; 104 var text_input = try wgt.TextInput.init(allocator, .{ 105 .top_label = .{ .text = try aa.print(" {s} ", .{keyLabel(key)}) }, 106 .bottom_label = .{ .text = fingerprint }, 107 .visible_width = input_width, 108 .round_corners = true, 109 .read_only = true, 110 .render_content = session.is_terminal, 111 }); 112 errdefer text_input.deinit(allocator); 113 text_input.getFocus().mode = .all; 114 try text_input.setContent(allocator, key); 115 // show the start of the key, as the web's input does 116 text_input.cursor = 0; 117 try row.children.put(allocator, text_input.getFocus().id, .{ .widget = .{ .text_input = text_input }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 118 } 119 { 120 var remove = try wgt.TextBox.init(allocator, "✕", .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 121 errdefer remove.deinit(allocator); 122 remove.getFocus().mode = .all; 123 remove.getFocus().kind = .{ .custom = try aa.print("a:{s}", .{try removeRoute(route, key).toUrl(session.page_arena)}) }; 124 try row.children.put(allocator, remove.getFocus().id, .{ .widget = .{ .text_box = remove }, .rect = null, .min_size = .{ .width = 3, .height = null } }); 125 } 126 row.getFocus().child_id = row.children.keys()[0]; 127 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .box = row }, .rect = null, .min_size = null }); 128 } 129 130 box.getFocus().child_id = box.children.keys()[add_index]; 131 var center = try ui.widget.Center.init(allocator, .{ .box = box }); 132 errdefer center.deinit(allocator); 133 return .{ 134 .scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .center = center }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }), 135 .data = data, 136 .session = session, 137 }; 138 } 139 140 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 141 self.scroll.deinit(allocator); 142 } 143 144 fn contentBox(self: *View) *wgt.Box(ui.Widget) { 145 return &self.scroll.child.center.child.box; 146 } 147 148 fn addBox(self: *View) *wgt.Box(ui.Widget) { 149 return &self.contentBox().children.values()[add_index].widget.box; 150 } 151 152 fn addInput(self: *View) *wgt.TextInput { 153 return &self.addBox().children.values()[0].widget.text_input; 154 } 155 156 fn addActive(self: *View) bool { 157 return self.contentBox().getFocus().child_id == self.addBox().getFocus().id; 158 } 159 160 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 161 self.clearGrid(); 162 if (self.data.confirm == null) try self.labelInputs(root_focus); 163 // the window's height as a minimum centers rows that fit 164 try self.scroll.build(allocator, .{ 165 .min_size = .{ .width = constraint.min_size.width, .height = constraint.max_size.height orelse constraint.min_size.height }, 166 .max_size = constraint.max_size, 167 }, root_focus); 168 } 169 170 // label the add input for this frame and register every input for the 171 // web form handling 172 fn labelInputs(self: *View, root_focus: *Focus) !void { 173 const add = self.addInput(); 174 const failure = if (self.session.formFeedback(.user_keys)) |saved| saved.failure else null; 175 add.options.top_label.text = if (failure) |value| switch (value) { 176 .invalid => " add ssh key (invalid) ", 177 .unsupported => " add ssh key (unsupported type) ", 178 .already_added => " add ssh key (already added) ", 179 .too_many => " add ssh key (too many keys) ", 180 } else add_label; 181 add.options.bottom_label.text = if (root_focus.grandchild_id == add.getFocus().id) " press enter " else ""; 182 // the web form handling finds the inputs by focus id 183 const inputs_arena = self.session.arena.allocator(); 184 for (self.contentBox().children.values()) |*child| { 185 const text_input = &child.widget.box.children.values()[0].widget.text_input; 186 try self.session.text_inputs.put(inputs_arena, text_input.getFocus().id, text_input); 187 } 188 } 189 190 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 191 if (self.data.confirm) |confirmed| { 192 const button_id = self.contentBox().getFocus().child_id orelse return; 193 if (inp.activated(root_focus, button_id, key)) try self.removeKey(allocator, confirmed); 194 return; 195 } 196 // the add row keeps every key but the vertical ones, which move between rows 197 if (self.addActive() and inp.vertDirection(key) == .none) { 198 if (key == .enter) { 199 const text = try self.addInput().text(allocator); 200 defer allocator.free(text); 201 try self.addKey(allocator, text); 202 } else try self.addInput().input(allocator, key, root_focus); 203 return; 204 } 205 206 const rows = self.contentBox(); 207 if (inp.rowDelta(key, @intCast(rows.children.count()))) |delta| { 208 ui.widget.moveRowFocus(rows, &self.scroll, root_focus, delta); 209 return; 210 } 211 // a row's ✕ is a link the host follows 212 if (key == .arrow_left or key == .arrow_right) _ = ui.widget.moveInSelectedRow(rows, root_focus, key == .arrow_right); 213 } 214 215 // the terminal changes the keys itself; the web posts the form instead 216 fn addKey(self: *View, allocator: std.mem.Allocator, key: []const u8) !void { 217 if (comptime wasm) return; 218 const io = self.session.io orelse return; 219 const users_dir = self.session.users_dir orelse return; 220 const admin_repo = self.session.admin_repo orelse return; 221 const user_id = self.session.userId() orelse return; 222 evt.addUserKey(io, allocator, users_dir, admin_repo, &user_id, key) catch |err| switch (err) { 223 // the account was removed under them 224 error.NotFound => return ui.UserNotifications.logOut(self.session), 225 else => { 226 const failure = ui.Session.FormFeedback.KeyFailure.fromError(err) orelse return err; 227 self.session.data.form_feedback = .{ .user_keys = .{ .failure = failure, .key = try self.session.arena.allocator().dupe(u8, key) } }; 228 return; 229 }, 230 }; 231 // reload the page with the keys as they are now 232 self.session.haxy_moment = try evt.currentMoment(evt.admin_repo_opts, admin_repo); 233 try self.session.navigate(self.session.data.current_page); 234 } 235 236 // the terminal removes the key itself; the web posts the form instead 237 fn removeKey(self: *View, allocator: std.mem.Allocator, key: []const u8) !void { 238 if (comptime wasm) return; 239 const io = self.session.io orelse return; 240 const users_dir = self.session.users_dir orelse return; 241 const admin_repo = self.session.admin_repo orelse return; 242 const user_id = self.session.userId() orelse return; 243 const fingerprint = evt.User.fingerprintOfKey(key) orelse unreachable; 244 evt.removeUserKey(io, allocator, users_dir, admin_repo, &user_id, &fingerprint) catch |err| switch (err) { 245 // the account was removed under them 246 error.NotFound => return ui.UserNotifications.logOut(self.session), 247 else => return err, 248 }; 249 self.session.haxy_moment = try evt.currentMoment(evt.admin_repo_opts, admin_repo); 250 try self.session.navigate(self.session.data.current_page.withUserTab(.{ .view = .keys })); 251 } 252 253 pub fn clearGrid(self: *View) void { 254 self.scroll.clearGrid(); 255 } 256 257 pub fn getGrid(self: View) ?Grid { 258 return self.scroll.getGrid(); 259 } 260 261 pub fn getFocus(self: *View) *Focus { 262 return self.scroll.getFocus(); 263 } 264 265 // up leaves the view from the add row or the confirmation 266 pub fn atTop(self: *View) bool { 267 return self.data.confirm != null or self.addActive(); 268 } 269 }; 270