╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ User.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
175 password: []const u8, 176 out: []u8, 177 io: std.Io, 178 ) ![]const u8 { 179 return bcrypt.strHash(password, .{ 180 .params = bcrypt.Params.owasp, 181 .encoding = .phc, 182 }, out, io); 183 } 184 185 pub const VerifyResult = union(enum) { 186 success: [evt.event_id_size]u8, 187 unknown_user, 188 wrong_password, 189 }; 190 191 // look up a user by name (via the name->user-id index) and verify the 192 // supplied password against the stored bcrypt hash. used by both the TTY 193 // login submit and the server's /login route. 194 pub fn verifyCredentials( 195 comptime DB: type, 196 comptime hash_kind: hash.HashKind, 197 haxy_moment: DB.HashMap(.read_only), 198 arena: *std.heap.ArenaAllocator, 199 name: []const u8, 200 password: []const u8, 201 ) !VerifyResult { 202 const name_index_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "name->user-id")) orelse return .unknown_user; 203 const name_index = try DB.HashMap(.read_only).init(name_index_cursor); 204 205 const user_id_cursor = try name_index.getCursor(hash.hashInt(hash_kind, name)) orelse return .unknown_user; 206 var user_id: [evt.event_id_size]u8 = undefined; 207 _ = try user_id_cursor.readBytes(&user_id); 208 209 const user_key = hash.hashInt(hash_kind, &user_id); 210 const event_id_to_user_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "event-id->user")) orelse return .unknown_user; 211 const event_id_to_user = try DB.HashMap(.read_only).init(event_id_to_user_cursor); 212 213 const user_cursor = try event_id_to_user.getCursor(user_key) orelse return .unknown_user; 214 const user_map = try DB.HashMap(.read_only).init(user_cursor); 215 const user_event = try evt.read(Record, DB, hash_kind, arena, user_map); 216 217 bcrypt.strVerify(user_event.event.password_hash, password, .{ .silently_truncate_password = false }) catch { 218 return .wrong_password; 219 }; 220 221 return .{ .success = user_id }; 222 } 223 224 // read a user by event id via the event-id->user index, or null if the id 225 // isn't a known user. field byte slices are allocated in `arena`. 226 pub fn readById( 227 comptime DB: type, 228 comptime hash_kind: hash.HashKind, 229 haxy_moment: DB.HashMap(.read_only), 230 arena: *std.heap.ArenaAllocator, 231 user_id: []const u8, 232 ) !?Record { 233 const event_id_to_user_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "event-id->user")) orelse return null; 234 const event_id_to_user = try DB.HashMap(.read_only).init(event_id_to_user_cursor); 235 const user_cursor = try event_id_to_user.getCursor(hash.hashInt(hash_kind, user_id)) orelse return null; 236 const user_map = try DB.HashMap(.read_only).init(user_cursor); 237 return try evt.read(Record, DB, hash_kind, arena, user_map); 238 } 239 240 // read a user by email via the email->user-id index, or null when no user has 241 // the email. field byte slices are allocated in `arena`. 242 pub fn readByEmail( 243 comptime DB: type, 244 comptime hash_kind: hash.HashKind, 245 haxy_moment: DB.HashMap(.read_only), 246 arena: *std.heap.ArenaAllocator, 247 email: []const u8, 248 ) !?Record { 249 const email_to_user_id_cursor = (try haxy_moment.getCursor(hash.hashInt(hash_kind, email_to_user_id_key))) orelse return null; 250 const email_to_user_id = try DB.HashMap(.read_only).init(email_to_user_id_cursor); 251 const user_id_cursor = (try email_to_user_id.getCursor(hash.hashInt(hash_kind, email))) orelse return null; 252 var user_id: [evt.event_id_size]u8 = undefined; 253 _ = try user_id_cursor.readBytes(&user_id); 254 return try readById(DB, hash_kind, haxy_moment, arena, &user_id); 255 } 256 257 // read a user by name from the admin event store, or null if the admin repo or 258 // the user doesn't exist. field byte slices are allocated in `arena`. 259 pub fn readByName( 260 io: std.Io, 261 allocator: std.mem.Allocator, 262 admin_repo_path: []const u8, 263 arena: *std.heap.ArenaAllocator, 264 name: []const u8, 265 ) !?Record { 266 var repo = rp.Repo(.xit, evt.admin_repo_opts).open(io, allocator, .{ .path = admin_repo_path }) catch |err| switch (err) { 267 error.RepoNotFound => return null, 268 else => |e| return e, 269 }; 270 defer repo.deinit(io, allocator); 271 272 const moment = try evt.currentMoment(evt.admin_repo_opts, &repo); 273 274 const name_to_user_id_cursor = try moment.getCursor(hash.hashInt(evt.admin_repo_opts.hash, "name->user-id")) orelse return null; 275 const name_to_user_id = try evt.AdminDB.HashMap(.read_only).init(name_to_user_id_cursor); 276 const user_id_cursor = try name_to_user_id.getCursor(hash.hashInt(evt.admin_repo_opts.hash, name)) orelse return null; 277 var user_id: [evt.event_id_size]u8 = undefined; 278 _ = try user_id_cursor.readBytes(&user_id); 279 280 return try readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, &user_id); 281 } 282 283 // flip a user's ANSI-art preference by re-emitting their User event with 284 // enable_ansi negated. a no-op for an unknown user. `repo` must be writable. 285 pub fn toggleAnsi( 286 comptime repo_opts: rp.RepoOpts(.xit), 287 io: std.Io, 288 allocator: std.mem.Allocator, 289 repo: *rp.Repo(.xit, repo_opts), 290 user_id: []const u8, 291 ) !void { 292 const DB = rp.Repo(.xit, repo_opts).DB; 293 var arena = std.heap.ArenaAllocator.init(allocator); 294 defer arena.deinit(); 295 296 const moment = try evt.currentMoment(repo_opts, repo); 297 const user = (try readById(DB, repo_opts.hash, moment, &arena, user_id)) orelse return; 298 299 var updated = user.event; 300 updated.enable_ansi = !updated.enable_ansi; 301 try evt.consume(.xit, repo_opts, io, allocator, repo, evt.events_ref, &[_]evt.EventWithId{.{ 302 .id = std.fmt.bytesToHex(user_id[0..evt.event_id_size].*, .lower), 303 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 304 .author_email = user.event.email, 305 .event = .{ .user = updated }, 306 }}); 307 } 308