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