╭ 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 display_name: []const u8, 10 email: []const u8, 11 password_hash: []const u8, 12 enable_ansi: bool = true, 13 ssh_keys: []const u8 = "", // newline-separated authorized_keys lines (one OpenSSH public key per line) 14 created_ts: u64 = 0, // the commit timestamp of the event that first created this user 15 16 const Self = @This(); 17 18 pub const name_max_len = 32; 19 20 // the moment keys `evt.merge` reads and writes for this kind 21 pub const record_map_key = "event-id->user"; 22 pub const id_set_key = "user-id-set"; 23 pub const name_index_key = "name->user-id"; 24 pub const conflicts_key = "conflicted-user-id->conflict"; 25 pub const id_to_field_to_oid_key = "user-id->field->oid"; 26 27 // resolves a commit's author email to its user at read time 28 pub const email_to_user_id_key = "email->user-id"; 29 30 // a user's key in the name index 31 pub fn indexKey(self: Self, allocator: std.mem.Allocator) ![]const u8 { 32 _ = allocator; 33 return self.name; 34 } 35 36 pub fn validateName(name: []const u8) !void { 37 if (name.len == 0) return error.NameEmpty; 38 if (name.len > name_max_len) return error.NameTooLong; 39 if (name[0] == '-' or name[name.len - 1] == '-') return error.InvalidName; 40 41 var previous_was_hyphen = false; 42 for (name) |c| { 43 if (std.ascii.isAlphanumeric(c)) { 44 previous_was_hyphen = false; 45 } else if (c == '-' and !previous_was_hyphen) { 46 previous_was_hyphen = true; 47 } else { 48 return error.InvalidName; 49 } 50 } 51 } 52 53 // the commit-derived fields must not appear in the JSON we output 54 pub fn jsonStringify(self: @This(), jw: anytype) !void { 55 try jw.beginObject(); 56 inline for (std.meta.fields(@This())) |field| { 57 if (comptime evt.derivedField(field.name)) continue; 58 try jw.objectField(field.name); 59 try jw.write(@field(self, field.name)); 60 } 61 try jw.endObject(); 62 } 63 64 // the subset of a user that's safe to hand to clients 65 pub const Safe = struct { 66 name: []const u8, 67 display_name: []const u8, 68 69 pub fn init(user: Self) Safe { 70 return .{ .name = user.name, .display_name = user.display_name }; 71 } 72 }; 73 74 pub fn consume( 75 comptime DB: type, 76 comptime hash_kind: hash.HashKind, 77 haxy_moment: DB.HashMap(.read_write), 78 event_id: *const [evt.event_id_size]u8, 79 event_maybe: ?@This(), 80 arena: *std.heap.ArenaAllocator, 81 // the commit this event came from, recorded against every field it 82 // changes. null from `merge`, which sets oids and conflicts itself. 83 event_oid: ?[]const u8, 84 ) !void { 85 const user_key = hash.hashInt(hash_kind, event_id); 86 87 const event_id_to_user_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "event-id->user")); 88 const event_id_to_user = try DB.HashMap(.read_write).init(event_id_to_user_cursor); 89 90 const name_to_user_id_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "name->user-id")); 91 const name_to_user_id = try DB.HashMap(.read_write).init(name_to_user_id_cursor); 92 93 const email_to_user_id_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, email_to_user_id_key)); 94 const email_to_user_id = try DB.HashMap(.read_write).init(email_to_user_id_cursor); 95 96 const conflicts_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, conflicts_key)); 97 const conflicts = try DB.SortedMap(.read_write).init(conflicts_cursor); 98 99 const user_id_to_field_to_oid_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, id_to_field_to_oid_key)); 100 const user_id_to_field_to_oid = try DB.HashMap(.read_write).init(user_id_to_field_to_oid_cursor); 101 102 if (event_maybe) |event| { 103 try validateName(event.name); 104 105 var event_to_write = event; 106 107 // if this event_id already maps to a user with a different name or 108 // email, drop the stale index entries first 109 var existing_event_maybe: ?@This() = null; 110 const existing_cursor_maybe = try event_id_to_user.getCursor(user_key); 111 if (existing_cursor_maybe) |existing_cursor| { 112 const existing_user = try DB.HashMap(.read_only).init(existing_cursor); 113 const existing_event = try evt.read(@This(), DB, hash_kind, arena, existing_user); 114 existing_event_maybe = existing_event; 115 // updates preserve the original creation timestamp 116 event_to_write.created_ts = existing_event.created_ts; 117 if (!std.mem.eql(u8, existing_event.name, event.name)) { 118 _ = try name_to_user_id.remove(hash.hashInt(hash_kind, existing_event.name)); 119 } 120 if (!std.mem.eql(u8, existing_event.email, event.email)) { 121 _ = try email_to_user_id.remove(hash.hashInt(hash_kind, existing_event.email)); 122 } 123 124 // any event settles the conflict, since resolving in the ui may 125 // keep our own values and so change nothing to detect 126 if (event_oid != null) { 127 _ = try conflicts.remove(&evt.orderKeyDesc(existing_event.created_ts, event_id)); 128 } 129 } 130 131 try evt.writeOid(@This(), DB, hash_kind, user_id_to_field_to_oid, user_key, existing_event_maybe, event, event_oid); 132 133 const user_cursor = try event_id_to_user.putCursor(user_key); 134 const user = try DB.HashMap(.read_write).init(user_cursor); 135 try evt.upsert(@This(), DB, hash_kind, user, event_to_write); 136 137 try name_to_user_id.put(hash.hashInt(hash_kind, event.name), .{ .bytes = event_id }); 138 try email_to_user_id.put(hash.hashInt(hash_kind, event.email), .{ .bytes = event_id }); 139 140 // first time we've seen this user: add it to the ordered set the users 141 // view paginates through. the key embeds the event id, so the set needs 142 // no value. 143 if (existing_cursor_maybe == null) { 144 const user_id_set_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "user-id-set")); 145 const user_id_set = try DB.SortedSet(.read_write).init(user_id_set_cursor); 146 const order_key = evt.orderKeyDesc(event_to_write.created_ts, event_id); 147 try user_id_set.put(&order_key); 148 } 149 } else { 150 // read the user so we can drop its name and email index entries 151 if (try event_id_to_user.getCursor(user_key)) |existing_cursor| { 152 const existing_user = try DB.HashMap(.read_only).init(existing_cursor); 153 const existing_event = try evt.read(@This(), DB, hash_kind, arena, existing_user); 154 _ = try name_to_user_id.remove(hash.hashInt(hash_kind, existing_event.name)); 155 _ = try email_to_user_id.remove(hash.hashInt(hash_kind, existing_event.email)); 156 157 // drop it from the ordered set using its recorded creation timestamp 158 const user_id_set_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "user-id-set")); 159 const user_id_set = try DB.SortedSet(.read_write).init(user_id_set_cursor); 160 const order_key = evt.orderKeyDesc(existing_event.created_ts, event_id); 161 _ = try user_id_set.remove(&order_key); 162 163 _ = try conflicts.remove(&order_key); 164 _ = try user_id_to_field_to_oid.remove(user_key); 165 } 166 167 if (!try event_id_to_user.remove(user_key)) return error.EventNotFound; 168 169 const user_id_to_repo_id_set_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "user-id->repo-id-set")); 170 const user_id_to_repo_id_set = try DB.HashMap(.read_write).init(user_id_to_repo_id_set_cursor); 171 _ = try user_id_to_repo_id_set.remove(user_key); 172 } 173 } 174 175 pub const password_hash_max_len = bcrypt.hash_length * 2; 176 177 pub fn hashPassword( 178 password: []const u8, 179 out: []u8, 180 io: std.Io, 181 ) ![]const u8 { 182 return bcrypt.strHash(password, .{ 183 .params = bcrypt.Params.owasp, 184 .encoding = .phc, 185 }, out, io); 186 } 187 188 pub const VerifyResult = union(enum) { 189 success: [evt.event_id_size]u8, 190 unknown_user, 191 wrong_password, 192 }; 193 194 // look up a user by name (via the name->user-id index) and verify the 195 // supplied password against the stored bcrypt hash. used by both the TTY 196 // login submit and the server's /login route. 197 pub fn verifyCredentials( 198 comptime DB: type, 199 comptime hash_kind: hash.HashKind, 200 haxy_moment: DB.HashMap(.read_only), 201 arena: *std.heap.ArenaAllocator, 202 name: []const u8, 203 password: []const u8, 204 ) !VerifyResult { 205 const name_index_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "name->user-id")) orelse return .unknown_user; 206 const name_index = try DB.HashMap(.read_only).init(name_index_cursor); 207 208 const user_id_cursor = try name_index.getCursor(hash.hashInt(hash_kind, name)) orelse return .unknown_user; 209 var user_id: [evt.event_id_size]u8 = undefined; 210 _ = try user_id_cursor.readBytes(&user_id); 211 212 const user_key = hash.hashInt(hash_kind, &user_id); 213 const event_id_to_user_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "event-id->user")) orelse return .unknown_user; 214 const event_id_to_user = try DB.HashMap(.read_only).init(event_id_to_user_cursor); 215 216 const user_cursor = try event_id_to_user.getCursor(user_key) orelse return .unknown_user; 217 const user_map = try DB.HashMap(.read_only).init(user_cursor); 218 const user_event = try evt.read(@This(), DB, hash_kind, arena, user_map); 219 220 bcrypt.strVerify(user_event.password_hash, password, .{ .silently_truncate_password = false }) catch { 221 return .wrong_password; 222 }; 223 224 return .{ .success = user_id }; 225 } 226 227 // read a user by event id via the event-id->user index, or null if the id 228 // isn't a known user. field byte slices are allocated in `arena`. 229 pub fn readById( 230 comptime DB: type, 231 comptime hash_kind: hash.HashKind, 232 haxy_moment: DB.HashMap(.read_only), 233 arena: *std.heap.ArenaAllocator, 234 user_id: []const u8, 235 ) !?@This() { 236 const event_id_to_user_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "event-id->user")) orelse return null; 237 const event_id_to_user = try DB.HashMap(.read_only).init(event_id_to_user_cursor); 238 const user_cursor = try event_id_to_user.getCursor(hash.hashInt(hash_kind, user_id)) orelse return null; 239 const user_map = try DB.HashMap(.read_only).init(user_cursor); 240 return try evt.read(@This(), DB, hash_kind, arena, user_map); 241 } 242 243 // read a user by email via the email->user-id index, or null when no user has 244 // the email. field byte slices are allocated in `arena`. 245 pub fn readByEmail( 246 comptime DB: type, 247 comptime hash_kind: hash.HashKind, 248 haxy_moment: DB.HashMap(.read_only), 249 arena: *std.heap.ArenaAllocator, 250 email: []const u8, 251 ) !?@This() { 252 const email_to_user_id_cursor = (try haxy_moment.getCursor(hash.hashInt(hash_kind, email_to_user_id_key))) orelse return null; 253 const email_to_user_id = try DB.HashMap(.read_only).init(email_to_user_id_cursor); 254 const user_id_cursor = (try email_to_user_id.getCursor(hash.hashInt(hash_kind, email))) orelse return null; 255 var user_id: [evt.event_id_size]u8 = undefined; 256 _ = try user_id_cursor.readBytes(&user_id); 257 return try readById(DB, hash_kind, haxy_moment, arena, &user_id); 258 } 259 260 // read a user by name from the admin event store, or null if the admin repo or 261 // the user doesn't exist. field byte slices are allocated in `arena`. 262 pub fn readByName( 263 io: std.Io, 264 allocator: std.mem.Allocator, 265 admin_repo_path: []const u8, 266 arena: *std.heap.ArenaAllocator, 267 name: []const u8, 268 ) !?Self { 269 var repo = rp.Repo(.xit, evt.admin_repo_opts).open(io, allocator, .{ .path = admin_repo_path }) catch |err| switch (err) { 270 error.RepoNotFound => return null, 271 else => |e| return e, 272 }; 273 defer repo.deinit(io, allocator); 274 275 const moment = try evt.currentMoment(evt.admin_repo_opts, &repo); 276 277 const name_to_user_id_cursor = try moment.getCursor(hash.hashInt(evt.admin_repo_opts.hash, "name->user-id")) orelse return null; 278 const name_to_user_id = try evt.AdminDB.HashMap(.read_only).init(name_to_user_id_cursor); 279 const user_id_cursor = try name_to_user_id.getCursor(hash.hashInt(evt.admin_repo_opts.hash, name)) orelse return null; 280 var user_id: [evt.event_id_size]u8 = undefined; 281 _ = try user_id_cursor.readBytes(&user_id); 282 283 return try readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, &user_id); 284 } 285 286 // flip a user's ANSI-art preference by re-emitting their User event with 287 // enable_ansi negated. a no-op for an unknown user. `repo` must be writable. 288 pub fn toggleAnsi( 289 comptime repo_opts: rp.RepoOpts(.xit), 290 io: std.Io, 291 allocator: std.mem.Allocator, 292 repo: *rp.Repo(.xit, repo_opts), 293 user_id: []const u8, 294 ) !void { 295 const DB = rp.Repo(.xit, repo_opts).DB; 296 var arena = std.heap.ArenaAllocator.init(allocator); 297 defer arena.deinit(); 298 299 const moment = try evt.currentMoment(repo_opts, repo); 300 const user = (try readById(DB, repo_opts.hash, moment, &arena, user_id)) orelse return; 301 302 var updated = user; 303 updated.enable_ansi = !user.enable_ansi; 304 try evt.consume(.xit, repo_opts, io, allocator, repo, evt.events_ref, &[_]evt.EventWithId{.{ 305 .id = std.fmt.bytesToHex(user_id[0..evt.event_id_size].*, .lower), 306 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 307 .author_email = user.email, 308 .event = .{ .user = updated }, 309 }}); 310 } 311