╭ 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 created_ts: u64 = 0, // the commit timestamp of the event that first created this user 15 16 const Self = @This(); 17 18 pub const name_max_len = 32; 19 20 // the moment keys `evt.merge` reads and writes for this kind 21 pub const record_map_key = "event-id->user"; 22 pub const id_set_key = "user-id-set"; 23 pub const name_index_key = "name->user-id"; 24 pub const conflicts_key = "conflicted-user-id->conflict"; 25 pub const id_to_field_to_oid_key = "user-id->field->oid"; 26 27 // resolves a commit's author email to its user at read time 28 pub const email_to_user_id_key = "email->user-id"; 29 30 // a user's key in the name index 31 pub fn indexKey(self: Self, allocator: std.mem.Allocator) ![]const u8 { 32 _ = allocator; 33 return self.name; 34 } 35 36 pub fn validateName(name: []const u8) !void { 37 if (name.len == 0) return error.NameEmpty; 38 if (name.len > name_max_len) return error.NameTooLong; 39 if (name[0] == '-' or name[name.len - 1] == '-') return error.InvalidName; 40 41 var previous_was_hyphen = false; 42 for (name) |c| { 43 if (std.ascii.isAlphanumeric(c)) { 44 previous_was_hyphen = false; 45 } else if (c == '-' and !previous_was_hyphen) { 46 previous_was_hyphen = true; 47 } else { 48 return error.InvalidName; 49 } 50 } 51 } 52 53 // `created_ts` comes from the commit timestamp, not the event 54 // payload, so it must not appear in the JSON we output 55 pub fn jsonStringify(self: @This(), jw: anytype) !void { 56 try jw.beginObject(); 57 inline for (std.meta.fields(@This())) |field| { 58 if (comptime std.mem.eql(u8, field.name, "created_ts")) continue; 59 try jw.objectField(field.name); 60 try jw.write(@field(self, field.name)); 61 } 62 try jw.endObject(); 63 } 64 65 // the subset of a user that's safe to hand to clients 66 pub const Safe = struct { 67 name: []const u8, 68 display_name: []const u8, 69 70 pub fn init(user: Self) Safe { 71 return .{ .name = user.name, .display_name = user.display_name }; 72 } 73 }; 74 75 pub fn consume( 76 comptime DB: type, 77 comptime hash_kind: hash.HashKind, 78 haxy_moment: DB.HashMap(.read_write), 79 event_id: *const [evt.event_id_size]u8, 80 event_maybe: ?@This(), 81 arena: *std.heap.ArenaAllocator, 82 created_ts: u64, 83 // the commit this event came from, recorded against every field it 84 // changes. null from `merge`, which sets oids and conflicts itself. 85 event_oid: ?[]const u8, 86 ) !void { 87 const user_key = hash.hashInt(hash_kind, event_id); 88 89 const event_id_to_user_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "event-id->user")); 90 const event_id_to_user = try DB.HashMap(.read_write).init(event_id_to_user_cursor); 91 92 const name_to_user_id_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "name->user-id")); 93 const name_to_user_id = try DB.HashMap(.read_write).init(name_to_user_id_cursor); 94 95 const email_to_user_id_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, email_to_user_id_key)); 96 const email_to_user_id = try DB.HashMap(.read_write).init(email_to_user_id_cursor); 97 98 const conflicts_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, conflicts_key)); 99 const conflicts = try DB.SortedMap(.read_write).init(conflicts_cursor); 100 101 const user_id_to_field_to_oid_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, id_to_field_to_oid_key)); 102 const user_id_to_field_to_oid = try DB.HashMap(.read_write).init(user_id_to_field_to_oid_cursor); 103 104 if (event_maybe) |event| { 105 try validateName(event.name); 106 107 var event_to_write = event; 108 109 // if this event_id already maps to a user with a different name or 110 // email, drop the stale index entries first 111 var existing_event_maybe: ?@This() = null; 112 const existing_cursor_maybe = try event_id_to_user.getCursor(user_key); 113 if (existing_cursor_maybe) |existing_cursor| { 114 const existing_user = try DB.HashMap(.read_only).init(existing_cursor); 115 const existing_event = try evt.read(@This(), DB, hash_kind, arena, existing_user); 116 existing_event_maybe = existing_event; 117 // updates preserve the original creation timestamp 118 event_to_write.created_ts = existing_event.created_ts; 119 if (!std.mem.eql(u8, existing_event.name, event.name)) { 120 _ = try name_to_user_id.remove(hash.hashInt(hash_kind, existing_event.name)); 121 } 122 if (!std.mem.eql(u8, existing_event.email, event.email)) { 123 _ = try email_to_user_id.remove(hash.hashInt(hash_kind, existing_event.email)); 124 } 125 126 // any event settles the conflict, since resolving in the ui may 127 // keep our own values and so change nothing to detect 128 if (event_oid != null) { 129 _ = try conflicts.remove(&evt.orderKeyDesc(existing_event.created_ts, event_id)); 130 } 131 } else { 132 // first time we've seen this user: stamp it with the commit timestamp 133 event_to_write.created_ts = created_ts; 134 } 135 136 try evt.writeOid(@This(), DB, hash_kind, user_id_to_field_to_oid, user_key, existing_event_maybe, event, event_oid); 137 138 const user_cursor = try event_id_to_user.putCursor(user_key); 139 const user = try DB.HashMap(.read_write).init(user_cursor); 140 try evt.upsert(@This(), DB, hash_kind, user, event_to_write); 141 142 try name_to_user_id.put(hash.hashInt(hash_kind, event.name), .{ .bytes = event_id }); 143 try email_to_user_id.put(hash.hashInt(hash_kind, event.email), .{ .bytes = event_id }); 144 145 // first time we've seen this user: add it to the ordered set the users 146 // view paginates through. the key embeds the event id, so the set needs 147 // no value. 148 if (existing_cursor_maybe == null) { 149 const user_id_set_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "user-id-set")); 150 const user_id_set = try DB.SortedSet(.read_write).init(user_id_set_cursor); 151 const order_key = evt.orderKeyDesc(event_to_write.created_ts, event_id); 152 try user_id_set.put(&order_key); 153 } 154 } else { 155 // read the user so we can drop its name and email index entries 156 if (try event_id_to_user.getCursor(user_key)) |existing_cursor| { 157 const existing_user = try DB.HashMap(.read_only).init(existing_cursor); 158 const existing_event = try evt.read(@This(), DB, hash_kind, arena, existing_user); 159 _ = try name_to_user_id.remove(hash.hashInt(hash_kind, existing_event.name)); 160 _ = try email_to_user_id.remove(hash.hashInt(hash_kind, existing_event.email)); 161 162 // drop it from the ordered set using its recorded creation timestamp 163 const user_id_set_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "user-id-set")); 164 const user_id_set = try DB.SortedSet(.read_write).init(user_id_set_cursor); 165 const order_key = evt.orderKeyDesc(existing_event.created_ts, event_id); 166 _ = try user_id_set.remove(&order_key); 167 168 _ = try conflicts.remove(&order_key); 169 _ = try user_id_to_field_to_oid.remove(user_key); 170 } 171 172 if (!try event_id_to_user.remove(user_key)) return error.EventNotFound; 173 174 const user_id_to_repo_id_set_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "user-id->repo-id-set")); 175 const user_id_to_repo_id_set = try DB.HashMap(.read_write).init(user_id_to_repo_id_set_cursor); 176 _ = try user_id_to_repo_id_set.remove(user_key); 177 } 178 } 179 180 pub const password_hash_max_len = bcrypt.hash_length * 2; 181 182 pub fn hashPassword( 183 password: []const u8, 184 out: []u8, 185 io: std.Io, 186 ) ![]const u8 { 187 return bcrypt.strHash(password, .{ 188 .params = bcrypt.Params.owasp, 189 .encoding = .phc, 190 }, out, io); 191 } 192 193 pub const VerifyResult = union(enum) { 194 success: [evt.event_id_size]u8, 195 unknown_user, 196 wrong_password, 197 }; 198 199 // look up a user by name (via the name->user-id index) and verify the 200 // supplied password against the stored bcrypt hash. used by both the TTY 201 // login submit and the server's /login route. 202 pub fn verifyCredentials( 203 comptime DB: type, 204 comptime hash_kind: hash.HashKind, 205 haxy_moment: DB.HashMap(.read_only), 206 arena: *std.heap.ArenaAllocator, 207 name: []const u8, 208 password: []const u8, 209 ) !VerifyResult { 210 const name_index_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "name->user-id")) orelse return .unknown_user; 211 const name_index = try DB.HashMap(.read_only).init(name_index_cursor); 212 213 const user_id_cursor = try name_index.getCursor(hash.hashInt(hash_kind, name)) orelse return .unknown_user; 214 var user_id: [evt.event_id_size]u8 = undefined; 215 _ = try user_id_cursor.readBytes(&user_id); 216 217 const user_key = hash.hashInt(hash_kind, &user_id); 218 const event_id_to_user_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "event-id->user")) orelse return .unknown_user; 219 const event_id_to_user = try DB.HashMap(.read_only).init(event_id_to_user_cursor); 220 221 const user_cursor = try event_id_to_user.getCursor(user_key) orelse return .unknown_user; 222 const user_map = try DB.HashMap(.read_only).init(user_cursor); 223 const user_event = try evt.read(@This(), DB, hash_kind, arena, user_map); 224 225 bcrypt.strVerify(user_event.password_hash, password, .{ .silently_truncate_password = false }) catch { 226 return .wrong_password; 227 }; 228 229 return .{ .success = user_id }; 230 } 231 232 // read a user by event id via the event-id->user index, or null if the id 233 // isn't a known user. field byte slices are allocated in `arena`. 234 pub fn readById( 235 comptime DB: type, 236 comptime hash_kind: hash.HashKind, 237 haxy_moment: DB.HashMap(.read_only), 238 arena: *std.heap.ArenaAllocator, 239 user_id: []const u8, 240 ) !?@This() { 241 const event_id_to_user_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "event-id->user")) orelse return null; 242 const event_id_to_user = try DB.HashMap(.read_only).init(event_id_to_user_cursor); 243 const user_cursor = try event_id_to_user.getCursor(hash.hashInt(hash_kind, user_id)) orelse return null; 244 const user_map = try DB.HashMap(.read_only).init(user_cursor); 245 return try evt.read(@This(), DB, hash_kind, arena, user_map); 246 } 247 248 // read a user by name from the admin event store, or null if the admin repo or 249 // the user doesn't exist. field byte slices are allocated in `arena`. 250 pub fn readByName( 251 io: std.Io, 252 allocator: std.mem.Allocator, 253 admin_repo_path: []const u8, 254 arena: *std.heap.ArenaAllocator, 255 name: []const u8, 256 ) !?Self { 257 var repo = rp.Repo(.xit, evt.admin_repo_opts).open(io, allocator, .{ .path = admin_repo_path }) catch |err| switch (err) { 258 error.RepoNotFound => return null, 259 else => |e| return e, 260 }; 261 defer repo.deinit(io, allocator); 262 263 const moment = try evt.currentMoment(evt.admin_repo_opts, &repo); 264 265 const name_to_user_id_cursor = try moment.getCursor(hash.hashInt(evt.admin_repo_opts.hash, "name->user-id")) orelse return null; 266 const name_to_user_id = try evt.AdminDB.HashMap(.read_only).init(name_to_user_id_cursor); 267 const user_id_cursor = try name_to_user_id.getCursor(hash.hashInt(evt.admin_repo_opts.hash, name)) orelse return null; 268 var user_id: [evt.event_id_size]u8 = undefined; 269 _ = try user_id_cursor.readBytes(&user_id); 270 271 return try readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, &user_id); 272 } 273 274 // flip a user's ANSI-art preference by re-emitting their User event with 275 // enable_ansi negated. a no-op for an unknown user. `repo` must be writable. 276 pub fn toggleAnsi( 277 comptime repo_opts: rp.RepoOpts(.xit), 278 io: std.Io, 279 allocator: std.mem.Allocator, 280 repo: *rp.Repo(.xit, repo_opts), 281 user_id: []const u8, 282 ) !void { 283 const DB = rp.Repo(.xit, repo_opts).DB; 284 var arena = std.heap.ArenaAllocator.init(allocator); 285 defer arena.deinit(); 286 287 const moment = try evt.currentMoment(repo_opts, repo); 288 const user = (try readById(DB, repo_opts.hash, moment, &arena, user_id)) orelse return; 289 290 var updated = user; 291 updated.enable_ansi = !user.enable_ansi; 292 try evt.consume(.xit, repo_opts, io, allocator, repo, evt.events_ref, &[_]evt.EventWithId{.{ 293 .id = std.fmt.bytesToHex(user_id[0..evt.event_id_size].*, .lower), 294 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 295 .author_email = user.email, 296 .event = .{ .user = updated }, 297 }}); 298 } 299