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