╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ User.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
71 try user_list.append(.{ .bytes = event_id }); 72 } 73 } else { 74 // read the user's name so we can drop its name->id index entry 75 if (try event_id_to_user.getCursor(user_key)) |existing_cursor| { 76 const existing_user = try DB.HashMap(.read_only).init(existing_cursor); 77 const existing_event = try evt.read(@This(), DB, hash_kind, arena, existing_user); 78 _ = try name_to_user_id.remove(hash.hashInt(hash_kind, existing_event.name)); 79 } 80 81 if (!try event_id_to_user.remove(user_key)) return error.EventNotFound; 82 83 const user_id_to_repos_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "user-id->repos")); 84 const user_id_to_repos = try DB.HashMap(.read_write).init(user_id_to_repos_cursor); 85 _ = try user_id_to_repos.remove(user_key); 86 } 87 } 88 89 pub const password_hash_max_len = bcrypt.hash_length * 2; 90 91 pub fn hashPassword( 92 password: []const u8, 93 out: []u8, 94 io: std.Io, 95 ) ![]const u8 { 96 return bcrypt.strHash(password, .{ 97 .params = bcrypt.Params.owasp, 98 .encoding = .phc, 99 }, out, io); 100 } 101 102 pub const VerifyResult = union(enum) { 103 success: [evt.event_id_size]u8, 104 unknown_user, 105 wrong_password, 106 }; 107 108 // look up a user by name (via the name->user-id index) and verify the 109 // supplied password against the stored bcrypt hash. used by both the TTY 110 // login submit and the server's /login route. 111 pub fn verifyCredentials( 112 comptime DB: type, 113 comptime hash_kind: hash.HashKind, 114 haxy_moment: DB.HashMap(.read_only), 115 arena: *std.heap.ArenaAllocator, 116 name: []const u8, 117 password: []const u8, 118 ) !VerifyResult { 119 const name_index_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "name->user-id")) orelse return .unknown_user; 120 const name_index = try DB.HashMap(.read_only).init(name_index_cursor); 121 122 const user_id_cursor = try name_index.getCursor(hash.hashInt(hash_kind, name)) orelse return .unknown_user; 123 var user_id: [evt.event_id_size]u8 = undefined; 124 _ = try user_id_cursor.readBytes(&user_id); 125 126 const user_key = hash.hashInt(hash_kind, &user_id); 127 const event_id_to_user_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "event-id->user")) orelse return .unknown_user; 128 const event_id_to_user = try DB.HashMap(.read_only).init(event_id_to_user_cursor); 129 130 const user_cursor = try event_id_to_user.getCursor(user_key) orelse return .unknown_user; 131 const user_map = try DB.HashMap(.read_only).init(user_cursor); 132 const user_event = try evt.read(@This(), DB, hash_kind, arena, user_map); 133 134 bcrypt.strVerify(user_event.password_hash, password, .{ .silently_truncate_password = false }) catch { 135 return .wrong_password; 136 }; 137 138 return .{ .success = user_id }; 139 } 140 141 // read a user by event id via the event-id->user index, or null if the id 142 // isn't a known user. field byte slices are allocated in `arena`. 143 pub fn readById( 144 comptime DB: type, 145 comptime hash_kind: hash.HashKind, 146 haxy_moment: DB.HashMap(.read_only), 147 arena: *std.heap.ArenaAllocator, 148 user_id: []const u8, 149 ) !?@This() { 150 const event_id_to_user_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "event-id->user")) orelse return null; 151 const event_id_to_user = try DB.HashMap(.read_only).init(event_id_to_user_cursor); 152 const user_cursor = try event_id_to_user.getCursor(hash.hashInt(hash_kind, user_id)) orelse return null; 153 const user_map = try DB.HashMap(.read_only).init(user_cursor); 154 return try evt.read(@This(), DB, hash_kind, arena, user_map); 155 } 156 157 // read a user by name from the admin event store, or null if the admin repo or 158 // the user doesn't exist. field byte slices are allocated in `arena`. 159 pub fn readByName( 160 io: std.Io, 161 allocator: std.mem.Allocator, 162 admin_repo_path: []const u8, 163 arena: *std.heap.ArenaAllocator, 164 name: []const u8, 165 ) !?Self { 166 var repo = rp.Repo(.xit, evt.admin_repo_opts).open(io, allocator, .{ .path = admin_repo_path }) catch |err| switch (err) { 167 error.RepoNotFound => return null, 168 else => |e| return e, 169 }; 170 defer repo.deinit(io, allocator); 171 172 const moment = try evt.currentMoment(evt.admin_repo_opts, &repo); 173 174 const name_to_user_id_cursor = try moment.getCursor(hash.hashInt(evt.admin_repo_opts.hash, "name->user-id")) orelse return null; 175 const name_to_user_id = try evt.AdminDB.HashMap(.read_only).init(name_to_user_id_cursor); 176 const user_id_cursor = try name_to_user_id.getCursor(hash.hashInt(evt.admin_repo_opts.hash, name)) orelse return null; 177 var user_id: [evt.event_id_size]u8 = undefined; 178 _ = try user_id_cursor.readBytes(&user_id); 179 180 return try readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, &user_id); 181 } 182 183 // flip a user's ANSI-art preference by re-emitting their User event with 184 // enable_ansi negated. a no-op for an unknown user. `repo` must be writable. 185 pub fn toggleAnsi( 186 comptime repo_opts: rp.RepoOpts(.xit), 187 io: std.Io, 188 allocator: std.mem.Allocator, 189 repo: *rp.Repo(.xit, repo_opts), 190 user_id: []const u8, 191 ) !void { 192 const DB = rp.Repo(.xit, repo_opts).DB; 193 var arena = std.heap.ArenaAllocator.init(allocator); 194 defer arena.deinit(); 195 196 const moment = try evt.currentMoment(repo_opts, repo); 197 const user = (try readById(DB, repo_opts.hash, moment, &arena, user_id)) orelse return; 198 199 var updated = user; 200 updated.enable_ansi = !user.enable_ansi; 201 try evt.commitAndConsume(repo_opts, io, allocator, repo, evt.events_ref, &[_]evt.EventWithId{.{ 202 .id = std.fmt.bytesToHex(user_id[0..evt.event_id_size].*, .lower), 203 .event = .{ .user = updated }, 204 }}); 205 } 206