╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ User.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
117 pub const password_hash_max_len = bcrypt.hash_length * 2; 118 119 pub fn hashPassword( 120 password: []const u8, 121 out: []u8, 122 io: std.Io, 123 ) ![]const u8 { 124 return bcrypt.strHash(password, .{ 125 .params = bcrypt.Params.owasp, 126 .encoding = .phc, 127 }, out, io); 128 } 129 130 pub const VerifyResult = union(enum) { 131 success: [evt.event_id_size]u8, 132 unknown_user, 133 wrong_password, 134 }; 135 136 // look up a user by name (via the name->user-id index) and verify the 137 // supplied password against the stored bcrypt hash. used by both the TTY 138 // login submit and the server's /login route. 139 pub fn verifyCredentials( 140 comptime DB: type, 141 comptime hash_kind: hash.HashKind, 142 haxy_moment: DB.HashMap(.read_only), 143 arena: *std.heap.ArenaAllocator, 144 name: []const u8, 145 password: []const u8, 146 ) !VerifyResult { 147 const name_index_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "name->user-id")) orelse return .unknown_user; 148 const name_index = try DB.HashMap(.read_only).init(name_index_cursor); 149 150 const user_id_cursor = try name_index.getCursor(hash.hashInt(hash_kind, name)) orelse return .unknown_user; 151 var user_id: [evt.event_id_size]u8 = undefined; 152 _ = try user_id_cursor.readBytes(&user_id); 153 154 const user_key = hash.hashInt(hash_kind, &user_id); 155 const event_id_to_user_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "event-id->user")) orelse return .unknown_user; 156 const event_id_to_user = try DB.HashMap(.read_only).init(event_id_to_user_cursor); 157 158 const user_cursor = try event_id_to_user.getCursor(user_key) orelse return .unknown_user; 159 const user_map = try DB.HashMap(.read_only).init(user_cursor); 160 const user_event = try evt.read(@This(), DB, hash_kind, arena, user_map); 161 162 bcrypt.strVerify(user_event.password_hash, password, .{ .silently_truncate_password = false }) catch { 163 return .wrong_password; 164 }; 165 166 return .{ .success = user_id }; 167 } 168 169 // read a user by event id via the event-id->user index, or null if the id 170 // isn't a known user. field byte slices are allocated in `arena`. 171 pub fn readById( 172 comptime DB: type, 173 comptime hash_kind: hash.HashKind, 174 haxy_moment: DB.HashMap(.read_only), 175 arena: *std.heap.ArenaAllocator, 176 user_id: []const u8, 177 ) !?@This() { 178 const event_id_to_user_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "event-id->user")) orelse return null; 179 const event_id_to_user = try DB.HashMap(.read_only).init(event_id_to_user_cursor); 180 const user_cursor = try event_id_to_user.getCursor(hash.hashInt(hash_kind, user_id)) orelse return null; 181 const user_map = try DB.HashMap(.read_only).init(user_cursor); 182 return try evt.read(@This(), DB, hash_kind, arena, user_map); 183 } 184 185 // read a user by name from the admin event store, or null if the admin repo or 186 // the user doesn't exist. field byte slices are allocated in `arena`. 187 pub fn readByName( 188 io: std.Io, 189 allocator: std.mem.Allocator, 190 admin_repo_path: []const u8, 191 arena: *std.heap.ArenaAllocator, 192 name: []const u8, 193 ) !?Self { 194 var repo = rp.Repo(.xit, evt.admin_repo_opts).open(io, allocator, .{ .path = admin_repo_path }) catch |err| switch (err) { 195 error.RepoNotFound => return null, 196 else => |e| return e, 197 }; 198 defer repo.deinit(io, allocator); 199 200 const moment = try evt.currentMoment(evt.admin_repo_opts, &repo); 201 202 const name_to_user_id_cursor = try moment.getCursor(hash.hashInt(evt.admin_repo_opts.hash, "name->user-id")) orelse return null; 203 const name_to_user_id = try evt.AdminDB.HashMap(.read_only).init(name_to_user_id_cursor); 204 const user_id_cursor = try name_to_user_id.getCursor(hash.hashInt(evt.admin_repo_opts.hash, name)) orelse return null; 205 var user_id: [evt.event_id_size]u8 = undefined; 206 _ = try user_id_cursor.readBytes(&user_id); 207 208 return try readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, &user_id); 209 } 210 211 // flip a user's ANSI-art preference by re-emitting their User event with 212 // enable_ansi negated. a no-op for an unknown user. `repo` must be writable. 213 pub fn toggleAnsi( 214 comptime repo_opts: rp.RepoOpts(.xit), 215 io: std.Io, 216 allocator: std.mem.Allocator, 217 repo: *rp.Repo(.xit, repo_opts), 218 user_id: []const u8, 219 ) !void { 220 const DB = rp.Repo(.xit, repo_opts).DB; 221 var arena = std.heap.ArenaAllocator.init(allocator); 222 defer arena.deinit(); 223 224 const moment = try evt.currentMoment(repo_opts, repo); 225 const user = (try readById(DB, repo_opts.hash, moment, &arena, user_id)) orelse return; 226 227 var updated = user; 228 updated.enable_ansi = !user.enable_ansi; 229 try evt.commitAndConsume(repo_opts, io, allocator, repo, evt.events_ref, &[_]evt.EventWithId{.{ 230 .id = std.fmt.bytesToHex(user_id[0..evt.event_id_size].*, .lower), 231 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 232 .event = .{ .user = updated }, 233 }}); 234 } 235