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