╭ 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 email: []const u8, 10 password_hash: []const u8, 11 enable_ansi: bool = true, 12 ssh_keys: []const u8 = "", // newline-separated authorized_keys lines (one OpenSSH public key per line) 13 14 // what the db stores: the event's data plus the commit-derived fields 15 pub const Record = struct { 16 event: Self, 17 removed: bool = false, 18 created_order: u64 = 0, 19 20 // a user's key in the name index 21 pub fn indexKey(self: Record, allocator: std.mem.Allocator) ![]const u8 { 22 _ = allocator; 23 return self.event.name; 24 } 25 }; 26 27 // the subset of a user that anyone may see 28 pub const Public = struct { 29 name: []const u8, 30 }; 31 32 const Self = @This(); 33 34 pub const name_max_len = 32; 35 36 // the moment keys `evt.merge` reads and writes for this kind 37 pub const merge_policy: evt.MergePolicy = .target_wins; 38 pub const record_map_key = "event-id->user"; 39 pub const id_set_key = "user-id-set"; 40 pub const name_index_key = "name->user-id"; 41 42 // resolves a commit's author email to its user at read time 43 pub const email_to_user_id_key = "email->user-id"; 44 45 pub fn validateName(name: []const u8) !void { 46 if (name.len == 0) return error.NameEmpty; 47 if (name.len > name_max_len) return error.NameTooLong; 48 if (name[0] == '-' or name[name.len - 1] == '-') return error.InvalidName; 49 50 var previous_was_hyphen = false; 51 for (name) |c| { 52 if (std.ascii.isAlphanumeric(c)) { 53 previous_was_hyphen = false; 54 } else if (c == '-' and !previous_was_hyphen) { 55 previous_was_hyphen = true; 56 } else { 57 return error.InvalidName; 58 } 59 } 60 } 61 62 pub fn consume( 63 comptime DB: type, 64 comptime hash_kind: hash.HashKind, 65 haxy_moment: DB.HashMap(.read_write), 66 event_id: *const [evt.event_id_size]u8, 67 record_maybe: ?Record, 68 arena: *std.heap.ArenaAllocator, 69 _: ?[]const u8, 70 ) !void { 71 const user_key = hash.hashInt(hash_kind, event_id); 72 73 const event_id_to_user_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "event-id->user")); 74 const event_id_to_user = try DB.HashMap(.read_write).init(event_id_to_user_cursor); 75 76 const name_to_user_id_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "name->user-id")); 77 const name_to_user_id = try DB.HashMap(.read_write).init(name_to_user_id_cursor); 78 79 const email_to_user_id_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, email_to_user_id_key)); 80 const email_to_user_id = try DB.HashMap(.read_write).init(email_to_user_id_cursor); 81 82 var existing_record_maybe: ?Record = null; 83 const existing_cursor_maybe = try event_id_to_user.getCursor(user_key); 84 if (existing_cursor_maybe) |existing_cursor| { 85 const existing_user = try DB.HashMap(.read_only).init(existing_cursor); 86 existing_record_maybe = try evt.read(Record, DB, hash_kind, arena, existing_user); 87 } 88 89 var record_to_write = if (record_maybe) |record| 90 record 91 else blk: { 92 var record = existing_record_maybe orelse return error.EventNotFound; 93 record.removed = true; 94 break :blk record; 95 }; 96 97 if (!record_to_write.removed) try validateName(record_to_write.event.name); 98 99 if (existing_record_maybe) |existing_record| { 100 // updates preserve the original creation metadata 101 record_to_write.created_order = existing_record.created_order; 102 103 // drop the old active indexes; active values are re-added below 104 if (!existing_record.removed) { 105 _ = try name_to_user_id.remove(hash.hashInt(hash_kind, existing_record.event.name)); 106 _ = try email_to_user_id.remove(hash.hashInt(hash_kind, existing_record.event.email)); 107 } 108 } 109 110 const user_cursor = try event_id_to_user.putCursor(user_key); 111 const user = try DB.HashMap(.read_write).init(user_cursor); 112 try evt.upsert(Record, DB, hash_kind, user, record_to_write); 113 try evt.indexEvent(DB, hash_kind, haxy_moment, event_id, .user, record_to_write.created_order, record_to_write.removed); 114 115 const order_key = evt.orderKeyDesc(record_to_write.created_order, event_id); 116 117 // the id set retains removed records so merges can carry removals 118 if (existing_cursor_maybe == null) { 119 const user_id_set_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, id_set_key)); 120 const user_id_set = try DB.SortedSet(.read_write).init(user_id_set_cursor); 121 try user_id_set.put(&order_key); 122 } 123 124 if (!record_to_write.removed) { 125 try name_to_user_id.put(hash.hashInt(hash_kind, record_to_write.event.name), .{ .bytes = event_id }); 126 try email_to_user_id.put(hash.hashInt(hash_kind, record_to_write.event.email), .{ .bytes = event_id }); 127 } 128 129 const became_removed = record_to_write.removed and if (existing_record_maybe) |existing| !existing.removed else true; 130 if (became_removed) { 131 // the user's repos go with them: the repo name index is keyed by owner 132 // id, so one left behind is listed but can never be resolved again 133 const user_id_to_repo_id_set_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "user-id->repo-id-set")); 134 const user_id_to_repo_id_set = try DB.HashMap(.read_write).init(user_id_to_repo_id_set_cursor); 135 if (try user_id_to_repo_id_set.getCursor(user_key)) |user_repos_cursor| { 136 const user_repos = try DB.SortedSet(.read_only).init(user_repos_cursor); 137 138 // collected up front, since removing a repo removes it from this set 139 var repo_ids: std.ArrayList([evt.event_id_size]u8) = .empty; 140 var repo_iter = try user_repos.iteratorFromIndex(0); 141 while (try repo_iter.next()) |kv_pair_cursor| { 142 try repo_ids.append(arena.allocator(), try evt.readOrderKeyId(DB, kv_pair_cursor)); 143 } 144 145 for (repo_ids.items) |*repo_id| { 146 try evt.Repo.consume(DB, hash_kind, haxy_moment, repo_id, null, arena, null); 147 } 148 } 149 _ = try user_id_to_repo_id_set.remove(user_key); 150 } 151 } 152 153 pub const password_hash_max_len = bcrypt.hash_length * 2; 154 155 pub fn hashPassword( 156 password: []const u8, 157 out: []u8, 158 io: std.Io, 159 ) ![]const u8 { 160 return bcrypt.strHash(password, .{ 161 .params = bcrypt.Params.owasp, 162 .encoding = .phc, 163 }, out, io); 164 } 165 166 pub const VerifyResult = union(enum) { 167 success: [evt.event_id_size]u8, 168 unknown_user, 169 wrong_password, 170 }; 171 172 // look up a user by name (via the name->user-id index) and verify the 173 // supplied password against the stored bcrypt hash. used by both the TTY 174 // login submit and the server's /login route. 175 pub fn verifyCredentials( 176 comptime DB: type, 177 comptime hash_kind: hash.HashKind, 178 haxy_moment: DB.HashMap(.read_only), 179 arena: *std.heap.ArenaAllocator, 180 name: []const u8, 181 password: []const u8, 182 ) !VerifyResult { 183 const name_index_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "name->user-id")) orelse return .unknown_user; 184 const name_index = try DB.HashMap(.read_only).init(name_index_cursor); 185 186 const user_id_cursor = try name_index.getCursor(hash.hashInt(hash_kind, name)) orelse return .unknown_user; 187 var user_id: [evt.event_id_size]u8 = undefined; 188 _ = try user_id_cursor.readBytes(&user_id); 189 190 const user_key = hash.hashInt(hash_kind, &user_id); 191 const event_id_to_user_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "event-id->user")) orelse return .unknown_user; 192 const event_id_to_user = try DB.HashMap(.read_only).init(event_id_to_user_cursor); 193 194 const user_cursor = try event_id_to_user.getCursor(user_key) orelse return .unknown_user; 195 const user_map = try DB.HashMap(.read_only).init(user_cursor); 196 const user_event = try evt.read(Record, DB, hash_kind, arena, user_map); 197 198 bcrypt.strVerify(user_event.event.password_hash, password, .{ .silently_truncate_password = false }) catch { 199 return .wrong_password; 200 }; 201 202 return .{ .success = user_id }; 203 } 204 205 // read a user by event id via the event-id->user index, or null if the id 206 // isn't a known user. field byte slices are allocated in `arena`. 207 pub fn readById( 208 comptime DB: type, 209 comptime hash_kind: hash.HashKind, 210 haxy_moment: DB.HashMap(.read_only), 211 arena: *std.heap.ArenaAllocator, 212 user_id: []const u8, 213 ) !?Record { 214 const event_id_to_user_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "event-id->user")) orelse return null; 215 const event_id_to_user = try DB.HashMap(.read_only).init(event_id_to_user_cursor); 216 const user_cursor = try event_id_to_user.getCursor(hash.hashInt(hash_kind, user_id)) orelse return null; 217 const user_map = try DB.HashMap(.read_only).init(user_cursor); 218 return try evt.read(Record, DB, hash_kind, arena, user_map); 219 } 220 221 // read a user by email via the email->user-id index, or null when no user has 222 // the email. field byte slices are allocated in `arena`. 223 pub fn readByEmail( 224 comptime DB: type, 225 comptime hash_kind: hash.HashKind, 226 haxy_moment: DB.HashMap(.read_only), 227 arena: *std.heap.ArenaAllocator, 228 email: []const u8, 229 ) !?Record { 230 const email_to_user_id_cursor = (try haxy_moment.getCursor(hash.hashInt(hash_kind, email_to_user_id_key))) orelse return null; 231 const email_to_user_id = try DB.HashMap(.read_only).init(email_to_user_id_cursor); 232 const user_id_cursor = (try email_to_user_id.getCursor(hash.hashInt(hash_kind, email))) orelse return null; 233 var user_id: [evt.event_id_size]u8 = undefined; 234 _ = try user_id_cursor.readBytes(&user_id); 235 return try readById(DB, hash_kind, haxy_moment, arena, &user_id); 236 } 237 238 // read a user by name from the admin event store, or null if the admin repo or 239 // the user doesn't exist. field byte slices are allocated in `arena`. 240 pub fn readByName( 241 io: std.Io, 242 allocator: std.mem.Allocator, 243 admin_repo_path: []const u8, 244 arena: *std.heap.ArenaAllocator, 245 name: []const u8, 246 ) !?Record { 247 var repo = rp.Repo(.xit, evt.admin_repo_opts).open(io, allocator, .{ .path = admin_repo_path }) catch |err| switch (err) { 248 error.RepoNotFound => return null, 249 else => |e| return e, 250 }; 251 defer repo.deinit(io, allocator); 252 253 const moment = try evt.currentMoment(evt.admin_repo_opts, &repo); 254 255 const name_to_user_id_cursor = try moment.getCursor(hash.hashInt(evt.admin_repo_opts.hash, "name->user-id")) orelse return null; 256 const name_to_user_id = try evt.AdminDB.HashMap(.read_only).init(name_to_user_id_cursor); 257 const user_id_cursor = try name_to_user_id.getCursor(hash.hashInt(evt.admin_repo_opts.hash, name)) orelse return null; 258 var user_id: [evt.event_id_size]u8 = undefined; 259 _ = try user_id_cursor.readBytes(&user_id); 260 261 return try readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, &user_id); 262 } 263 264 // flip a user's ANSI-art preference by re-emitting their User event with 265 // enable_ansi negated. a no-op for an unknown user. `repo` must be writable. 266 pub fn toggleAnsi( 267 comptime repo_opts: rp.RepoOpts(.xit), 268 io: std.Io, 269 allocator: std.mem.Allocator, 270 repo: *rp.Repo(.xit, repo_opts), 271 user_id: []const u8, 272 ) !void { 273 const DB = rp.Repo(.xit, repo_opts).DB; 274 var arena = std.heap.ArenaAllocator.init(allocator); 275 defer arena.deinit(); 276 277 const moment = try evt.currentMoment(repo_opts, repo); 278 const user = (try readById(DB, repo_opts.hash, moment, &arena, user_id)) orelse return; 279 280 var updated = user.event; 281 updated.enable_ansi = !updated.enable_ansi; 282 try evt.consume(.xit, repo_opts, io, allocator, repo, evt.events_ref, &[_]evt.EventWithId{.{ 283 .id = std.fmt.bytesToHex(user_id[0..evt.event_id_size].*, .lower), 284 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 285 .author = .{ .name = user.event.name, .email = user.event.email }, 286 .event = .{ .user = updated }, 287 }}); 288 } 289