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