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