╭ User.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const evt = @import("../event.zig"); 3 const xit = @import("xit"); 4 const rp = xit.repo; 5 const hash = xit.hash; 6 const bcrypt = std.crypto.pwhash.bcrypt; 7 8 name: []const u8, 9 display_name: []const u8, 10 email: []const u8, 11 password_hash: []const u8, 12 enable_ansi: bool = false, 13 14 const Self = @This(); 15 16 // the subset of a user that's safe to hand to clients 17 pub const Safe = struct { 18 name: []const u8, 19 display_name: []const u8, 20 21 pub fn init(user: Self) Safe { 22 return .{ .name = user.name, .display_name = user.display_name }; 23 } 24 }; 25 26 pub fn consume( 27 comptime DB: type, 28 comptime hash_kind: hash.HashKind, 29 haxy_moment: DB.HashMap(.read_write), 30 event_id: *const [evt.event_id_size]u8, 31 event_maybe: ?@This(), 32 arena: *std.heap.ArenaAllocator, 33 ) !void { 34 const user_key = hash.hashInt(hash_kind, event_id); 35 36 const event_id_to_user_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "event-id->user")); 37 const event_id_to_user = try DB.HashMap(.read_write).init(event_id_to_user_cursor); 38 39 const name_to_user_id_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "name->user-id")); 40 const name_to_user_id = try DB.HashMap(.read_write).init(name_to_user_id_cursor); 41 42 if (event_maybe) |event| { 43 // if this event_id already maps to a user with a different name, 44 // drop the stale name->id entry first 45 if (try event_id_to_user.getCursor(user_key)) |existing_cursor| { 46 const existing_user = try DB.HashMap(.read_only).init(existing_cursor); 47 const existing_event = try evt.read(@This(), DB, hash_kind, arena, existing_user); 48 if (!std.mem.eql(u8, existing_event.name, event.name)) { 49 _ = try name_to_user_id.remove(hash.hashInt(hash_kind, existing_event.name)); 50 } 51 } 52 53 const user_cursor = try event_id_to_user.putCursor(user_key); 54 const user = try DB.HashMap(.read_write).init(user_cursor); 55 try evt.upsert(@This(), DB, hash_kind, user, event); 56 57 try name_to_user_id.put(hash.hashInt(hash_kind, event.name), .{ .bytes = event_id }); 58 } else { 59 // read the user's name so we can drop its name->id index entry 60 if (try event_id_to_user.getCursor(user_key)) |existing_cursor| { 61 const existing_user = try DB.HashMap(.read_only).init(existing_cursor); 62 const existing_event = try evt.read(@This(), DB, hash_kind, arena, existing_user); 63 _ = try name_to_user_id.remove(hash.hashInt(hash_kind, existing_event.name)); 64 } 65 66 if (!try event_id_to_user.remove(user_key)) return error.EventNotFound; 67 68 const user_id_to_repos_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "user-id->repos")); 69 const user_id_to_repos = try DB.HashMap(.read_write).init(user_id_to_repos_cursor); 70 _ = try user_id_to_repos.remove(user_key); 71 } 72 } 73 74 pub const password_hash_max_len = bcrypt.hash_length * 2; 75 76 pub fn hashPassword( 77 password: []const u8, 78 out: []u8, 79 io: std.Io, 80 ) ![]const u8 { 81 return bcrypt.strHash(password, .{ 82 .params = bcrypt.Params.owasp, 83 .encoding = .phc, 84 }, out, io); 85 } 86 87 pub const VerifyResult = union(enum) { 88 success: [evt.event_id_size]u8, 89 unknown_user, 90 wrong_password, 91 }; 92 93 // look up a user by name (via the name->user-id index) and verify the 94 // supplied password against the stored bcrypt hash. used by both the TTY 95 // login submit and the server's /login route. 96 pub fn verifyCredentials( 97 comptime DB: type, 98 comptime hash_kind: hash.HashKind, 99 haxy_moment: DB.HashMap(.read_only), 100 arena: *std.heap.ArenaAllocator, 101 name: []const u8, 102 password: []const u8, 103 ) !VerifyResult { 104 const name_index_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "name->user-id")) orelse return .unknown_user; 105 const name_index = try DB.HashMap(.read_only).init(name_index_cursor); 106 107 const user_id_cursor = try name_index.getCursor(hash.hashInt(hash_kind, name)) orelse return .unknown_user; 108 var user_id: [evt.event_id_size]u8 = undefined; 109 _ = try user_id_cursor.readBytes(&user_id); 110 111 const user_key = hash.hashInt(hash_kind, &user_id); 112 const event_id_to_user_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "event-id->user")) orelse return .unknown_user; 113 const event_id_to_user = try DB.HashMap(.read_only).init(event_id_to_user_cursor); 114 115 const user_cursor = try event_id_to_user.getCursor(user_key) orelse return .unknown_user; 116 const user_map = try DB.HashMap(.read_only).init(user_cursor); 117 const user_event = try evt.read(@This(), DB, hash_kind, arena, user_map); 118 119 bcrypt.strVerify(user_event.password_hash, password, .{ .silently_truncate_password = false }) catch { 120 return .wrong_password; 121 }; 122 123 return .{ .success = user_id }; 124 } 125 126 // read a user by event id via the event-id->user index, or null if the id 127 // isn't a known user. field byte slices are allocated in `arena`. 128 pub fn readById( 129 comptime DB: type, 130 comptime hash_kind: hash.HashKind, 131 haxy_moment: DB.HashMap(.read_only), 132 arena: *std.heap.ArenaAllocator, 133 user_id: []const u8, 134 ) !?@This() { 135 const event_id_to_user_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "event-id->user")) orelse return null; 136 const event_id_to_user = try DB.HashMap(.read_only).init(event_id_to_user_cursor); 137 const user_cursor = try event_id_to_user.getCursor(hash.hashInt(hash_kind, user_id)) orelse return null; 138 const user_map = try DB.HashMap(.read_only).init(user_cursor); 139 return try evt.read(@This(), DB, hash_kind, arena, user_map); 140 } 141 142 // flip a user's ANSI-art preference by re-emitting their User event with 143 // enable_ansi negated. a no-op for an unknown user. `repo` must be writable. 144 pub fn toggleAnsi( 145 comptime repo_opts: rp.RepoOpts(.xit), 146 io: std.Io, 147 allocator: std.mem.Allocator, 148 repo: *rp.Repo(.xit, repo_opts), 149 user_id: []const u8, 150 ) !void { 151 const DB = rp.Repo(.xit, repo_opts).DB; 152 var arena = std.heap.ArenaAllocator.init(allocator); 153 defer arena.deinit(); 154 155 const moment = try evt.currentMoment(repo_opts, repo); 156 const user = (try readById(DB, repo_opts.hash, moment, &arena, user_id)) orelse return; 157 158 var updated = user; 159 updated.enable_ansi = !user.enable_ansi; 160 try evt.commitAndConsume(repo_opts, io, allocator, repo, evt.events_ref, &[_]evt.EventWithId{.{ 161 .id = std.fmt.bytesToHex(user_id[0..evt.event_id_size].*, .lower), 162 .event = .{ .user = updated }, 163 }}); 164 } 165