╭ 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 pub fn validateName(name: []const u8) !void { 21 if (name.len == 0) return error.NameEmpty; 22 if (name.len > name_max_len) return error.NameTooLong; 23 if (name[0] == '-' or name[name.len - 1] == '-') return error.InvalidName; 24 25 var previous_was_hyphen = false; 26 for (name) |c| { 27 if (std.ascii.isAlphanumeric(c)) { 28 previous_was_hyphen = false; 29 } else if (c == '-' and !previous_was_hyphen) { 30 previous_was_hyphen = true; 31 } else { 32 return error.InvalidName; 33 } 34 } 35 } 36 37 // `created_ts` comes from the commit timestamp, not the event 38 // payload, so it must not appear in the JSON we output 39 pub fn jsonStringify(self: @This(), jw: anytype) !void { 40 try jw.beginObject(); 41 inline for (std.meta.fields(@This())) |field| { 42 if (comptime std.mem.eql(u8, field.name, "created_ts")) continue; 43 try jw.objectField(field.name); 44 try jw.write(@field(self, field.name)); 45 } 46 try jw.endObject(); 47 } 48 49 // the subset of a user that's safe to hand to clients 50 pub const Safe = struct { 51 name: []const u8, 52 display_name: []const u8, 53 54 pub fn init(user: Self) Safe { 55 return .{ .name = user.name, .display_name = user.display_name }; 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 event_maybe: ?@This(), 65 arena: *std.heap.ArenaAllocator, 66 created_ts: u64, 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->user-id")); 74 const name_to_user_id = try DB.HashMap(.read_write).init(name_to_user_id_cursor); 75 76 if (event_maybe) |event| { 77 try validateName(event.name); 78 79 var event_to_write = event; 80 81 // if this event_id already maps to a user with a different name, 82 // drop the stale name->id entry first 83 const existing_cursor_maybe = try event_id_to_user.getCursor(user_key); 84 if (existing_cursor_maybe) |existing_cursor| { 85 const existing_user = try DB.HashMap(.read_only).init(existing_cursor); 86 const existing_event = try evt.read(@This(), DB, hash_kind, arena, existing_user); 87 // updates preserve the original creation timestamp 88 event_to_write.created_ts = existing_event.created_ts; 89 if (!std.mem.eql(u8, existing_event.name, event.name)) { 90 _ = try name_to_user_id.remove(hash.hashInt(hash_kind, existing_event.name)); 91 } 92 } else { 93 // first time we've seen this user: stamp it with the commit timestamp 94 event_to_write.created_ts = created_ts; 95 } 96 97 const user_cursor = try event_id_to_user.putCursor(user_key); 98 const user = try DB.HashMap(.read_write).init(user_cursor); 99 try evt.upsert(@This(), DB, hash_kind, user, event_to_write); 100 101 try name_to_user_id.put(hash.hashInt(hash_kind, event.name), .{ .bytes = event_id }); 102 103 // first time we've seen this user: add it to the ordered set the users 104 // view paginates through. the key (orderKey) embeds the event id, so the 105 // set needs no value. 106 if (existing_cursor_maybe == null) { 107 const user_id_set_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "user-id-set")); 108 const user_id_set = try DB.SortedSet(.read_write).init(user_id_set_cursor); 109 const order_key = evt.orderKey(event_to_write.created_ts, event_id); 110 try user_id_set.put(&order_key); 111 } 112 } else { 113 // read the user's name so we can drop its name->id index entry 114 if (try event_id_to_user.getCursor(user_key)) |existing_cursor| { 115 const existing_user = try DB.HashMap(.read_only).init(existing_cursor); 116 const existing_event = try evt.read(@This(), DB, hash_kind, arena, existing_user); 117 _ = try name_to_user_id.remove(hash.hashInt(hash_kind, existing_event.name)); 118 119 // drop it from the ordered set using its recorded creation timestamp 120 const user_id_set_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "user-id-set")); 121 const user_id_set = try DB.SortedSet(.read_write).init(user_id_set_cursor); 122 const order_key = evt.orderKey(existing_event.created_ts, event_id); 123 _ = try user_id_set.remove(&order_key); 124 } 125 126 if (!try event_id_to_user.remove(user_key)) return error.EventNotFound; 127 128 const user_id_to_repo_id_set_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "user-id->repo-id-set")); 129 const user_id_to_repo_id_set = try DB.HashMap(.read_write).init(user_id_to_repo_id_set_cursor); 130 _ = try user_id_to_repo_id_set.remove(user_key); 131 } 132 } 133 134 pub const password_hash_max_len = bcrypt.hash_length * 2; 135 136 pub fn hashPassword( 137 password: []const u8, 138 out: []u8, 139 io: std.Io, 140 ) ![]const u8 { 141 return bcrypt.strHash(password, .{ 142 .params = bcrypt.Params.owasp, 143 .encoding = .phc, 144 }, out, io); 145 } 146 147 pub const VerifyResult = union(enum) { 148 success: [evt.event_id_size]u8, 149 unknown_user, 150 wrong_password, 151 }; 152 153 // look up a user by name (via the name->user-id index) and verify the 154 // supplied password against the stored bcrypt hash. used by both the TTY 155 // login submit and the server's /login route. 156 pub fn verifyCredentials( 157 comptime DB: type, 158 comptime hash_kind: hash.HashKind, 159 haxy_moment: DB.HashMap(.read_only), 160 arena: *std.heap.ArenaAllocator, 161 name: []const u8, 162 password: []const u8, 163 ) !VerifyResult { 164 const name_index_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "name->user-id")) orelse return .unknown_user; 165 const name_index = try DB.HashMap(.read_only).init(name_index_cursor); 166 167 const user_id_cursor = try name_index.getCursor(hash.hashInt(hash_kind, name)) orelse return .unknown_user; 168 var user_id: [evt.event_id_size]u8 = undefined; 169 _ = try user_id_cursor.readBytes(&user_id); 170 171 const user_key = hash.hashInt(hash_kind, &user_id); 172 const event_id_to_user_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "event-id->user")) orelse return .unknown_user; 173 const event_id_to_user = try DB.HashMap(.read_only).init(event_id_to_user_cursor); 174 175 const user_cursor = try event_id_to_user.getCursor(user_key) orelse return .unknown_user; 176 const user_map = try DB.HashMap(.read_only).init(user_cursor); 177 const user_event = try evt.read(@This(), DB, hash_kind, arena, user_map); 178 179 bcrypt.strVerify(user_event.password_hash, password, .{ .silently_truncate_password = false }) catch { 180 return .wrong_password; 181 }; 182 183 return .{ .success = user_id }; 184 } 185 186 // read a user by event id via the event-id->user index, or null if the id 187 // isn't a known user. field byte slices are allocated in `arena`. 188 pub fn readById( 189 comptime DB: type, 190 comptime hash_kind: hash.HashKind, 191 haxy_moment: DB.HashMap(.read_only), 192 arena: *std.heap.ArenaAllocator, 193 user_id: []const u8, 194 ) !?@This() { 195 const event_id_to_user_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "event-id->user")) orelse return null; 196 const event_id_to_user = try DB.HashMap(.read_only).init(event_id_to_user_cursor); 197 const user_cursor = try event_id_to_user.getCursor(hash.hashInt(hash_kind, user_id)) orelse return null; 198 const user_map = try DB.HashMap(.read_only).init(user_cursor); 199 return try evt.read(@This(), DB, hash_kind, arena, user_map); 200 } 201 202 // read a user by name from the admin event store, or null if the admin repo or 203 // the user doesn't exist. field byte slices are allocated in `arena`. 204 pub fn readByName( 205 io: std.Io, 206 allocator: std.mem.Allocator, 207 admin_repo_path: []const u8, 208 arena: *std.heap.ArenaAllocator, 209 name: []const u8, 210 ) !?Self { 211 var repo = rp.Repo(.xit, evt.admin_repo_opts).open(io, allocator, .{ .path = admin_repo_path }) catch |err| switch (err) { 212 error.RepoNotFound => return null, 213 else => |e| return e, 214 }; 215 defer repo.deinit(io, allocator); 216 217 const moment = try evt.currentMoment(evt.admin_repo_opts, &repo); 218 219 const name_to_user_id_cursor = try moment.getCursor(hash.hashInt(evt.admin_repo_opts.hash, "name->user-id")) orelse return null; 220 const name_to_user_id = try evt.AdminDB.HashMap(.read_only).init(name_to_user_id_cursor); 221 const user_id_cursor = try name_to_user_id.getCursor(hash.hashInt(evt.admin_repo_opts.hash, name)) orelse return null; 222 var user_id: [evt.event_id_size]u8 = undefined; 223 _ = try user_id_cursor.readBytes(&user_id); 224 225 return try readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, &user_id); 226 } 227 228 // flip a user's ANSI-art preference by re-emitting their User event with 229 // enable_ansi negated. a no-op for an unknown user. `repo` must be writable. 230 pub fn toggleAnsi( 231 comptime repo_opts: rp.RepoOpts(.xit), 232 io: std.Io, 233 allocator: std.mem.Allocator, 234 repo: *rp.Repo(.xit, repo_opts), 235 user_id: []const u8, 236 ) !void { 237 const DB = rp.Repo(.xit, repo_opts).DB; 238 var arena = std.heap.ArenaAllocator.init(allocator); 239 defer arena.deinit(); 240 241 const moment = try evt.currentMoment(repo_opts, repo); 242 const user = (try readById(DB, repo_opts.hash, moment, &arena, user_id)) orelse return; 243 244 var updated = user; 245 updated.enable_ansi = !user.enable_ansi; 246 try evt.commitAndConsume(repo_opts, io, allocator, repo, evt.events_ref, &[_]evt.EventWithId{.{ 247 .id = std.fmt.bytesToHex(user_id[0..evt.event_id_size].*, .lower), 248 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 249 .event = .{ .user = updated }, 250 }}); 251 } 252