╭ 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 try evt.indexEvent(DB, hash_kind, haxy_moment, event_id, .user, record_to_write.created_ts); 116 117 const order_key = evt.orderKeyDesc(record_to_write.created_ts, event_id); 118 119 // the id set retains tombstones so merges can carry deletions 120 if (existing_cursor_maybe == null) { 121 const user_id_set_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, id_set_key)); 122 const user_id_set = try DB.SortedSet(.read_write).init(user_id_set_cursor); 123 try user_id_set.put(&order_key); 124 } 125 126 if (!record_to_write.deleted) { 127 try name_to_user_id.put(hash.hashInt(hash_kind, record_to_write.event.name), .{ .bytes = event_id }); 128 try email_to_user_id.put(hash.hashInt(hash_kind, record_to_write.event.email), .{ .bytes = event_id }); 129 } 130 131 const became_deleted = record_to_write.deleted and if (existing_record_maybe) |existing| !existing.deleted else true; 132 if (became_deleted) { 133 // the user's repos go with them: the repo name index is keyed by owner 134 // id, so one left behind is listed but can never be resolved again 135 const user_id_to_repo_id_set_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "user-id->repo-id-set")); 136 const user_id_to_repo_id_set = try DB.HashMap(.read_write).init(user_id_to_repo_id_set_cursor); 137 if (try user_id_to_repo_id_set.getCursor(user_key)) |user_repos_cursor| { 138 const user_repos = try DB.SortedSet(.read_only).init(user_repos_cursor); 139 140 // collected up front, since deleting a repo removes it from this set 141 var repo_ids: std.ArrayList([evt.event_id_size]u8) = .empty; 142 var repo_iter = try user_repos.iteratorFromIndex(0); 143 while (try repo_iter.next()) |kv_pair_cursor| { 144 try repo_ids.append(arena.allocator(), try evt.readOrderKeyId(DB, kv_pair_cursor)); 145 } 146 147 for (repo_ids.items) |*repo_id| { 148 try evt.Repo.consume(DB, hash_kind, haxy_moment, repo_id, null, arena, null); 149 } 150 } 151 _ = try user_id_to_repo_id_set.remove(user_key); 152 } 153 } 154 155 pub const password_hash_max_len = bcrypt.hash_length * 2; 156 157 pub fn hashPassword( 158 password: []const u8, 159 out: []u8, 160 io: std.Io, 161 ) ![]const u8 { 162 return bcrypt.strHash(password, .{ 163 .params = bcrypt.Params.owasp, 164 .encoding = .phc, 165 }, out, io); 166 } 167 168 pub const VerifyResult = union(enum) { 169 success: [evt.event_id_size]u8, 170 unknown_user, 171 wrong_password, 172 }; 173 174 // look up a user by name (via the name->user-id index) and verify the 175 // supplied password against the stored bcrypt hash. used by both the TTY 176 // login submit and the server's /login route. 177 pub fn verifyCredentials( 178 comptime DB: type, 179 comptime hash_kind: hash.HashKind, 180 haxy_moment: DB.HashMap(.read_only), 181 arena: *std.heap.ArenaAllocator, 182 name: []const u8, 183 password: []const u8, 184 ) !VerifyResult { 185 const name_index_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "name->user-id")) orelse return .unknown_user; 186 const name_index = try DB.HashMap(.read_only).init(name_index_cursor); 187 188 const user_id_cursor = try name_index.getCursor(hash.hashInt(hash_kind, name)) orelse return .unknown_user; 189 var user_id: [evt.event_id_size]u8 = undefined; 190 _ = try user_id_cursor.readBytes(&user_id); 191 192 const user_key = hash.hashInt(hash_kind, &user_id); 193 const event_id_to_user_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "event-id->user")) orelse return .unknown_user; 194 const event_id_to_user = try DB.HashMap(.read_only).init(event_id_to_user_cursor); 195 196 const user_cursor = try event_id_to_user.getCursor(user_key) orelse return .unknown_user; 197 const user_map = try DB.HashMap(.read_only).init(user_cursor); 198 const user_event = try evt.read(Record, DB, hash_kind, arena, user_map); 199 200 bcrypt.strVerify(user_event.event.password_hash, password, .{ .silently_truncate_password = false }) catch { 201 return .wrong_password; 202 }; 203 204 return .{ .success = user_id }; 205 } 206 207 // read a user by event id via the event-id->user index, or null if the id 208 // isn't a known user. field byte slices are allocated in `arena`. 209 pub fn readById( 210 comptime DB: type, 211 comptime hash_kind: hash.HashKind, 212 haxy_moment: DB.HashMap(.read_only), 213 arena: *std.heap.ArenaAllocator, 214 user_id: []const u8, 215 ) !?Record { 216 const event_id_to_user_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "event-id->user")) orelse return null; 217 const event_id_to_user = try DB.HashMap(.read_only).init(event_id_to_user_cursor); 218 const user_cursor = try event_id_to_user.getCursor(hash.hashInt(hash_kind, user_id)) orelse return null; 219 const user_map = try DB.HashMap(.read_only).init(user_cursor); 220 return try evt.read(Record, DB, hash_kind, arena, user_map); 221 } 222 223 // read a user by email via the email->user-id index, or null when no user has 224 // the email. field byte slices are allocated in `arena`. 225 pub fn readByEmail( 226 comptime DB: type, 227 comptime hash_kind: hash.HashKind, 228 haxy_moment: DB.HashMap(.read_only), 229 arena: *std.heap.ArenaAllocator, 230 email: []const u8, 231 ) !?Record { 232 const email_to_user_id_cursor = (try haxy_moment.getCursor(hash.hashInt(hash_kind, email_to_user_id_key))) orelse return null; 233 const email_to_user_id = try DB.HashMap(.read_only).init(email_to_user_id_cursor); 234 const user_id_cursor = (try email_to_user_id.getCursor(hash.hashInt(hash_kind, email))) orelse return null; 235 var user_id: [evt.event_id_size]u8 = undefined; 236 _ = try user_id_cursor.readBytes(&user_id); 237 return try readById(DB, hash_kind, haxy_moment, arena, &user_id); 238 } 239 240 // read a user by name from the admin event store, or null if the admin repo or 241 // the user doesn't exist. field byte slices are allocated in `arena`. 242 pub fn readByName( 243 io: std.Io, 244 allocator: std.mem.Allocator, 245 admin_repo_path: []const u8, 246 arena: *std.heap.ArenaAllocator, 247 name: []const u8, 248 ) !?Record { 249 var repo = rp.Repo(.xit, evt.admin_repo_opts).open(io, allocator, .{ .path = admin_repo_path }) catch |err| switch (err) { 250 error.RepoNotFound => return null, 251 else => |e| return e, 252 }; 253 defer repo.deinit(io, allocator); 254 255 const moment = try evt.currentMoment(evt.admin_repo_opts, &repo); 256 257 const name_to_user_id_cursor = try moment.getCursor(hash.hashInt(evt.admin_repo_opts.hash, "name->user-id")) orelse return null; 258 const name_to_user_id = try evt.AdminDB.HashMap(.read_only).init(name_to_user_id_cursor); 259 const user_id_cursor = try name_to_user_id.getCursor(hash.hashInt(evt.admin_repo_opts.hash, name)) orelse return null; 260 var user_id: [evt.event_id_size]u8 = undefined; 261 _ = try user_id_cursor.readBytes(&user_id); 262 263 return try readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, &user_id); 264 } 265 266 // flip a user's ANSI-art preference by re-emitting their User event with 267 // enable_ansi negated. a no-op for an unknown user. `repo` must be writable. 268 pub fn toggleAnsi( 269 comptime repo_opts: rp.RepoOpts(.xit), 270 io: std.Io, 271 allocator: std.mem.Allocator, 272 repo: *rp.Repo(.xit, repo_opts), 273 user_id: []const u8, 274 ) !void { 275 const DB = rp.Repo(.xit, repo_opts).DB; 276 var arena = std.heap.ArenaAllocator.init(allocator); 277 defer arena.deinit(); 278 279 const moment = try evt.currentMoment(repo_opts, repo); 280 const user = (try readById(DB, repo_opts.hash, moment, &arena, user_id)) orelse return; 281 282 var updated = user.event; 283 updated.enable_ansi = !updated.enable_ansi; 284 try evt.consume(.xit, repo_opts, io, allocator, repo, evt.events_ref, &[_]evt.EventWithId{.{ 285 .id = std.fmt.bytesToHex(user_id[0..evt.event_id_size].*, .lower), 286 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 287 .author_email = user.event.email, 288 .event = .{ .user = updated }, 289 }}); 290 } 291