╭ 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 updated_order: u64 = 0, 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 }; 32 33 const Self = @This(); 34 35 pub const name_max_len = 32; 36 37 // the moment keys `evt.merge` reads and writes for this kind 38 pub const merge_policy: evt.MergePolicy = .target_wins; 39 pub const record_map_key = "event-id->user"; 40 pub const all_id_set_key = "user-id-set"; 41 pub const active_id_set_key = "active-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 = record_maybe orelse try evt.removedRecord(Record, DB, hash_kind, haxy_moment.readOnly(), existing_record_maybe); 92 93 if (!record_to_write.removed) try validateName(record_to_write.event.name); 94 95 if (existing_record_maybe) |existing_record| { 96 // updates preserve the original creation metadata 97 record_to_write.created_order = existing_record.created_order; 98 99 // drop the old active indexes; active values are re-added below 100 if (!existing_record.removed) { 101 _ = try name_to_user_id.remove(hash.hashInt(hash_kind, existing_record.event.name)); 102 _ = try email_to_user_id.remove(hash.hashInt(hash_kind, existing_record.event.email)); 103 } 104 } 105 106 const user_cursor = try event_id_to_user.putCursor(user_key); 107 const user = try DB.HashMap(.read_write).init(user_cursor); 108 try evt.upsert(Record, DB, hash_kind, user, record_to_write); 109 try evt.indexEvent(DB, hash_kind, haxy_moment, event_id, .user, existing_record_maybe, record_to_write); 110 111 const order_key = evt.orderKeyDesc(record_to_write.created_order, event_id); 112 113 // the id set retains removed records so merges can carry removals 114 if (existing_cursor_maybe == null) { 115 const user_id_set_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, all_id_set_key)); 116 const user_id_set = try DB.SortedSet(.read_write).init(user_id_set_cursor); 117 try user_id_set.put(&order_key); 118 } 119 120 const active = try DB.SortedSet(.read_write).init(try haxy_moment.putCursor(hash.hashInt(hash_kind, active_id_set_key))); 121 if (record_to_write.removed) { 122 _ = try active.remove(&order_key); 123 } else { 124 try active.put(&order_key); 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 user_map = try userMap(DB, hash_kind, haxy_moment, user_id) orelse return null; 215 return try evt.read(Record, DB, hash_kind, arena, user_map); 216 } 217 218 // read just the public part of a user by email via the email->user-id index, or 219 // null when no user has the email. field byte slices are allocated in `arena`. 220 pub fn readByEmail( 221 comptime DB: type, 222 comptime hash_kind: hash.HashKind, 223 haxy_moment: DB.HashMap(.read_only), 224 arena: *std.heap.ArenaAllocator, 225 email: []const u8, 226 ) !?Public { 227 const email_to_user_id_cursor = (try haxy_moment.getCursor(hash.hashInt(hash_kind, email_to_user_id_key))) orelse return null; 228 const email_to_user_id = try DB.HashMap(.read_only).init(email_to_user_id_cursor); 229 const user_id_cursor = (try email_to_user_id.getCursor(hash.hashInt(hash_kind, email))) orelse return null; 230 var user_id: [evt.event_id_size]u8 = undefined; 231 _ = try user_id_cursor.readBytes(&user_id); 232 const user_map = try userMap(DB, hash_kind, haxy_moment, &user_id) orelse return null; 233 const record = try evt.read(struct { event: Public }, DB, hash_kind, arena, user_map); 234 return record.event; 235 } 236 237 // a user's record map via the event-id->user index 238 fn userMap( 239 comptime DB: type, 240 comptime hash_kind: hash.HashKind, 241 haxy_moment: DB.HashMap(.read_only), 242 user_id: []const u8, 243 ) !?DB.HashMap(.read_only) { 244 const event_id_to_user_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "event-id->user")) orelse return null; 245 const event_id_to_user = try DB.HashMap(.read_only).init(event_id_to_user_cursor); 246 const user_cursor = try event_id_to_user.getCursor(hash.hashInt(hash_kind, user_id)) orelse return null; 247 return try DB.HashMap(.read_only).init(user_cursor); 248 } 249 250 // read a user by name from the admin event store, or null if the admin repo or 251 // the user doesn't exist. field byte slices are allocated in `arena`. 252 pub fn readByName( 253 io: std.Io, 254 allocator: std.mem.Allocator, 255 admin_repo_path: []const u8, 256 arena: *std.heap.ArenaAllocator, 257 name: []const u8, 258 ) !?Record { 259 var repo = rp.Repo(.xit, evt.admin_repo_opts).open(io, allocator, .{ .path = admin_repo_path }) catch |err| switch (err) { 260 error.RepoNotFound => return null, 261 else => |e| return e, 262 }; 263 defer repo.deinit(io, allocator); 264 265 const moment = try evt.currentMoment(evt.admin_repo_opts, &repo); 266 267 const name_to_user_id_cursor = try moment.getCursor(hash.hashInt(evt.admin_repo_opts.hash, "name->user-id")) orelse return null; 268 const name_to_user_id = try evt.AdminDB.HashMap(.read_only).init(name_to_user_id_cursor); 269 const user_id_cursor = try name_to_user_id.getCursor(hash.hashInt(evt.admin_repo_opts.hash, name)) orelse return null; 270 var user_id: [evt.event_id_size]u8 = undefined; 271 _ = try user_id_cursor.readBytes(&user_id); 272 273 return try readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, &user_id); 274 } 275 276 // flip a user's ANSI-art preference by re-emitting their User event with 277 // enable_ansi negated. a no-op for an unknown user. `repo` must be writable. 278 pub fn toggleAnsi( 279 comptime repo_opts: rp.RepoOpts(.xit), 280 io: std.Io, 281 allocator: std.mem.Allocator, 282 repo: *rp.Repo(.xit, repo_opts), 283 user_id: []const u8, 284 ) !void { 285 const DB = rp.Repo(.xit, repo_opts).DB; 286 var arena = std.heap.ArenaAllocator.init(allocator); 287 defer arena.deinit(); 288 289 const moment = try evt.currentMoment(repo_opts, repo); 290 const user = (try readById(DB, repo_opts.hash, moment, &arena, user_id)) orelse return; 291 292 var updated = user.event; 293 updated.enable_ansi = !updated.enable_ansi; 294 try evt.consume(.server, .admin, .xit, repo_opts, io, allocator, repo, evt.events_ref, &[_]evt.EventWithId{.{ 295 .id = std.fmt.bytesToHex(user_id[0..evt.event_id_size].*, .lower), 296 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 297 .author = .{ .name = user.event.name, .email = user.event.email }, 298 .event = .{ .user = updated }, 299 }}); 300 } 301