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