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