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