╭ User.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const evt = @import("../event.zig"); 3 const xit = @import("xit"); 4 const rp = xit.repo; 5 const hash = xit.hash; 6 const bcrypt = std.crypto.pwhash.bcrypt; 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 // 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 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 user_id = (try readIdByEmail(DB, hash_kind, haxy_moment, email)) orelse return null; 195 const user_map = try userMap(DB, hash_kind, haxy_moment, &user_id) orelse return null; 196 const record = try evt.read(struct { event: Public }, DB, hash_kind, arena, user_map); 197 return record.event; 198 } 199 200 // a user's id via the email->user-id index 201 pub fn readIdByEmail( 202 comptime DB: type, 203 comptime hash_kind: hash.HashKind, 204 haxy_moment: DB.HashMap(.read_only), 205 email: []const u8, 206 ) !?[evt.event_id_size]u8 { 207 const email_to_user_id_cursor = (try haxy_moment.getCursor(hash.hashInt(hash_kind, email_to_user_id_key))) orelse return null; 208 const email_to_user_id = try DB.HashMap(.read_only).init(email_to_user_id_cursor); 209 const user_id_cursor = (try email_to_user_id.getCursor(hash.hashInt(hash_kind, email))) orelse return null; 210 var user_id: [evt.event_id_size]u8 = undefined; 211 _ = try user_id_cursor.readBytes(&user_id); 212 return user_id; 213 } 214 215 // a user's id via the name->user-id index 216 pub fn readIdByName( 217 comptime DB: type, 218 comptime hash_kind: hash.HashKind, 219 haxy_moment: DB.HashMap(.read_only), 220 name: []const u8, 221 ) !?[evt.event_id_size]u8 { 222 const index_cursor = (try haxy_moment.getCursor(hash.hashInt(hash_kind, name_index_key))) orelse return null; 223 const index = try DB.SortedMap(.read_only).init(index_cursor); 224 const user_id_cursor = (try index.getCursor(name)) orelse return null; 225 var user_id: [evt.event_id_size]u8 = undefined; 226 _ = try user_id_cursor.readBytes(&user_id); 227 return user_id; 228 } 229 230 // a user's record map via the event-id->user index 231 fn userMap( 232 comptime DB: type, 233 comptime hash_kind: hash.HashKind, 234 haxy_moment: DB.HashMap(.read_only), 235 user_id: []const u8, 236 ) !?DB.HashMap(.read_only) { 237 const event_id_to_user_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "event-id->user")) orelse return null; 238 const event_id_to_user = try DB.HashMap(.read_only).init(event_id_to_user_cursor); 239 const user_cursor = try event_id_to_user.getCursor(hash.hashInt(hash_kind, user_id)) orelse return null; 240 return try DB.HashMap(.read_only).init(user_cursor); 241 } 242 243 // read a user by name from the admin event store, or null if the admin repo or 244 // the user doesn't exist. field byte slices are allocated in `arena`. 245 pub fn readByName( 246 io: std.Io, 247 allocator: std.mem.Allocator, 248 admin_repo_path: []const u8, 249 arena: *std.heap.ArenaAllocator, 250 name: []const u8, 251 ) !?Record { 252 var repo = rp.Repo(.xit, evt.admin_repo_opts).open(io, allocator, .{ .path = admin_repo_path }) catch |err| switch (err) { 253 error.RepoNotFound => return null, 254 else => |e| return e, 255 }; 256 defer repo.deinit(io, allocator); 257 258 const moment = try evt.currentMoment(evt.admin_repo_opts, &repo); 259 260 const user_id = try readIdByName(evt.AdminDB, evt.admin_repo_opts.hash, moment, name) orelse return null; 261 262 return try readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, &user_id); 263 } 264