╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ User.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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 user_map = try userMap(DB, hash_kind, haxy_moment, user_id) orelse return null; 215 return try evt.read(Record, DB, hash_kind, arena, user_map); 216 } 217 218 // read just the public part of a user by email via the email->user-id index, or 219 // null when no user has the email. field byte slices are allocated in `arena`. 220 pub fn readByEmail( 221 comptime DB: type, 222 comptime hash_kind: hash.HashKind, 223 haxy_moment: DB.HashMap(.read_only), 224 arena: *std.heap.ArenaAllocator, 225 email: []const u8, 226 ) !?Public { 227 const email_to_user_id_cursor = (try haxy_moment.getCursor(hash.hashInt(hash_kind, email_to_user_id_key))) orelse return null; 228 const email_to_user_id = try DB.HashMap(.read_only).init(email_to_user_id_cursor); 229 const user_id_cursor = (try email_to_user_id.getCursor(hash.hashInt(hash_kind, email))) orelse return null; 230 var user_id: [evt.event_id_size]u8 = undefined; 231 _ = try user_id_cursor.readBytes(&user_id); 232 const user_map = try userMap(DB, hash_kind, haxy_moment, &user_id) orelse return null; 233 const record = try evt.read(struct { event: Public }, DB, hash_kind, arena, user_map); 234 return record.event; 235 } 236 237 // a user's record map via the event-id->user index 238 fn userMap( 239 comptime DB: type, 240 comptime hash_kind: hash.HashKind, 241 haxy_moment: DB.HashMap(.read_only), 242 user_id: []const u8, 243 ) !?DB.HashMap(.read_only) { 244 const event_id_to_user_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "event-id->user")) orelse return null; 245 const event_id_to_user = try DB.HashMap(.read_only).init(event_id_to_user_cursor); 246 const user_cursor = try event_id_to_user.getCursor(hash.hashInt(hash_kind, user_id)) orelse return null; 247 return try DB.HashMap(.read_only).init(user_cursor); 248 } 249 250 // read a user by name from the admin event store, or null if the admin repo or 251 // the user doesn't exist. field byte slices are allocated in `arena`. 252 pub fn readByName( 253 io: std.Io, 254 allocator: std.mem.Allocator, 255 admin_repo_path: []const u8, 256 arena: *std.heap.ArenaAllocator, 257 name: []const u8, 258 ) !?Record { 259 var repo = rp.Repo(.xit, evt.admin_repo_opts).open(io, allocator, .{ .path = admin_repo_path }) catch |err| switch (err) { 260 error.RepoNotFound => return null, 261 else => |e| return e, 262 }; 263 defer repo.deinit(io, allocator); 264 265 const moment = try evt.currentMoment(evt.admin_repo_opts, &repo); 266 267 const name_to_user_id_cursor = try moment.getCursor(hash.hashInt(evt.admin_repo_opts.hash, "name->user-id")) orelse return null; 268 const name_to_user_id = try evt.AdminDB.HashMap(.read_only).init(name_to_user_id_cursor); 269 const user_id_cursor = try name_to_user_id.getCursor(hash.hashInt(evt.admin_repo_opts.hash, name)) orelse return null; 270 var user_id: [evt.event_id_size]u8 = undefined; 271 _ = try user_id_cursor.readBytes(&user_id); 272 273 return try readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, &user_id); 274 } 275 276 // flip a user's ANSI-art preference by re-emitting their User event with 277 // enable_ansi negated. a no-op for an unknown user. `repo` must be writable. 278 pub fn toggleAnsi( 279 comptime repo_opts: rp.RepoOpts(.xit), 280 io: std.Io, 281 allocator: std.mem.Allocator, 282 repo: *rp.Repo(.xit, repo_opts), 283 user_id: []const u8, 284 ) !void { 285 const DB = rp.Repo(.xit, repo_opts).DB; 286 var arena = std.heap.ArenaAllocator.init(allocator); 287 defer arena.deinit(); 288 289 const moment = try evt.currentMoment(repo_opts, repo); 290 const user = (try readById(DB, repo_opts.hash, moment, &arena, user_id)) orelse return; 291 292 var updated = user.event; 293 updated.enable_ansi = !updated.enable_ansi; 294 try evt.consume(.server, .admin, .xit, repo_opts, io, allocator, repo, evt.events_ref, &[_]evt.EventWithId{.{ 295 .id = std.fmt.bytesToHex(user_id[0..evt.event_id_size].*, .lower), 296 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 297 .author = .{ .name = user.event.name, .email = user.event.email }, 298 .event = .{ .user = updated }, 299 }}); 300 } 301