╭ User.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const evt = @import("../event.zig"); 3 const xit = @import("xit"); 4 const rp = xit.repo; 5 const hash = xit.hash; 6 const bcrypt = std.crypto.pwhash.bcrypt; 7 8 name: []const u8, 9 display_name: []const u8, 10 email: []const u8, 11 password_hash: []const u8, 12 enable_ansi: bool = true, 13 // newline-separated authorized_keys lines (one OpenSSH public key per line) 14 ssh_keys: []const u8 = "", 15 16 const Self = @This(); 17 18 pub const name_max_len = 32; 19 20 // the subset of a user that's safe to hand to clients 21 pub const Safe = struct { 22 name: []const u8, 23 display_name: []const u8, 24 25 pub fn init(user: Self) Safe { 26 return .{ .name = user.name, .display_name = user.display_name }; 27 } 28 }; 29 30 pub fn consume( 31 comptime DB: type, 32 comptime hash_kind: hash.HashKind, 33 haxy_moment: DB.HashMap(.read_write), 34 event_id: *const [evt.event_id_size]u8, 35 event_maybe: ?@This(), 36 arena: *std.heap.ArenaAllocator, 37 ) !void { 38 const user_key = hash.hashInt(hash_kind, event_id); 39 40 const event_id_to_user_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "event-id->user")); 41 const event_id_to_user = try DB.HashMap(.read_write).init(event_id_to_user_cursor); 42 43 const name_to_user_id_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "name->user-id")); 44 const name_to_user_id = try DB.HashMap(.read_write).init(name_to_user_id_cursor); 45 46 if (event_maybe) |event| { 47 if (event.name.len > name_max_len) return error.NameTooLong; 48 49 // if this event_id already maps to a user with a different name, 50 // drop the stale name->id entry first 51 const existing_cursor_maybe = try event_id_to_user.getCursor(user_key); 52 if (existing_cursor_maybe) |existing_cursor| { 53 const existing_user = try DB.HashMap(.read_only).init(existing_cursor); 54 const existing_event = try evt.read(@This(), DB, hash_kind, arena, existing_user); 55 if (!std.mem.eql(u8, existing_event.name, event.name)) { 56 _ = try name_to_user_id.remove(hash.hashInt(hash_kind, existing_event.name)); 57 } 58 } 59 60 const user_cursor = try event_id_to_user.putCursor(user_key); 61 const user = try DB.HashMap(.read_write).init(user_cursor); 62 try evt.upsert(@This(), DB, hash_kind, user, event); 63 64 try name_to_user_id.put(hash.hashInt(hash_kind, event.name), .{ .bytes = event_id }); 65 66 // first time we've seen this user: append its id to the ordered list 67 // the users view paginates through (a delete leaves a tombstone here). 68 if (existing_cursor_maybe == null) { 69 const user_list_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "user-list")); 70 const user_list = try DB.ArrayList(.read_write).init(user_list_cursor); 71 try user_list.append(.{ .bytes = event_id }); 72 } 73 } else { 74 // read the user's name so we can drop its name->id index entry 75 if (try event_id_to_user.getCursor(user_key)) |existing_cursor| { 76 const existing_user = try DB.HashMap(.read_only).init(existing_cursor); 77 const existing_event = try evt.read(@This(), DB, hash_kind, arena, existing_user); 78 _ = try name_to_user_id.remove(hash.hashInt(hash_kind, existing_event.name)); 79 } 80 81 if (!try event_id_to_user.remove(user_key)) return error.EventNotFound; 82 83 const user_id_to_repos_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "user-id->repos")); 84 const user_id_to_repos = try DB.HashMap(.read_write).init(user_id_to_repos_cursor); 85 _ = try user_id_to_repos.remove(user_key); 86 } 87 } 88 89 pub const password_hash_max_len = bcrypt.hash_length * 2; 90 91 pub fn hashPassword( 92 password: []const u8, 93 out: []u8, 94 io: std.Io, 95 ) ![]const u8 { 96 return bcrypt.strHash(password, .{ 97 .params = bcrypt.Params.owasp, 98 .encoding = .phc, 99 }, out, io); 100 } 101 102 pub const VerifyResult = union(enum) { 103 success: [evt.event_id_size]u8, 104 unknown_user, 105 wrong_password, 106 }; 107 108 // look up a user by name (via the name->user-id index) and verify the 109 // supplied password against the stored bcrypt hash. used by both the TTY 110 // login submit and the server's /login route. 111 pub fn verifyCredentials( 112 comptime DB: type, 113 comptime hash_kind: hash.HashKind, 114 haxy_moment: DB.HashMap(.read_only), 115 arena: *std.heap.ArenaAllocator, 116 name: []const u8, 117 password: []const u8, 118 ) !VerifyResult { 119 const name_index_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "name->user-id")) orelse return .unknown_user; 120 const name_index = try DB.HashMap(.read_only).init(name_index_cursor); 121 122 const user_id_cursor = try name_index.getCursor(hash.hashInt(hash_kind, name)) orelse return .unknown_user; 123 var user_id: [evt.event_id_size]u8 = undefined; 124 _ = try user_id_cursor.readBytes(&user_id); 125 126 const user_key = hash.hashInt(hash_kind, &user_id); 127 const event_id_to_user_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "event-id->user")) orelse return .unknown_user; 128 const event_id_to_user = try DB.HashMap(.read_only).init(event_id_to_user_cursor); 129 130 const user_cursor = try event_id_to_user.getCursor(user_key) orelse return .unknown_user; 131 const user_map = try DB.HashMap(.read_only).init(user_cursor); 132 const user_event = try evt.read(@This(), DB, hash_kind, arena, user_map); 133 134 bcrypt.strVerify(user_event.password_hash, password, .{ .silently_truncate_password = false }) catch { 135 return .wrong_password; 136 }; 137 138 return .{ .success = user_id }; 139 } 140 141 // read a user by event id via the event-id->user index, or null if the id 142 // isn't a known user. field byte slices are allocated in `arena`. 143 pub fn readById( 144 comptime DB: type, 145 comptime hash_kind: hash.HashKind, 146 haxy_moment: DB.HashMap(.read_only), 147 arena: *std.heap.ArenaAllocator, 148 user_id: []const u8, 149 ) !?@This() { 150 const event_id_to_user_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "event-id->user")) orelse return null; 151 const event_id_to_user = try DB.HashMap(.read_only).init(event_id_to_user_cursor); 152 const user_cursor = try event_id_to_user.getCursor(hash.hashInt(hash_kind, user_id)) orelse return null; 153 const user_map = try DB.HashMap(.read_only).init(user_cursor); 154 return try evt.read(@This(), DB, hash_kind, arena, user_map); 155 } 156 157 // read a user by name from the admin event store, or null if the admin repo or 158 // the user doesn't exist. field byte slices are allocated in `arena`. 159 pub fn readByName( 160 io: std.Io, 161 allocator: std.mem.Allocator, 162 admin_repo_path: []const u8, 163 arena: *std.heap.ArenaAllocator, 164 name: []const u8, 165 ) !?Self { 166 var repo = rp.Repo(.xit, evt.admin_repo_opts).open(io, allocator, .{ .path = admin_repo_path }) catch |err| switch (err) { 167 error.RepoNotFound => return null, 168 else => |e| return e, 169 }; 170 defer repo.deinit(io, allocator); 171 172 const moment = try evt.currentMoment(evt.admin_repo_opts, &repo); 173 174 const name_to_user_id_cursor = try moment.getCursor(hash.hashInt(evt.admin_repo_opts.hash, "name->user-id")) orelse return null; 175 const name_to_user_id = try evt.AdminDB.HashMap(.read_only).init(name_to_user_id_cursor); 176 const user_id_cursor = try name_to_user_id.getCursor(hash.hashInt(evt.admin_repo_opts.hash, name)) orelse return null; 177 var user_id: [evt.event_id_size]u8 = undefined; 178 _ = try user_id_cursor.readBytes(&user_id); 179 180 return try readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, &user_id); 181 } 182 183 // flip a user's ANSI-art preference by re-emitting their User event with 184 // enable_ansi negated. a no-op for an unknown user. `repo` must be writable. 185 pub fn toggleAnsi( 186 comptime repo_opts: rp.RepoOpts(.xit), 187 io: std.Io, 188 allocator: std.mem.Allocator, 189 repo: *rp.Repo(.xit, repo_opts), 190 user_id: []const u8, 191 ) !void { 192 const DB = rp.Repo(.xit, repo_opts).DB; 193 var arena = std.heap.ArenaAllocator.init(allocator); 194 defer arena.deinit(); 195 196 const moment = try evt.currentMoment(repo_opts, repo); 197 const user = (try readById(DB, repo_opts.hash, moment, &arena, user_id)) orelse return; 198 199 var updated = user; 200 updated.enable_ansi = !user.enable_ansi; 201 try evt.commitAndConsume(repo_opts, io, allocator, repo, evt.events_ref, &[_]evt.EventWithId{.{ 202 .id = std.fmt.bytesToHex(user_id[0..evt.event_id_size].*, .lower), 203 .event = .{ .user = updated }, 204 }}); 205 } 206