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