╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ User.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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(.xit, 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 }}, false); 251 } 252