╭ 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 enable_ansi: bool = true, 12 ssh_keys: []const u8 = "", // newline-separated authorized_keys lines (one OpenSSH public key per line) 13 14 // what the db stores: the event's data plus the commit-derived fields 15 pub const Record = struct { 16 event: Self, 17 removed: bool = false, 18 created_order: u64 = 0, 19 updated_order: u64 = 0, 20 }; 21 22 // the subset of a user that anyone may see 23 pub const Public = struct { 24 name: []const u8, 25 }; 26 27 const Self = @This(); 28 29 pub const name_min_len = 4; 30 pub const name_max_len = 32; 31 32 // the moment keys `evt.merge` reads and writes for this kind 33 pub const merge_policy: evt.MergePolicy = .target_wins; 34 pub const record_map_key = "event-id->user"; 35 pub const all_id_set_key = "user-id-set"; 36 pub const name_index_key = "name->user-id"; 37 38 // resolves a commit's author email to its user at read time 39 pub const email_to_user_id_key = "email->user-id"; 40 41 pub fn validateName(name: []const u8) !void { 42 if (name.len == 0) return error.NameEmpty; 43 if (name.len < name_min_len) return error.NameTooShort; 44 if (name.len > name_max_len) return error.NameTooLong; 45 if (name[0] == '-' or name[name.len - 1] == '-') return error.InvalidName; 46 47 var previous_was_hyphen = false; 48 for (name) |c| { 49 if (std.ascii.isAlphanumeric(c)) { 50 previous_was_hyphen = false; 51 } else if (c == '-' and !previous_was_hyphen) { 52 previous_was_hyphen = true; 53 } else { 54 return error.InvalidName; 55 } 56 } 57 } 58 59 pub fn consume( 60 comptime DB: type, 61 comptime hash_kind: hash.HashKind, 62 haxy_moment: DB.HashMap(.read_write), 63 event_id: *const [evt.event_id_size]u8, 64 record_maybe: ?Record, 65 arena: *std.heap.ArenaAllocator, 66 _: ?[]const u8, 67 ) !void { 68 const user_key = hash.hashInt(hash_kind, event_id); 69 70 const event_id_to_user_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "event-id->user")); 71 const event_id_to_user = try DB.HashMap(.read_write).init(event_id_to_user_cursor); 72 73 const name_to_user_id_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, name_index_key)); 74 const name_to_user_id = try DB.SortedMap(.read_write).init(name_to_user_id_cursor); 75 76 const email_to_user_id_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, email_to_user_id_key)); 77 const email_to_user_id = try DB.HashMap(.read_write).init(email_to_user_id_cursor); 78 79 var existing_record_maybe: ?Record = null; 80 const existing_cursor_maybe = try event_id_to_user.getCursor(user_key); 81 if (existing_cursor_maybe) |existing_cursor| { 82 const existing_user = try DB.HashMap(.read_only).init(existing_cursor); 83 existing_record_maybe = try evt.read(Record, DB, hash_kind, arena, existing_user); 84 } 85 86 var record_to_write = record_maybe orelse try evt.removedRecord(Record, DB, hash_kind, haxy_moment.readOnly(), existing_record_maybe); 87 88 if (!record_to_write.removed) try validateName(record_to_write.event.name); 89 90 if (existing_record_maybe) |existing_record| { 91 // updates preserve the original creation metadata 92 record_to_write.created_order = existing_record.created_order; 93 94 // drop the old active indexes; active values are re-added below 95 if (!existing_record.removed) { 96 _ = try name_to_user_id.remove(existing_record.event.name); 97 _ = try email_to_user_id.remove(hash.hashInt(hash_kind, existing_record.event.email)); 98 } 99 } 100 101 const user_cursor = try event_id_to_user.putCursor(user_key); 102 const user = try DB.HashMap(.read_write).init(user_cursor); 103 try evt.upsert(Record, DB, hash_kind, user, record_to_write); 104 try evt.indexEvent(DB, hash_kind, haxy_moment, event_id, .user, existing_record_maybe, record_to_write); 105 106 const order_key = evt.orderKeyDesc(record_to_write.created_order, event_id); 107 108 // the id set retains removed records so merges can carry removals 109 if (existing_cursor_maybe == null) { 110 const user_id_set_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, all_id_set_key)); 111 const user_id_set = try DB.SortedSet(.read_write).init(user_id_set_cursor); 112 try user_id_set.put(&order_key); 113 } 114 115 if (!record_to_write.removed) { 116 try name_to_user_id.put(record_to_write.event.name, .{ .bytes = event_id }); 117 try email_to_user_id.put(hash.hashInt(hash_kind, record_to_write.event.email), .{ .bytes = event_id }); 118 } 119 } 120 121 pub const password_hash_max_len = bcrypt.hash_length * 2; 122 123 pub fn hashPassword( 124 password: []const u8, 125 out: []u8, 126 io: std.Io, 127 ) ![]const u8 { 128 return bcrypt.strHash(password, .{ 129 .params = bcrypt.Params.owasp, 130 .encoding = .phc, 131 }, out, io); 132 } 133 134 pub const VerifyResult = union(enum) { 135 success: [evt.event_id_size]u8, 136 unknown_user, 137 wrong_password, 138 }; 139 140 // look up a user by name (via the name->user-id index) and verify the 141 // supplied password against the stored bcrypt hash. used by both the TTY 142 // login submit and the server's /login route. 143 pub fn verifyCredentials( 144 comptime DB: type, 145 comptime hash_kind: hash.HashKind, 146 haxy_moment: DB.HashMap(.read_only), 147 arena: *std.heap.ArenaAllocator, 148 name: []const u8, 149 password: []const u8, 150 ) !VerifyResult { 151 const user_id = try readIdByName(DB, hash_kind, haxy_moment, name) orelse return .unknown_user; 152 153 const user_key = hash.hashInt(hash_kind, &user_id); 154 const event_id_to_user_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "event-id->user")) orelse return .unknown_user; 155 const event_id_to_user = try DB.HashMap(.read_only).init(event_id_to_user_cursor); 156 157 const user_cursor = try event_id_to_user.getCursor(user_key) orelse return .unknown_user; 158 const user_map = try DB.HashMap(.read_only).init(user_cursor); 159 const user_event = try evt.read(Record, DB, hash_kind, arena, user_map); 160 161 bcrypt.strVerify(user_event.event.password_hash, password, .{ .silently_truncate_password = false }) catch { 162 return .wrong_password; 163 }; 164 165 return .{ .success = user_id }; 166 } 167 168 // read a user by event id via the event-id->user index, or null if the id 169 // isn't a known user. field byte slices are allocated in `arena`. 170 pub fn readById( 171 comptime DB: type, 172 comptime hash_kind: hash.HashKind, 173 haxy_moment: DB.HashMap(.read_only), 174 arena: *std.heap.ArenaAllocator, 175 user_id: []const u8, 176 ) !?Record { 177 const user_map = try userMap(DB, hash_kind, haxy_moment, user_id) orelse return null; 178 return try evt.read(Record, DB, hash_kind, arena, user_map); 179 } 180 181 // read just the public part of a user by email via the email->user-id index, or 182 // null when no user has the email. field byte slices are allocated in `arena`. 183 pub fn readByEmail( 184 comptime DB: type, 185 comptime hash_kind: hash.HashKind, 186 haxy_moment: DB.HashMap(.read_only), 187 arena: *std.heap.ArenaAllocator, 188 email: []const u8, 189 ) !?Public { 190 const email_to_user_id_cursor = (try haxy_moment.getCursor(hash.hashInt(hash_kind, email_to_user_id_key))) orelse return null; 191 const email_to_user_id = try DB.HashMap(.read_only).init(email_to_user_id_cursor); 192 const user_id_cursor = (try email_to_user_id.getCursor(hash.hashInt(hash_kind, email))) orelse return null; 193 var user_id: [evt.event_id_size]u8 = undefined; 194 _ = try user_id_cursor.readBytes(&user_id); 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 name->user-id index 201 pub fn readIdByName( 202 comptime DB: type, 203 comptime hash_kind: hash.HashKind, 204 haxy_moment: DB.HashMap(.read_only), 205 name: []const u8, 206 ) !?[evt.event_id_size]u8 { 207 const index_cursor = (try haxy_moment.getCursor(hash.hashInt(hash_kind, name_index_key))) orelse return null; 208 const index = try DB.SortedMap(.read_only).init(index_cursor); 209 const user_id_cursor = (try index.getCursor(name)) 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 record map via the event-id->user index 216 fn userMap( 217 comptime DB: type, 218 comptime hash_kind: hash.HashKind, 219 haxy_moment: DB.HashMap(.read_only), 220 user_id: []const u8, 221 ) !?DB.HashMap(.read_only) { 222 const event_id_to_user_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "event-id->user")) orelse return null; 223 const event_id_to_user = try DB.HashMap(.read_only).init(event_id_to_user_cursor); 224 const user_cursor = try event_id_to_user.getCursor(hash.hashInt(hash_kind, user_id)) orelse return null; 225 return try DB.HashMap(.read_only).init(user_cursor); 226 } 227 228 // read a user by name from the admin event store, or null if the admin repo or 229 // the user doesn't exist. field byte slices are allocated in `arena`. 230 pub fn readByName( 231 io: std.Io, 232 allocator: std.mem.Allocator, 233 admin_repo_path: []const u8, 234 arena: *std.heap.ArenaAllocator, 235 name: []const u8, 236 ) !?Record { 237 var repo = rp.Repo(.xit, evt.admin_repo_opts).open(io, allocator, .{ .path = admin_repo_path }) catch |err| switch (err) { 238 error.RepoNotFound => return null, 239 else => |e| return e, 240 }; 241 defer repo.deinit(io, allocator); 242 243 const moment = try evt.currentMoment(evt.admin_repo_opts, &repo); 244 245 const user_id = try readIdByName(evt.AdminDB, evt.admin_repo_opts.hash, moment, name) orelse return null; 246 247 return try readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, &user_id); 248 } 249 250 // flip a user's ANSI-art preference by re-emitting their User event with 251 // enable_ansi negated. a no-op for an unknown user. `repo` must be writable. 252 pub fn toggleAnsi( 253 comptime repo_opts: rp.RepoOpts(.xit), 254 io: std.Io, 255 allocator: std.mem.Allocator, 256 repo: *rp.Repo(.xit, repo_opts), 257 users_dir: []const u8, 258 user_id: []const u8, 259 ) !void { 260 const DB = rp.Repo(.xit, repo_opts).DB; 261 var arena = std.heap.ArenaAllocator.init(allocator); 262 defer arena.deinit(); 263 264 const moment = try evt.currentMoment(repo_opts, repo); 265 const user = (try readById(DB, repo_opts.hash, moment, &arena, user_id)) orelse return; 266 267 var updated = user.event; 268 updated.enable_ansi = !updated.enable_ansi; 269 try evt.consume(.{ .server = .{ .users_dir = users_dir } }, .admin, .xit, repo_opts, io, allocator, repo, evt.events_ref, &[_]evt.EventWithId{.{ 270 .id = std.fmt.bytesToHex(user_id[0..evt.event_id_size].*, .lower), 271 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 272 .author = .{ .name = user.event.name, .email = user.event.email }, 273 .event = .{ .user = updated }, 274 }}); 275 } 276