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