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