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