const std = @import("std"); const util = @import("util"); const builtin = @import("builtin"); const db = @import("./db.zig"); const models = @import("./db/models.zig"); pub const DateTime = util.DateTime; pub const Uuid = util.Uuid; const Config = @import("./main.zig").Config; const PwHash = std.crypto.pwhash.scrypt; const pw_hash_params = PwHash.Params.interactive; const pw_hash_encoding = .phc; const pw_hash_buf_size = 128; const token_len = 20; const token_str_len = std.base64.standard.Encoder.calcSize(token_len); const invite_code_len = 16; const invite_code_str_len = std.base64.url_safe.Encoder.calcSize(invite_code_len); // Frees an api struct and its fields allocated from alloc pub fn free(alloc: std.mem.Allocator, val: anytype) void { switch (@typeInfo(@TypeOf(val))) { .Pointer => |ptr_info| switch (ptr_info.size) { .One => { free(alloc, val.*); alloc.destroy(val); }, .Slice => { for (val) |elem| free(alloc, elem); alloc.free(val); }, else => unreachable, }, .Struct => inline for (std.meta.fields(@TypeOf(val))) |f| free(alloc, @field(val, f.name)), .Array => for (val) |elem| free(alloc, elem), .Optional => if (val) |opt| free(alloc, opt), .Bool, .Int, .Float, .Enum => {}, else => unreachable, } } pub fn firstIndexOf(str: []const u8, ch: u8) ?usize { for (str) |c, i| { if (c == ch) return i; } return null; } pub fn CreateInfo(comptime T: type) type { const t_fields = std.meta.fields(T); var fields: [t_fields.len - 1]std.builtin.Type.StructField = undefined; var count = 0; inline for (t_fields) |f| { if (std.mem.eql(u8, f.name, "id")) continue; fields[count] = f; count += 1; } return @Type(.{ .Struct = .{ .layout = .Auto, .fields = &fields, .decls = &[0]std.builtin.Type.Declaration{}, .is_tuple = false, } }); } fn reify(comptime T: type, id: Uuid, val: CreateInfo(T)) T { var result: T = undefined; result.id = id; inline for (std.meta.fields(CreateInfo(T))) |f| { @field(result, f.name) = @field(val, f.name); } return result; } pub const NoteCreateInfo = struct { content: []const u8, }; pub const Scheme = models.Community.Scheme; pub const CommunityCreateOptions = struct { name: []const u8, host: []const u8, }; pub const RegistrationInfo = struct { username: []const u8, password: []const u8, email: ?[]const u8, invite_code: ?[]const u8, }; pub const LoginResult = struct { user_id: Uuid, token: [token_str_len]u8, issued_at: DateTime, }; pub const InviteOptions = struct { name: []const u8 = "", max_uses: ?i64 = null, lifetime: ?i64 = null, // unix seconds, TODO make a TimeSpan type to_community: ?[]const u8, }; threadlocal var prng: std.rand.DefaultPrng = undefined; pub fn initThreadPrng(seed: u64) void { prng = std.rand.DefaultPrng.init(seed +% std.Thread.getCurrentId()); } // Returned slice points into buf fn hashPassword(password: []const u8, alloc: std.mem.Allocator, buf: *[pw_hash_buf_size]u8) ![]const u8 { return PwHash.strHash(password, .{ .allocator = alloc, .params = pw_hash_params, .encoding = pw_hash_encoding }, buf); } pub const ApiSource = struct { db: db.Database, internal_alloc: std.mem.Allocator, config: Config, pub const Conn = ApiConn(db.Database); const root_username = "root"; const root_id = Uuid.nil; const root_password_envvar = "CLUSTER_ROOT_PASSWORD"; const cluster_community_id = Uuid.nil; pub fn init(alloc: std.mem.Allocator, cfg: Config) !ApiSource { var self = ApiSource{ .db = try db.Database.init(), .internal_alloc = alloc, .config = cfg, }; if ((try self.db.execRow2(&.{i64}, "SELECT 1 FROM user WHERE id = ? LIMIT 1;", .{root_id}, null)) == null) { std.log.info("No cluster root user detected. Creating...", .{}); const root_password = std.os.getenv(root_password_envvar) orelse { std.log.err( "No root user created and no password specified. Please provide the password for the root user by the ${s} environment variable for initial startup.", .{root_password_envvar}, ); @panic("No root password provided"); }; var buf: [pw_hash_buf_size]u8 = undefined; const hash = try hashPassword(root_password, self.internal_alloc, &buf); try self.db.insert2("community", .{ .id = cluster_community_id, .name = "Cluster System Pseudocommunity", .host = cfg.cluster_host, .scheme = cfg.cluster_scheme, }); try self.db.insert2("user", .{ .id = root_id, .username = root_username, .community_id = cluster_community_id, }); try self.db.insert2("local_user", .{ .user_id = root_id, .hashed_password = hash, .invite_id = null, .email = null, }); } return self; } pub fn connectUnauthorized(self: *ApiSource, host: []const u8, alloc: std.mem.Allocator) !Conn { const community_id = blk: { const result = try self.db.execRow2(&.{Uuid}, "SELECT id FROM community WHERE host = ?", .{host}, null); if (result) |r| break :blk r[0]; break :blk null; }; if (community_id == null and !util.ciutf8.eql(self.config.cluster_host, host)) { return error.NoCommunity; } return Conn{ .db = self.db, .internal_alloc = self.internal_alloc, .as_user = null, .on_community = community_id, .arena = std.heap.ArenaAllocator.init(alloc), }; } pub fn connectToken(self: *ApiSource, host: []const u8, token: []const u8, alloc: std.mem.Allocator) !Conn { var conn = try self.connectUnauthorized(host, alloc); errdefer conn.close(); const decoded_len = std.base64.standard.Decoder.calcSizeForSlice(token) catch return error.InvalidToken; if (decoded_len != token_len) return error.InvalidToken; var decoded: [token_len]u8 = undefined; std.base64.standard.Decoder.decode(&decoded, token) catch return error.InvalidToken; var hash: models.ByteArray(models.Token.hash_len) = undefined; models.Token.HashFn.hash(&decoded, &hash.data, .{}); const db_token = (try self.db.getBy(models.Token, .hash, hash, conn.arena.allocator())) orelse return error.InvalidToken; //const token_result = try self.db.execRow2( //&.{Uuid}, //\\SELECT user.id //\\FROM token //\\ JOIN user ON token.user_id = user.id //\\ JOIN community ON //); //const token_result = (try self.db.execRow2( //&.{Uuid}, //"SELECT id FROM token WHERE hash = ?", //.{hash}, //null, //)) orelse return error.InvalidToken; //conn.as_user = token_result[0]; conn.as_user = db_token.user_id; return conn; } }; fn ApiConn(comptime DbConn: type) type { return struct { const Self = @This(); db: DbConn, internal_alloc: std.mem.Allocator, // used *only* for large, internal buffers as_user: ?Uuid, on_community: ?Uuid, arena: std.heap.ArenaAllocator, pub fn close(self: *Self) void { self.arena.deinit(); } fn getAuthenticatedUser(self: *Self) !models.User { if (self.as_user) |id| { const user = try self.db.getBy(models.User, .id, id, self.arena.allocator()); if (user == null) return error.NotAuthorized; return user.?; } else { return error.NotAuthorized; } } fn getAuthenticatedLocalUser(self: *Self) !models.LocalUser { if (self.as_user) |user_id| { const local_user = try self.db.getBy(models.LocalUser, .user_id, user_id, self.arena.allocator()); if (local_user == null) return error.NotAuthorized; return local_user.?; } else { return error.NotAuthorized; } } fn getAuthenticatedActor(self: *Self) !models.Actor { return if (self.as_user) |user_id| (try self.db.getBy(models.Actor, .user_id, user_id, self.arena.allocator())) orelse error.NotAuthorized else error.NotAuthorized; } pub fn login(self: *Self, username: []const u8, password: []const u8) !LoginResult { // TODO: This gives away the existence of a user through a timing side channel. is that acceptable? //const user_info = (try self.db.getBy(models.User, .username, username, self.arena.allocator())) orelse return error.InvalidLogin; //const local_user_info = (try self.db.getBy(models.LocalUser, .user_id, user_info.id, self.arena.allocator())) orelse return error.InvalidLogin; const user_info = (try self.db.execRow2( &.{ Uuid, []const u8 }, \\SELECT user.id, local_user.hashed_password \\FROM user JOIN local_user ON local_user.user_id = user.id \\WHERE user.username = ? , .{username}, self.arena.allocator(), )) orelse return error.InvalidLogin; const user_id = user_info[0]; const hashed_password = user_info[1]; //defer free(self.arena.allocator(), user_info); const Hash = std.crypto.pwhash.scrypt; Hash.strVerify(hashed_password, password, .{ .allocator = self.internal_alloc }) catch |err| switch (err) { error.PasswordVerificationFailed => return error.InvalidLogin, else => return err, }; const token = try self.createToken(user_id); var token_enc: [token_str_len]u8 = undefined; _ = std.base64.standard.Encoder.encode(&token_enc, &token.value); return LoginResult{ .user_id = user_id, .token = token_enc, .issued_at = token.info.issued_at, }; } const TokenResult = struct { info: models.Token, value: [token_len]u8, }; fn createToken(self: *Self, user_id: Uuid) !TokenResult { var token: [token_len]u8 = undefined; std.crypto.random.bytes(&token); var hash: [models.Token.hash_len]u8 = undefined; models.Token.HashFn.hash(&token, &hash, .{}); const db_token = models.Token{ .id = Uuid.randV4(prng.random()), .hash = .{ .data = hash }, .user_id = user_id, .issued_at = DateTime.now(), }; try self.db.insert2("token", db_token); return TokenResult{ .info = db_token, .value = token, }; } pub fn createInvite(self: *Self, options: InviteOptions) !models.Invite { const id = Uuid.randV4(prng.random()); const user = try self.getAuthenticatedUser(); // Users can only make invites to their own community, unless they // are system users const community_id = if (options.to_community) |host| blk: { const desired_community = (try self.db.execRow2( &.{Uuid}, "SELECT id FROM community WHERE host = ?", .{host}, null, )) orelse return error.CommunityNotFound; if (user.community_id != null and !Uuid.eql(desired_community[0], user.community_id.?)) { return error.WrongCommunity; } break :blk desired_community[0]; } else null; if (user.community_id != null and community_id == null) { return error.WrongCommunity; } var code: [invite_code_len]u8 = undefined; std.crypto.random.bytes(&code); var code_str = try self.arena.allocator().alloc(u8, invite_code_str_len); _ = std.base64.url_safe.Encoder.encode(code_str, &code); const now = DateTime.now(); const expires_at = if (options.lifetime) |lifetime| DateTime{ .seconds_since_epoch = lifetime + now.seconds_since_epoch, } else null; const invite = models.Invite{ .id = id, .name = try self.arena.allocator().dupe(u8, options.name), .created_by = user.id, .invite_code = code_str, .to_community = community_id, .max_uses = options.max_uses, .created_at = now, .expires_at = expires_at, }; try self.db.insert(models.Invite, invite); return invite; } pub fn getInvite(self: *Self, id: Uuid) !?models.Invite { return self.db.getBy(models.Invite, .id, id, self.arena.allocator()); } }; }