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