╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ User.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
200 201 return .{ .success = user_id }; 202 } 203 204 pub fn verifyPassword(password_hash: []const u8, password: []const u8) bool { 205 bcrypt.strVerify(password_hash, password, .{ .silently_truncate_password = false }) catch return false; 206 return true; 207 } 208 209 // read a user by event id via the event-id->user index, or null if the id 210 // isn't a known user. field byte slices are allocated in `arena`. 211 pub fn readById( 212 comptime DB: type, 213 comptime hash_kind: hash.HashKind, 214 haxy_moment: DB.HashMap(.read_only), 215 arena: *std.heap.ArenaAllocator, 216 user_id: []const u8, 217 ) !?Record { 218 const user_map = try userMap(DB, hash_kind, haxy_moment, user_id) orelse return null; 219 return try evt.read(Record, DB, hash_kind, arena, user_map); 220 } 221 222 // read just the public part of a user by email via the email->user-id index, or 223 // null when no user has the email. field byte slices are allocated in `arena`. 224 pub fn readByEmail( 225 comptime DB: type, 226 comptime hash_kind: hash.HashKind, 227 haxy_moment: DB.HashMap(.read_only), 228 arena: *std.heap.ArenaAllocator, 229 email: []const u8, 230 ) !?Public { 231 const user_id = (try readIdByEmail(DB, hash_kind, haxy_moment, email)) orelse return null; 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 id via the email->user-id index 238 pub fn readIdByEmail( 239 comptime DB: type, 240 comptime hash_kind: hash.HashKind, 241 haxy_moment: DB.HashMap(.read_only), 242 email: []const u8, 243 ) !?[evt.event_id_size]u8 { 244 const email_to_user_id_cursor = (try haxy_moment.getCursor(hash.hashInt(hash_kind, email_to_user_id_key))) orelse return null; 245 const email_to_user_id = try DB.HashMap(.read_only).init(email_to_user_id_cursor); 246 const user_id_cursor = (try email_to_user_id.getCursor(hash.hashInt(hash_kind, email))) orelse return null; 247 var user_id: [evt.event_id_size]u8 = undefined; 248 _ = try user_id_cursor.readBytes(&user_id); 249 return user_id; 250 } 251 252 // a user's id via the name->user-id index 253 pub fn readIdByName( 254 comptime DB: type, 255 comptime hash_kind: hash.HashKind, 256 haxy_moment: DB.HashMap(.read_only), 257 name: []const u8, 258 ) !?[evt.event_id_size]u8 { 259 const index_cursor = (try haxy_moment.getCursor(hash.hashInt(hash_kind, name_index_key))) orelse return null; 260 const index = try DB.SortedMap(.read_only).init(index_cursor); 261 const user_id_cursor = (try index.getCursor(name)) orelse return null; 262 var user_id: [evt.event_id_size]u8 = undefined; 263 _ = try user_id_cursor.readBytes(&user_id); 264 return user_id; 265 } 266 267 // the ids of the active users holding the ssh key with this fingerprint 268 pub fn readIdsByFingerprint( 269 comptime DB: type, 270 comptime hash_kind: hash.HashKind, 271 haxy_moment: DB.HashMap(.read_only), 272 arena: *std.heap.ArenaAllocator, 273 fingerprint: *const [ssh.fingerprint_len]u8, 274 ) ![]const [evt.event_id_size]u8 { 275 var user_ids: std.ArrayList([evt.event_id_size]u8) = .empty; 276 const index_cursor = (try haxy_moment.getCursor(hash.hashInt(hash_kind, fingerprint_to_user_id_set_key))) orelse return user_ids.items; 277 const index = try DB.HashMap(.read_only).init(index_cursor); 278 const user_id_set_cursor = (try index.getCursor(hash.hashInt(hash_kind, fingerprint))) orelse return user_ids.items; 279 const user_id_set = try DB.SortedSet(.read_only).init(user_id_set_cursor); 280 281 var iter = try user_id_set.iterator(); 282 while (try iter.next()) |kv_pair_cursor| { 283 var cursor = kv_pair_cursor; 284 const kv_pair = try cursor.readKeyValuePair(); 285 var user_id: [evt.event_id_size]u8 = undefined; 286 _ = try kv_pair.key_cursor.readBytes(&user_id); 287 try user_ids.append(arena.allocator(), user_id); 288 } 289 return user_ids.items; 290 } 291 292 // whether the user holds the ssh key with this fingerprint 293 pub fn holdsKey( 294 comptime DB: type, 295 comptime hash_kind: hash.HashKind, 296 haxy_moment: DB.HashMap(.read_only), 297 arena: *std.heap.ArenaAllocator, 298 user_id: *const [evt.event_id_size]u8, 299 fingerprint: *const [ssh.fingerprint_len]u8, 300 ) !bool { 301 for (try readIdsByFingerprint(DB, hash_kind, haxy_moment, arena, fingerprint)) |holder_id| { 302 if (std.mem.eql(u8, &holder_id, user_id)) return true; 303 } 304 return false; 305 } 306 307 // a user's record map via the event-id->user index 308 fn userMap( 309 comptime DB: type, 310 comptime hash_kind: hash.HashKind, 311 haxy_moment: DB.HashMap(.read_only), 312 user_id: []const u8, 313 ) !?DB.HashMap(.read_only) { 314 const event_id_to_user_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "event-id->user")) orelse return null; 315 const event_id_to_user = try DB.HashMap(.read_only).init(event_id_to_user_cursor); 316 const user_cursor = try event_id_to_user.getCursor(hash.hashInt(hash_kind, user_id)) orelse return null; 317 return try DB.HashMap(.read_only).init(user_cursor); 318 } 319 320 // read a user by name from the admin event store, or null if the admin repo or 321 // the user doesn't exist. field byte slices are allocated in `arena`. 322 pub fn readByName( 323 io: std.Io, 324 allocator: std.mem.Allocator, 325 admin_repo_path: []const u8, 326 arena: *std.heap.ArenaAllocator, 327 name: []const u8, 328 ) !?Record { 329 var repo = rp.Repo(.xit, evt.admin_repo_opts).open(io, allocator, .{ .path = admin_repo_path }) catch |err| switch (err) { 330 error.RepoNotFound => return null, 331 else => |e| return e, 332 }; 333 defer repo.deinit(io, allocator); 334 335 const moment = try evt.currentMoment(evt.admin_repo_opts, &repo); 336 337 const user_id = try readIdByName(evt.AdminDB, evt.admin_repo_opts.hash, moment, name) orelse return null; 338 339 return try readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, &user_id); 340 } 341