╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ User.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
21 // the subset of a user that anyone may see 22 pub const Public = struct { 23 name: []const u8, 24 }; 25 26 const Self = @This(); 27 28 pub const name_min_len = 4; 29 pub const name_max_len = 32; 30 31 // the moment keys `evt.merge` reads and writes for this kind 32 pub const merge_policy: evt.MergePolicy = .target_wins; 33 pub const record_map_key = "event-id->user"; 34 pub const all_id_set_key = "user-id-set"; 35 pub const name_index_key = "name->user-id"; 36 37 // resolves a commit's author email to its user at read time 38 pub const email_to_user_id_key = "email->user-id"; 39 40 pub fn validateName(name: []const u8) !void { 41 if (name.len == 0) return error.NameEmpty; 42 if (name.len < name_min_len) return error.NameTooShort; 43 if (name.len > name_max_len) return error.NameTooLong; 44 if (name[0] == '-' or name[name.len - 1] == '-') return error.InvalidName; 45 // reserved for the top-level pages' url segment 46 if (std.ascii.eqlIgnoreCase(name, "home")) return error.InvalidName; 47 48 var previous_was_hyphen = false; 49 for (name) |c| { 50 if (std.ascii.isAlphanumeric(c)) { 51 previous_was_hyphen = false; 52 } else if (c == '-' and !previous_was_hyphen) { 53 previous_was_hyphen = true; 54 } else { 55 return error.InvalidName; 56 } 57 } 58 } 59 60 pub fn consume( 61 comptime DB: type, 62 comptime hash_kind: hash.HashKind, 63 haxy_moment: DB.HashMap(.read_write), 64 event_id: *const [evt.event_id_size]u8, 65 record_maybe: ?Record, 66 arena: *std.heap.ArenaAllocator, 67 _: ?[]const u8, 68 ) !void { 69 const user_key = hash.hashInt(hash_kind, event_id); 70 71 const event_id_to_user_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "event-id->user")); 72 const event_id_to_user = try DB.HashMap(.read_write).init(event_id_to_user_cursor); 73 74 const name_to_user_id_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, name_index_key)); 75 const name_to_user_id = try DB.SortedMap(.read_write).init(name_to_user_id_cursor); 76 77 const email_to_user_id_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, email_to_user_id_key)); 78 const email_to_user_id = try DB.HashMap(.read_write).init(email_to_user_id_cursor); 79 80 var existing_record_maybe: ?Record = null; 81 const existing_cursor_maybe = try event_id_to_user.getCursor(user_key); 82 if (existing_cursor_maybe) |existing_cursor| { 83 const existing_user = try DB.HashMap(.read_only).init(existing_cursor); 84 existing_record_maybe = try evt.read(Record, DB, hash_kind, arena, existing_user); 85 } 86 87 var record_to_write = record_maybe orelse try evt.removedRecord(Record, DB, hash_kind, haxy_moment.readOnly(), existing_record_maybe); 88 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 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