╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ User.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
89 if (!record_to_write.removed) try validateName(record_to_write.event.name); 90 91 if (existing_record_maybe) |existing_record| { 92 // updates preserve the original creation metadata 93 record_to_write.created_order = existing_record.created_order; 94 95 // drop the old active indexes; active values are re-added below 96 if (!existing_record.removed) { 97 _ = try name_to_user_id.remove(existing_record.event.name); 98 _ = try email_to_user_id.remove(hash.hashInt(hash_kind, existing_record.event.email)); 99 } 100 } 101 102 const user_cursor = try event_id_to_user.putCursor(user_key); 103 const user = try DB.HashMap(.read_write).init(user_cursor); 104 try evt.upsert(Record, DB, hash_kind, user, record_to_write); 105 try evt.indexEvent(DB, hash_kind, haxy_moment, event_id, .user, existing_record_maybe, record_to_write); 106 107 const order_key = evt.orderKeyDesc(record_to_write.created_order, event_id); 108 109 // the id set retains removed records so merges can carry removals 110 if (existing_cursor_maybe == null) { 111 const user_id_set_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, all_id_set_key)); 112 const user_id_set = try DB.SortedSet(.read_write).init(user_id_set_cursor); 113 try user_id_set.put(&order_key); 114 } 115 116 if (!record_to_write.removed) { 117 try name_to_user_id.put(record_to_write.event.name, .{ .bytes = event_id }); 118 try email_to_user_id.put(hash.hashInt(hash_kind, record_to_write.event.email), .{ .bytes = event_id }); 119 } 120 } 121 122 pub const password_hash_max_len = bcrypt.hash_length * 2; 123 124 pub fn hashPassword( 125 password: []const u8, 126 out: []u8, 127 io: std.Io, 128 ) ![]const u8 { 129 return bcrypt.strHash(password, .{ 130 .params = bcrypt.Params.owasp, 131 .encoding = .phc, 132 }, out, io); 133 } 134 135 pub const VerifyResult = union(enum) { 136 success: [evt.event_id_size]u8, 137 unknown_user, 138 wrong_password, 139 }; 140 141 // look up a user by name (via the name->user-id index) and verify the 142 // supplied password against the stored bcrypt hash. used by both the TTY 143 // login submit and the server's /login route. 144 pub fn verifyCredentials( 145 comptime DB: type, 146 comptime hash_kind: hash.HashKind, 147 haxy_moment: DB.HashMap(.read_only), 148 arena: *std.heap.ArenaAllocator, 149 name: []const u8, 150 password: []const u8, 151 ) !VerifyResult { 152 const user_id = try readIdByName(DB, hash_kind, haxy_moment, name) orelse return .unknown_user; 153 154 const user_key = hash.hashInt(hash_kind, &user_id); 155 const event_id_to_user_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "event-id->user")) orelse return .unknown_user; 156 const event_id_to_user = try DB.HashMap(.read_only).init(event_id_to_user_cursor); 157 158 const user_cursor = try event_id_to_user.getCursor(user_key) orelse return .unknown_user; 159 const user_map = try DB.HashMap(.read_only).init(user_cursor); 160 const user_event = try evt.read(Record, DB, hash_kind, arena, user_map); 161 162 if (!verifyPassword(user_event.event.password_hash, password)) return .wrong_password; 163 164 return .{ .success = user_id }; 165 } 166 167 pub fn verifyPassword(password_hash: []const u8, password: []const u8) bool { 168 bcrypt.strVerify(password_hash, password, .{ .silently_truncate_password = false }) catch return false; 169 return true; 170 } 171 172 // read a user by event id via the event-id->user index, or null if the id 173 // isn't a known user. field byte slices are allocated in `arena`. 174 pub fn readById( 175 comptime DB: type, 176 comptime hash_kind: hash.HashKind, 177 haxy_moment: DB.HashMap(.read_only), 178 arena: *std.heap.ArenaAllocator, 179 user_id: []const u8, 180 ) !?Record { 181 const user_map = try userMap(DB, hash_kind, haxy_moment, user_id) orelse return null; 182 return try evt.read(Record, DB, hash_kind, arena, user_map); 183 } 184 185 // read just the public part of a user by email via the email->user-id index, or 186 // null when no user has the email. field byte slices are allocated in `arena`. 187 pub fn readByEmail( 188 comptime DB: type, 189 comptime hash_kind: hash.HashKind, 190 haxy_moment: DB.HashMap(.read_only), 191 arena: *std.heap.ArenaAllocator, 192 email: []const u8, 193 ) !?Public { 194 const email_to_user_id_cursor = (try haxy_moment.getCursor(hash.hashInt(hash_kind, email_to_user_id_key))) orelse return null; 195 const email_to_user_id = try DB.HashMap(.read_only).init(email_to_user_id_cursor); 196 const user_id_cursor = (try email_to_user_id.getCursor(hash.hashInt(hash_kind, email))) orelse return null; 197 var user_id: [evt.event_id_size]u8 = undefined; 198 _ = try user_id_cursor.readBytes(&user_id); 199 const user_map = try userMap(DB, hash_kind, haxy_moment, &user_id) orelse return null; 200 const record = try evt.read(struct { event: Public }, DB, hash_kind, arena, user_map); 201 return record.event; 202 } 203 204 // a user's id via the name->user-id index 205 pub fn readIdByName( 206 comptime DB: type, 207 comptime hash_kind: hash.HashKind, 208 haxy_moment: DB.HashMap(.read_only), 209 name: []const u8, 210 ) !?[evt.event_id_size]u8 { 211 const index_cursor = (try haxy_moment.getCursor(hash.hashInt(hash_kind, name_index_key))) orelse return null; 212 const index = try DB.SortedMap(.read_only).init(index_cursor); 213 const user_id_cursor = (try index.getCursor(name)) orelse return null; 214 var user_id: [evt.event_id_size]u8 = undefined; 215 _ = try user_id_cursor.readBytes(&user_id); 216 return user_id; 217 } 218 219 // a user's record map via the event-id->user index 220 fn userMap( 221 comptime DB: type, 222 comptime hash_kind: hash.HashKind, 223 haxy_moment: DB.HashMap(.read_only), 224 user_id: []const u8, 225 ) !?DB.HashMap(.read_only) { 226 const event_id_to_user_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "event-id->user")) orelse return null; 227 const event_id_to_user = try DB.HashMap(.read_only).init(event_id_to_user_cursor); 228 const user_cursor = try event_id_to_user.getCursor(hash.hashInt(hash_kind, user_id)) orelse return null; 229 return try DB.HashMap(.read_only).init(user_cursor); 230 } 231 232 // read a user by name from the admin event store, or null if the admin repo or 233 // the user doesn't exist. field byte slices are allocated in `arena`. 234 pub fn readByName( 235 io: std.Io, 236 allocator: std.mem.Allocator, 237 admin_repo_path: []const u8, 238 arena: *std.heap.ArenaAllocator, 239 name: []const u8, 240 ) !?Record { 241 var repo = rp.Repo(.xit, evt.admin_repo_opts).open(io, allocator, .{ .path = admin_repo_path }) catch |err| switch (err) { 242 error.RepoNotFound => return null, 243 else => |e| return e, 244 }; 245 defer repo.deinit(io, allocator); 246 247 const moment = try evt.currentMoment(evt.admin_repo_opts, &repo); 248 249 const user_id = try readIdByName(evt.AdminDB, evt.admin_repo_opts.hash, moment, name) orelse return null; 250 251 return try readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, &user_id); 252 } 253