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