╭ 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 const ssh = @import("../serve_ssh_protocol.zig"); 8 9 name: []const u8, 10 email: []const u8, 11 password_hash: []const u8, 12 ssh_keys: []const u8 = "", // newline-separated authorized_keys lines (one OpenSSH public key per line) 13 14 // what the db stores: the event's data plus the commit-derived fields 15 pub const Record = struct { 16 event: Self, 17 removed: bool = false, 18 created_order: u64 = 0, 19 updated_order: u64 = 0, 20 }; 21 22 // the subset of a user that anyone may see 23 pub const Public = struct { 24 name: []const u8, 25 }; 26 27 const Self = @This(); 28 29 pub const name_min_len = 4; 30 pub const name_max_len = 32; 31 32 // the moment keys `evt.merge` reads and writes for this kind 33 pub const merge_policy: evt.MergePolicy = .target_wins; 34 pub const record_map_key = "event-id->user"; 35 pub const all_id_set_key = "user-id-set"; 36 pub const name_index_key = "name->user-id"; 37 38 // resolves a commit's author email to its user at read time 39 pub const email_to_user_id_key = "email->user-id"; 40 41 pub const fingerprint_len = ssh.fingerprint_len; 42 43 // the users holding each ssh key. a set, since two users may register one key. 44 pub const fingerprint_to_user_id_set_key = "ssh-fingerprint->user-id-set"; 45 46 pub fn validateName(name: []const u8) !void { 47 if (name.len == 0) return error.NameEmpty; 48 if (name.len < name_min_len) return error.NameTooShort; 49 if (name.len > name_max_len) return error.NameTooLong; 50 if (name[0] == '-' or name[name.len - 1] == '-') return error.InvalidName; 51 // reserved for the top-level pages' url segment 52 if (std.ascii.eqlIgnoreCase(name, "home")) return error.InvalidName; 53 54 var previous_was_hyphen = false; 55 for (name) |c| { 56 if (std.ascii.isAlphanumeric(c)) { 57 previous_was_hyphen = false; 58 } else if (c == '-' and !previous_was_hyphen) { 59 previous_was_hyphen = true; 60 } else { 61 return error.InvalidName; 62 } 63 } 64 } 65 66 pub fn consume( 67 comptime DB: type, 68 comptime hash_kind: hash.HashKind, 69 haxy_moment: DB.HashMap(.read_write), 70 event_id: *const [evt.event_id_size]u8, 71 record_maybe: ?Record, 72 arena: *std.heap.ArenaAllocator, 73 _: ?[]const u8, 74 ) !void { 75 const user_key = hash.hashInt(hash_kind, event_id); 76 77 const event_id_to_user_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "event-id->user")); 78 const event_id_to_user = try DB.HashMap(.read_write).init(event_id_to_user_cursor); 79 80 const name_to_user_id_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, name_index_key)); 81 const name_to_user_id = try DB.SortedMap(.read_write).init(name_to_user_id_cursor); 82 83 const email_to_user_id_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, email_to_user_id_key)); 84 const email_to_user_id = try DB.HashMap(.read_write).init(email_to_user_id_cursor); 85 86 const fingerprint_to_user_id_set_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, fingerprint_to_user_id_set_key)); 87 const fingerprint_to_user_id_set = try DB.HashMap(.read_write).init(fingerprint_to_user_id_set_cursor); 88 89 var existing_record_maybe: ?Record = null; 90 const existing_cursor_maybe = try event_id_to_user.getCursor(user_key); 91 if (existing_cursor_maybe) |existing_cursor| { 92 const existing_user = try DB.HashMap(.read_only).init(existing_cursor); 93 existing_record_maybe = try evt.read(Record, DB, hash_kind, arena, existing_user); 94 } 95 96 var record_to_write = record_maybe orelse try evt.removedRecord(Record, DB, hash_kind, haxy_moment.readOnly(), existing_record_maybe); 97 98 if (!record_to_write.removed) try validateName(record_to_write.event.name); 99 100 if (existing_record_maybe) |existing_record| { 101 // updates preserve the original creation metadata 102 record_to_write.created_order = existing_record.created_order; 103 104 // drop the old active indexes; active values are re-added below 105 if (!existing_record.removed) { 106 _ = try name_to_user_id.remove(existing_record.event.name); 107 _ = try email_to_user_id.remove(hash.hashInt(hash_kind, existing_record.event.email)); 108 109 var keys = std.mem.splitScalar(u8, existing_record.event.ssh_keys, '\n'); 110 while (keys.next()) |line| { 111 const fingerprint = fingerprintOfKey(line) orelse continue; 112 const user_id_set = try DB.SortedSet(.read_write).init(try fingerprint_to_user_id_set.putCursor(hash.hashInt(hash_kind, &fingerprint))); 113 _ = try user_id_set.remove(event_id); 114 } 115 } 116 } 117 118 const user_cursor = try event_id_to_user.putCursor(user_key); 119 const user = try DB.HashMap(.read_write).init(user_cursor); 120 try evt.upsert(Record, DB, hash_kind, user, record_to_write); 121 try evt.indexEvent(DB, hash_kind, haxy_moment, event_id, .user, existing_record_maybe, record_to_write); 122 123 const order_key = evt.orderKeyDesc(record_to_write.created_order, event_id); 124 125 // the id set retains removed records so merges can carry removals 126 if (existing_cursor_maybe == null) { 127 const user_id_set_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, all_id_set_key)); 128 const user_id_set = try DB.SortedSet(.read_write).init(user_id_set_cursor); 129 try user_id_set.put(&order_key); 130 } 131 132 if (!record_to_write.removed) { 133 try name_to_user_id.put(record_to_write.event.name, .{ .bytes = event_id }); 134 try email_to_user_id.put(hash.hashInt(hash_kind, record_to_write.event.email), .{ .bytes = event_id }); 135 136 var keys = std.mem.splitScalar(u8, record_to_write.event.ssh_keys, '\n'); 137 while (keys.next()) |line| { 138 const fingerprint = fingerprintOfKey(line) orelse continue; 139 const user_id_set = try DB.SortedSet(.read_write).init(try fingerprint_to_user_id_set.putCursor(hash.hashInt(hash_kind, &fingerprint))); 140 try user_id_set.put(event_id); 141 } 142 } 143 } 144 145 // the SHA256 fingerprint of one authorized_keys line, or null if the line is 146 // blank or not a parseable "<type> <base64> [comment]" entry 147 pub fn fingerprintOfKey(line: []const u8) ?[ssh.fingerprint_len]u8 { 148 var it = std.mem.tokenizeAny(u8, line, " \t\r"); 149 _ = it.next() orelse return null; // key type 150 const blob_b64 = it.next() orelse return null; 151 152 const decoder = std.base64.standard.Decoder; 153 const blob_len = decoder.calcSizeForSlice(blob_b64) catch return null; 154 var blob_buf: [4096]u8 = undefined; 155 if (blob_len > blob_buf.len) return null; 156 decoder.decode(blob_buf[0..blob_len], blob_b64) catch return null; 157 158 return ssh.formatFingerprint(blob_buf[0..blob_len]); 159 } 160 161 pub const password_hash_max_len = bcrypt.hash_length * 2; 162 163 pub fn hashPassword( 164 password: []const u8, 165 out: []u8, 166 io: std.Io, 167 ) ![]const u8 { 168 return bcrypt.strHash(password, .{ 169 .params = bcrypt.Params.owasp, 170 .encoding = .phc, 171 }, out, io); 172 } 173 174 pub const VerifyResult = union(enum) { 175 success: [evt.event_id_size]u8, 176 unknown_user, 177 wrong_password, 178 }; 179 180 // look up a user by name (via the name->user-id index) and verify the 181 // supplied password against the stored bcrypt hash. used by both the TTY 182 // login submit and the server's /login route. 183 pub fn verifyCredentials( 184 comptime DB: type, 185 comptime hash_kind: hash.HashKind, 186 haxy_moment: DB.HashMap(.read_only), 187 arena: *std.heap.ArenaAllocator, 188 name: []const u8, 189 password: []const u8, 190 ) !VerifyResult { 191 const user_id = try readIdByName(DB, hash_kind, haxy_moment, name) orelse return .unknown_user; 192 193 const user_key = hash.hashInt(hash_kind, &user_id); 194 const event_id_to_user_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "event-id->user")) orelse return .unknown_user; 195 const event_id_to_user = try DB.HashMap(.read_only).init(event_id_to_user_cursor); 196 197 const user_cursor = try event_id_to_user.getCursor(user_key) orelse return .unknown_user; 198 const user_map = try DB.HashMap(.read_only).init(user_cursor); 199 const user_event = try evt.read(Record, DB, hash_kind, arena, user_map); 200 201 if (!verifyPassword(user_event.event.password_hash, password)) return .wrong_password; 202 203 return .{ .success = user_id }; 204 } 205 206 pub fn verifyPassword(password_hash: []const u8, password: []const u8) bool { 207 bcrypt.strVerify(password_hash, password, .{ .silently_truncate_password = false }) catch return false; 208 return true; 209 } 210 211 // read a user by event id via the event-id->user index, or null if the id 212 // isn't a known user. field byte slices are allocated in `arena`. 213 pub fn readById( 214 comptime DB: type, 215 comptime hash_kind: hash.HashKind, 216 haxy_moment: DB.HashMap(.read_only), 217 arena: *std.heap.ArenaAllocator, 218 user_id: []const u8, 219 ) !?Record { 220 const user_map = try userMap(DB, hash_kind, haxy_moment, user_id) orelse return null; 221 return try evt.read(Record, DB, hash_kind, arena, user_map); 222 } 223 224 // read just the public part of a user by email via the email->user-id index, or 225 // null when no user has the email. field byte slices are allocated in `arena`. 226 pub fn readByEmail( 227 comptime DB: type, 228 comptime hash_kind: hash.HashKind, 229 haxy_moment: DB.HashMap(.read_only), 230 arena: *std.heap.ArenaAllocator, 231 email: []const u8, 232 ) !?Public { 233 const user_id = (try readIdByEmail(DB, hash_kind, haxy_moment, email)) orelse return null; 234 const user_map = try userMap(DB, hash_kind, haxy_moment, &user_id) orelse return null; 235 const record = try evt.read(struct { event: Public }, DB, hash_kind, arena, user_map); 236 return record.event; 237 } 238 239 // a user's id via the email->user-id index 240 pub fn readIdByEmail( 241 comptime DB: type, 242 comptime hash_kind: hash.HashKind, 243 haxy_moment: DB.HashMap(.read_only), 244 email: []const u8, 245 ) !?[evt.event_id_size]u8 { 246 const email_to_user_id_cursor = (try haxy_moment.getCursor(hash.hashInt(hash_kind, email_to_user_id_key))) orelse return null; 247 const email_to_user_id = try DB.HashMap(.read_only).init(email_to_user_id_cursor); 248 const user_id_cursor = (try email_to_user_id.getCursor(hash.hashInt(hash_kind, email))) orelse return null; 249 var user_id: [evt.event_id_size]u8 = undefined; 250 _ = try user_id_cursor.readBytes(&user_id); 251 return user_id; 252 } 253 254 // a user's id via the name->user-id index 255 pub fn readIdByName( 256 comptime DB: type, 257 comptime hash_kind: hash.HashKind, 258 haxy_moment: DB.HashMap(.read_only), 259 name: []const u8, 260 ) !?[evt.event_id_size]u8 { 261 const index_cursor = (try haxy_moment.getCursor(hash.hashInt(hash_kind, name_index_key))) orelse return null; 262 const index = try DB.SortedMap(.read_only).init(index_cursor); 263 const user_id_cursor = (try index.getCursor(name)) orelse return null; 264 var user_id: [evt.event_id_size]u8 = undefined; 265 _ = try user_id_cursor.readBytes(&user_id); 266 return user_id; 267 } 268 269 // the ids of the active users holding the ssh key with this fingerprint 270 pub fn readIdsByFingerprint( 271 comptime DB: type, 272 comptime hash_kind: hash.HashKind, 273 haxy_moment: DB.HashMap(.read_only), 274 arena: *std.heap.ArenaAllocator, 275 fingerprint: *const [ssh.fingerprint_len]u8, 276 ) ![]const [evt.event_id_size]u8 { 277 var user_ids: std.ArrayList([evt.event_id_size]u8) = .empty; 278 const index_cursor = (try haxy_moment.getCursor(hash.hashInt(hash_kind, fingerprint_to_user_id_set_key))) orelse return user_ids.items; 279 const index = try DB.HashMap(.read_only).init(index_cursor); 280 const user_id_set_cursor = (try index.getCursor(hash.hashInt(hash_kind, fingerprint))) orelse return user_ids.items; 281 const user_id_set = try DB.SortedSet(.read_only).init(user_id_set_cursor); 282 283 var iter = try user_id_set.iterator(); 284 while (try iter.next()) |kv_pair_cursor| { 285 var cursor = kv_pair_cursor; 286 const kv_pair = try cursor.readKeyValuePair(); 287 var user_id: [evt.event_id_size]u8 = undefined; 288 _ = try kv_pair.key_cursor.readBytes(&user_id); 289 try user_ids.append(arena.allocator(), user_id); 290 } 291 return user_ids.items; 292 } 293 294 // whether the user holds the ssh key with this fingerprint 295 pub fn holdsKey( 296 comptime DB: type, 297 comptime hash_kind: hash.HashKind, 298 haxy_moment: DB.HashMap(.read_only), 299 arena: *std.heap.ArenaAllocator, 300 user_id: *const [evt.event_id_size]u8, 301 fingerprint: *const [ssh.fingerprint_len]u8, 302 ) !bool { 303 for (try readIdsByFingerprint(DB, hash_kind, haxy_moment, arena, fingerprint)) |holder_id| { 304 if (std.mem.eql(u8, &holder_id, user_id)) return true; 305 } 306 return false; 307 } 308 309 // a user's record map via the event-id->user index 310 fn userMap( 311 comptime DB: type, 312 comptime hash_kind: hash.HashKind, 313 haxy_moment: DB.HashMap(.read_only), 314 user_id: []const u8, 315 ) !?DB.HashMap(.read_only) { 316 const event_id_to_user_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "event-id->user")) orelse return null; 317 const event_id_to_user = try DB.HashMap(.read_only).init(event_id_to_user_cursor); 318 const user_cursor = try event_id_to_user.getCursor(hash.hashInt(hash_kind, user_id)) orelse return null; 319 return try DB.HashMap(.read_only).init(user_cursor); 320 } 321 322 // read a user by name from the admin event store, or null if the admin repo or 323 // the user doesn't exist. field byte slices are allocated in `arena`. 324 pub fn readByName( 325 io: std.Io, 326 allocator: std.mem.Allocator, 327 admin_repo_path: []const u8, 328 arena: *std.heap.ArenaAllocator, 329 name: []const u8, 330 ) !?Record { 331 var repo = rp.Repo(.xit, evt.admin_repo_opts).open(io, allocator, .{ .path = admin_repo_path }) catch |err| switch (err) { 332 error.RepoNotFound => return null, 333 else => |e| return e, 334 }; 335 defer repo.deinit(io, allocator); 336 337 const moment = try evt.currentMoment(evt.admin_repo_opts, &repo); 338 339 const user_id = try readIdByName(evt.AdminDB, evt.admin_repo_opts.hash, moment, name) orelse return null; 340 341 return try readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, &user_id); 342 } 343