╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ User.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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 bcrypt.strVerify(user_event.event.password_hash, password, .{ .silently_truncate_password = false }) catch { 163 return .wrong_password; 164 }; 165 166 return .{ .success = user_id }; 167 } 168 169 // read a user by event id via the event-id->user index, or null if the id 170 // isn't a known user. field byte slices are allocated in `arena`. 171 pub fn readById( 172 comptime DB: type, 173 comptime hash_kind: hash.HashKind, 174 haxy_moment: DB.HashMap(.read_only), 175 arena: *std.heap.ArenaAllocator, 176 user_id: []const u8, 177 ) !?Record { 178 const user_map = try userMap(DB, hash_kind, haxy_moment, user_id) orelse return null; 179 return try evt.read(Record, DB, hash_kind, arena, user_map); 180 } 181 182 // read just the public part of a user by email via the email->user-id index, or 183 // null when no user has the email. field byte slices are allocated in `arena`. 184 pub fn readByEmail( 185 comptime DB: type, 186 comptime hash_kind: hash.HashKind, 187 haxy_moment: DB.HashMap(.read_only), 188 arena: *std.heap.ArenaAllocator, 189 email: []const u8, 190 ) !?Public { 191 const email_to_user_id_cursor = (try haxy_moment.getCursor(hash.hashInt(hash_kind, email_to_user_id_key))) orelse return null; 192 const email_to_user_id = try DB.HashMap(.read_only).init(email_to_user_id_cursor); 193 const user_id_cursor = (try email_to_user_id.getCursor(hash.hashInt(hash_kind, email))) orelse return null; 194 var user_id: [evt.event_id_size]u8 = undefined; 195 _ = try user_id_cursor.readBytes(&user_id); 196 const user_map = try userMap(DB, hash_kind, haxy_moment, &user_id) orelse return null; 197 const record = try evt.read(struct { event: Public }, DB, hash_kind, arena, user_map); 198 return record.event; 199 } 200 201 // a user's id via the name->user-id index 202 pub fn readIdByName( 203 comptime DB: type, 204 comptime hash_kind: hash.HashKind, 205 haxy_moment: DB.HashMap(.read_only), 206 name: []const u8, 207 ) !?[evt.event_id_size]u8 { 208 const index_cursor = (try haxy_moment.getCursor(hash.hashInt(hash_kind, name_index_key))) orelse return null; 209 const index = try DB.SortedMap(.read_only).init(index_cursor); 210 const user_id_cursor = (try index.getCursor(name)) orelse return null; 211 var user_id: [evt.event_id_size]u8 = undefined; 212 _ = try user_id_cursor.readBytes(&user_id); 213 return user_id; 214 } 215 216 // a user's record map via the event-id->user index 217 fn userMap( 218 comptime DB: type, 219 comptime hash_kind: hash.HashKind, 220 haxy_moment: DB.HashMap(.read_only), 221 user_id: []const u8, 222 ) !?DB.HashMap(.read_only) { 223 const event_id_to_user_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "event-id->user")) orelse return null; 224 const event_id_to_user = try DB.HashMap(.read_only).init(event_id_to_user_cursor); 225 const user_cursor = try event_id_to_user.getCursor(hash.hashInt(hash_kind, user_id)) orelse return null; 226 return try DB.HashMap(.read_only).init(user_cursor); 227 } 228 229 // read a user by name from the admin event store, or null if the admin repo or 230 // the user doesn't exist. field byte slices are allocated in `arena`. 231 pub fn readByName( 232 io: std.Io, 233 allocator: std.mem.Allocator, 234 admin_repo_path: []const u8, 235 arena: *std.heap.ArenaAllocator, 236 name: []const u8, 237 ) !?Record { 238 var repo = rp.Repo(.xit, evt.admin_repo_opts).open(io, allocator, .{ .path = admin_repo_path }) catch |err| switch (err) { 239 error.RepoNotFound => return null, 240 else => |e| return e, 241 }; 242 defer repo.deinit(io, allocator); 243 244 const moment = try evt.currentMoment(evt.admin_repo_opts, &repo); 245 246 const user_id = try readIdByName(evt.AdminDB, evt.admin_repo_opts.hash, moment, name) orelse return null; 247 248 return try readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, &user_id); 249 } 250