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