╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ User.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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