╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ User.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
86 if (existing_cursor_maybe) |existing_cursor| { 87 const existing_user = try DB.HashMap(.read_only).init(existing_cursor); 88 existing_record_maybe = try evt.read(Record, DB, hash_kind, arena, existing_user); 89 } 90 91 var record_to_write = record_maybe orelse try evt.removedRecord(Record, DB, hash_kind, haxy_moment.readOnly(), existing_record_maybe); 92 93 if (!record_to_write.removed) try validateName(record_to_write.event.name); 94 95 if (existing_record_maybe) |existing_record| { 96 // updates preserve the original creation metadata 97 record_to_write.created_order = existing_record.created_order; 98 99 // drop the old active indexes; active values are re-added below 100 if (!existing_record.removed) { 101 _ = try name_to_user_id.remove(hash.hashInt(hash_kind, existing_record.event.name)); 102 _ = try email_to_user_id.remove(hash.hashInt(hash_kind, existing_record.event.email)); 103 } 104 } 105 106 const user_cursor = try event_id_to_user.putCursor(user_key); 107 const user = try DB.HashMap(.read_write).init(user_cursor); 108 try evt.upsert(Record, DB, hash_kind, user, record_to_write); 109 try evt.indexEvent(DB, hash_kind, haxy_moment, event_id, .user, existing_record_maybe, record_to_write); 110 111 const order_key = evt.orderKeyDesc(record_to_write.created_order, event_id); 112 113 // the id set retains removed records so merges can carry removals 114 if (existing_cursor_maybe == null) { 115 const user_id_set_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, all_id_set_key)); 116 const user_id_set = try DB.SortedSet(.read_write).init(user_id_set_cursor); 117 try user_id_set.put(&order_key); 118 } 119 120 const active = try DB.SortedSet(.read_write).init(try haxy_moment.putCursor(hash.hashInt(hash_kind, active_id_set_key))); 121 if (record_to_write.removed) { 122 _ = try active.remove(&order_key); 123 } else { 124 try active.put(&order_key); 125 try name_to_user_id.put(hash.hashInt(hash_kind, record_to_write.event.name), .{ .bytes = event_id }); 126 try email_to_user_id.put(hash.hashInt(hash_kind, record_to_write.event.email), .{ .bytes = event_id }); 127 } 128 129 const became_removed = record_to_write.removed and if (existing_record_maybe) |existing| !existing.removed else true; 130 if (became_removed) { 131 // the user's repos go with them: the repo name index is keyed by owner 132 // id, so one left behind is listed but can never be resolved again 133 const user_id_to_repo_id_set_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "user-id->repo-id-set")); 134 const user_id_to_repo_id_set = try DB.HashMap(.read_write).init(user_id_to_repo_id_set_cursor); 135 if (try user_id_to_repo_id_set.getCursor(user_key)) |user_repos_cursor| { 136 const user_repos = try DB.SortedSet(.read_only).init(user_repos_cursor); 137 138 // collected up front, since removing a repo removes it from this set 139 var repo_ids: std.ArrayList([evt.event_id_size]u8) = .empty; 140 var repo_iter = try user_repos.iteratorFromIndex(0); 141 while (try repo_iter.next()) |kv_pair_cursor| { 142 try repo_ids.append(arena.allocator(), try evt.readOrderKeyId(DB, kv_pair_cursor)); 143 } 144 145 for (repo_ids.items) |*repo_id| { 146 try evt.Repo.consume(DB, hash_kind, haxy_moment, repo_id, null, arena, null); 147 } 148 } 149 _ = try user_id_to_repo_id_set.remove(user_key); 150 } 151 } 152 153 pub const password_hash_max_len = bcrypt.hash_length * 2; 154 155 pub fn hashPassword( 156 password: []const u8, 157 out: []u8, 158 io: std.Io, 159 ) ![]const u8 { 160 return bcrypt.strHash(password, .{ 161 .params = bcrypt.Params.owasp, 162 .encoding = .phc, 163 }, out, io); 164 } 165 166 pub const VerifyResult = union(enum) { 167 success: [evt.event_id_size]u8, 168 unknown_user, 169 wrong_password, 170 }; 171 172 // look up a user by name (via the name->user-id index) and verify the 173 // supplied password against the stored bcrypt hash. used by both the TTY 174 // login submit and the server's /login route. 175 pub fn verifyCredentials( 176 comptime DB: type, 177 comptime hash_kind: hash.HashKind, 178 haxy_moment: DB.HashMap(.read_only), 179 arena: *std.heap.ArenaAllocator, 180 name: []const u8, 181 password: []const u8, 182 ) !VerifyResult { 183 const name_index_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "name->user-id")) orelse return .unknown_user; 184 const name_index = try DB.HashMap(.read_only).init(name_index_cursor); 185 186 const user_id_cursor = try name_index.getCursor(hash.hashInt(hash_kind, name)) orelse return .unknown_user; 187 var user_id: [evt.event_id_size]u8 = undefined; 188 _ = try user_id_cursor.readBytes(&user_id); 189 190 const user_key = hash.hashInt(hash_kind, &user_id); 191 const event_id_to_user_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "event-id->user")) orelse return .unknown_user; 192 const event_id_to_user = try DB.HashMap(.read_only).init(event_id_to_user_cursor); 193 194 const user_cursor = try event_id_to_user.getCursor(user_key) orelse return .unknown_user; 195 const user_map = try DB.HashMap(.read_only).init(user_cursor); 196 const user_event = try evt.read(Record, DB, hash_kind, arena, user_map); 197 198 bcrypt.strVerify(user_event.event.password_hash, password, .{ .silently_truncate_password = false }) catch { 199 return .wrong_password; 200 }; 201 202 return .{ .success = user_id }; 203 } 204 205 // read a user by event id via the event-id->user index, or null if the id 206 // isn't a known user. field byte slices are allocated in `arena`. 207 pub fn readById( 208 comptime DB: type, 209 comptime hash_kind: hash.HashKind, 210 haxy_moment: DB.HashMap(.read_only), 211 arena: *std.heap.ArenaAllocator, 212 user_id: []const u8, 213 ) !?Record { 214 const event_id_to_user_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "event-id->user")) orelse return null; 215 const event_id_to_user = try DB.HashMap(.read_only).init(event_id_to_user_cursor); 216 const user_cursor = try event_id_to_user.getCursor(hash.hashInt(hash_kind, user_id)) orelse return null; 217 const user_map = try DB.HashMap(.read_only).init(user_cursor); 218 return try evt.read(Record, DB, hash_kind, arena, user_map); 219 } 220 221 // read a user by email via the email->user-id index, or null when no user has 222 // the email. field byte slices are allocated in `arena`. 223 pub fn readByEmail( 224 comptime DB: type, 225 comptime hash_kind: hash.HashKind, 226 haxy_moment: DB.HashMap(.read_only), 227 arena: *std.heap.ArenaAllocator, 228 email: []const u8, 229 ) !?Record { 230 const email_to_user_id_cursor = (try haxy_moment.getCursor(hash.hashInt(hash_kind, email_to_user_id_key))) orelse return null; 231 const email_to_user_id = try DB.HashMap(.read_only).init(email_to_user_id_cursor); 232 const user_id_cursor = (try email_to_user_id.getCursor(hash.hashInt(hash_kind, email))) orelse return null; 233 var user_id: [evt.event_id_size]u8 = undefined; 234 _ = try user_id_cursor.readBytes(&user_id); 235 return try readById(DB, hash_kind, haxy_moment, arena, &user_id); 236 } 237 238 // read a user by name from the admin event store, or null if the admin repo or 239 // the user doesn't exist. field byte slices are allocated in `arena`. 240 pub fn readByName( 241 io: std.Io, 242 allocator: std.mem.Allocator, 243 admin_repo_path: []const u8, 244 arena: *std.heap.ArenaAllocator, 245 name: []const u8, 246 ) !?Record { 247 var repo = rp.Repo(.xit, evt.admin_repo_opts).open(io, allocator, .{ .path = admin_repo_path }) catch |err| switch (err) { 248 error.RepoNotFound => return null, 249 else => |e| return e, 250 }; 251 defer repo.deinit(io, allocator); 252 253 const moment = try evt.currentMoment(evt.admin_repo_opts, &repo); 254 255 const name_to_user_id_cursor = try moment.getCursor(hash.hashInt(evt.admin_repo_opts.hash, "name->user-id")) orelse return null; 256 const name_to_user_id = try evt.AdminDB.HashMap(.read_only).init(name_to_user_id_cursor); 257 const user_id_cursor = try name_to_user_id.getCursor(hash.hashInt(evt.admin_repo_opts.hash, name)) orelse return null; 258 var user_id: [evt.event_id_size]u8 = undefined; 259 _ = try user_id_cursor.readBytes(&user_id); 260 261 return try readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, &user_id); 262 } 263 264 // flip a user's ANSI-art preference by re-emitting their User event with 265 // enable_ansi negated. a no-op for an unknown user. `repo` must be writable. 266 pub fn toggleAnsi( 267 comptime repo_opts: rp.RepoOpts(.xit), 268 io: std.Io, 269 allocator: std.mem.Allocator, 270 repo: *rp.Repo(.xit, repo_opts), 271 user_id: []const u8, 272 ) !void { 273 const DB = rp.Repo(.xit, repo_opts).DB; 274 var arena = std.heap.ArenaAllocator.init(allocator); 275 defer arena.deinit(); 276 277 const moment = try evt.currentMoment(repo_opts, repo); 278 const user = (try readById(DB, repo_opts.hash, moment, &arena, user_id)) orelse return; 279 280 var updated = user.event; 281 updated.enable_ansi = !updated.enable_ansi; 282 try evt.consume(.server, .admin, .xit, repo_opts, io, allocator, repo, evt.events_ref, &[_]evt.EventWithId{.{ 283 .id = std.fmt.bytesToHex(user_id[0..evt.event_id_size].*, .lower), 284 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 285 .author = .{ .name = user.event.name, .email = user.event.email }, 286 .event = .{ .user = updated }, 287 }}); 288 } 289