╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ User.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
7 8 name: []const u8, 9 email: []const u8, 10 password_hash: []const u8, 11 ssh_keys: []const u8 = "", // newline-separated authorized_keys lines (one OpenSSH public key per line) 12 13 // what the db stores: the event's data plus the commit-derived fields 14 pub const Record = struct { 15 event: Self, 16 removed: bool = false, 17 created_order: u64 = 0, 18 updated_order: u64 = 0, 19 }; 20 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 46 var previous_was_hyphen = false; 47 for (name) |c| { 48 if (std.ascii.isAlphanumeric(c)) { 49 previous_was_hyphen = false; 50 } else if (c == '-' and !previous_was_hyphen) { 51 previous_was_hyphen = true; 52 } else { 53 return error.InvalidName; 54 } 55 } 56 } 57 58 pub fn consume( 59 comptime DB: type, 60 comptime hash_kind: hash.HashKind, 61 haxy_moment: DB.HashMap(.read_write), 62 event_id: *const [evt.event_id_size]u8, 63 record_maybe: ?Record, 64 arena: *std.heap.ArenaAllocator, 65 _: ?[]const u8, 66 ) !void { 67 const user_key = hash.hashInt(hash_kind, event_id); 68 69 const event_id_to_user_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "event-id->user")); 70 const event_id_to_user = try DB.HashMap(.read_write).init(event_id_to_user_cursor); 71 72 const name_to_user_id_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, name_index_key)); 73 const name_to_user_id = try DB.SortedMap(.read_write).init(name_to_user_id_cursor); 74 75 const email_to_user_id_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, email_to_user_id_key)); 76 const email_to_user_id = try DB.HashMap(.read_write).init(email_to_user_id_cursor); 77 78 var existing_record_maybe: ?Record = null; 79 const existing_cursor_maybe = try event_id_to_user.getCursor(user_key); 80 if (existing_cursor_maybe) |existing_cursor| { 81 const existing_user = try DB.HashMap(.read_only).init(existing_cursor); 82 existing_record_maybe = try evt.read(Record, DB, hash_kind, arena, existing_user); 83 } 84 85 var record_to_write = record_maybe orelse try evt.removedRecord(Record, DB, hash_kind, haxy_moment.readOnly(), existing_record_maybe); 86 87 if (!record_to_write.removed) try validateName(record_to_write.event.name); 88 89 if (existing_record_maybe) |existing_record| { 90 // updates preserve the original creation metadata 91 record_to_write.created_order = existing_record.created_order; 92 93 // drop the old active indexes; active values are re-added below 94 if (!existing_record.removed) { 95 _ = try name_to_user_id.remove(existing_record.event.name); 96 _ = try email_to_user_id.remove(hash.hashInt(hash_kind, existing_record.event.email)); 97 } 98 } 99 100 const user_cursor = try event_id_to_user.putCursor(user_key); 101 const user = try DB.HashMap(.read_write).init(user_cursor); 102 try evt.upsert(Record, DB, hash_kind, user, record_to_write); 103 try evt.indexEvent(DB, hash_kind, haxy_moment, event_id, .user, existing_record_maybe, record_to_write); 104 105 const order_key = evt.orderKeyDesc(record_to_write.created_order, event_id); 106 107 // the id set retains removed records so merges can carry removals 108 if (existing_cursor_maybe == null) { 109 const user_id_set_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, all_id_set_key)); 110 const user_id_set = try DB.SortedSet(.read_write).init(user_id_set_cursor); 111 try user_id_set.put(&order_key); 112 } 113 114 if (!record_to_write.removed) { 115 try name_to_user_id.put(record_to_write.event.name, .{ .bytes = event_id }); 116 try email_to_user_id.put(hash.hashInt(hash_kind, record_to_write.event.email), .{ .bytes = event_id }); 117 } 118 } 119 120 pub const password_hash_max_len = bcrypt.hash_length * 2; 121 122 pub fn hashPassword( 123 password: []const u8, 124 out: []u8, 125 io: std.Io, 126 ) ![]const u8 { 127 return bcrypt.strHash(password, .{ 128 .params = bcrypt.Params.owasp, 129 .encoding = .phc, 130 }, out, io); 131 } 132 133 pub const VerifyResult = union(enum) { 134 success: [evt.event_id_size]u8, 135 unknown_user, 136 wrong_password, 137 }; 138 139 // look up a user by name (via the name->user-id index) and verify the 140 // supplied password against the stored bcrypt hash. used by both the TTY 141 // login submit and the server's /login route. 142 pub fn verifyCredentials( 143 comptime DB: type, 144 comptime hash_kind: hash.HashKind, 145 haxy_moment: DB.HashMap(.read_only), 146 arena: *std.heap.ArenaAllocator, 147 name: []const u8, 148 password: []const u8, 149 ) !VerifyResult { 150 const user_id = try readIdByName(DB, hash_kind, haxy_moment, name) orelse return .unknown_user; 151 152 const user_key = hash.hashInt(hash_kind, &user_id); 153 const event_id_to_user_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "event-id->user")) orelse return .unknown_user; 154 const event_id_to_user = try DB.HashMap(.read_only).init(event_id_to_user_cursor); 155 156 const user_cursor = try event_id_to_user.getCursor(user_key) orelse return .unknown_user; 157 const user_map = try DB.HashMap(.read_only).init(user_cursor); 158 const user_event = try evt.read(Record, DB, hash_kind, arena, user_map); 159 160 bcrypt.strVerify(user_event.event.password_hash, password, .{ .silently_truncate_password = false }) catch { 161 return .wrong_password; 162 }; 163 164 return .{ .success = user_id }; 165 } 166 167 // read a user by event id via the event-id->user index, or null if the id 168 // isn't a known user. field byte slices are allocated in `arena`. 169 pub fn readById( 170 comptime DB: type, 171 comptime hash_kind: hash.HashKind, 172 haxy_moment: DB.HashMap(.read_only), 173 arena: *std.heap.ArenaAllocator, 174 user_id: []const u8, 175 ) !?Record { 176 const user_map = try userMap(DB, hash_kind, haxy_moment, user_id) orelse return null; 177 return try evt.read(Record, DB, hash_kind, arena, user_map); 178 } 179 180 // read just the public part of a user by email via the email->user-id index, or 181 // null when no user has the email. field byte slices are allocated in `arena`. 182 pub fn readByEmail( 183 comptime DB: type, 184 comptime hash_kind: hash.HashKind, 185 haxy_moment: DB.HashMap(.read_only), 186 arena: *std.heap.ArenaAllocator, 187 email: []const u8, 188 ) !?Public { 189 const email_to_user_id_cursor = (try haxy_moment.getCursor(hash.hashInt(hash_kind, email_to_user_id_key))) orelse return null; 190 const email_to_user_id = try DB.HashMap(.read_only).init(email_to_user_id_cursor); 191 const user_id_cursor = (try email_to_user_id.getCursor(hash.hashInt(hash_kind, email))) orelse return null; 192 var user_id: [evt.event_id_size]u8 = undefined; 193 _ = try user_id_cursor.readBytes(&user_id); 194 const user_map = try userMap(DB, hash_kind, haxy_moment, &user_id) orelse return null; 195 const record = try evt.read(struct { event: Public }, DB, hash_kind, arena, user_map); 196 return record.event; 197 } 198 199 // a user's id via the name->user-id index 200 pub fn readIdByName( 201 comptime DB: type, 202 comptime hash_kind: hash.HashKind, 203 haxy_moment: DB.HashMap(.read_only), 204 name: []const u8, 205 ) !?[evt.event_id_size]u8 { 206 const index_cursor = (try haxy_moment.getCursor(hash.hashInt(hash_kind, name_index_key))) orelse return null; 207 const index = try DB.SortedMap(.read_only).init(index_cursor); 208 const user_id_cursor = (try index.getCursor(name)) orelse return null; 209 var user_id: [evt.event_id_size]u8 = undefined; 210 _ = try user_id_cursor.readBytes(&user_id); 211 return user_id; 212 } 213 214 // a user's record map via the event-id->user index 215 fn userMap( 216 comptime DB: type, 217 comptime hash_kind: hash.HashKind, 218 haxy_moment: DB.HashMap(.read_only), 219 user_id: []const u8, 220 ) !?DB.HashMap(.read_only) { 221 const event_id_to_user_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "event-id->user")) orelse return null; 222 const event_id_to_user = try DB.HashMap(.read_only).init(event_id_to_user_cursor); 223 const user_cursor = try event_id_to_user.getCursor(hash.hashInt(hash_kind, user_id)) orelse return null; 224 return try DB.HashMap(.read_only).init(user_cursor); 225 } 226 227 // read a user by name from the admin event store, or null if the admin repo or 228 // the user doesn't exist. field byte slices are allocated in `arena`. 229 pub fn readByName( 230 io: std.Io, 231 allocator: std.mem.Allocator, 232 admin_repo_path: []const u8, 233 arena: *std.heap.ArenaAllocator, 234 name: []const u8, 235 ) !?Record { 236 var repo = rp.Repo(.xit, evt.admin_repo_opts).open(io, allocator, .{ .path = admin_repo_path }) catch |err| switch (err) { 237 error.RepoNotFound => return null, 238 else => |e| return e, 239 }; 240 defer repo.deinit(io, allocator); 241 242 const moment = try evt.currentMoment(evt.admin_repo_opts, &repo); 243 244 const user_id = try readIdByName(evt.AdminDB, evt.admin_repo_opts.hash, moment, name) orelse return null; 245 246 return try readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, &user_id); 247 } 248