2023-01-13 05:40:33 +01:00
|
|
|
import { v4 as uuid } from "uuid";
|
|
|
|
import generateNativeUserToken from "../server/api/common/generate-native-user-token.js";
|
|
|
|
import { genRsaKeyPair } from "@/misc/gen-key-pair.js";
|
|
|
|
import { User } from "@/models/entities/user.js";
|
|
|
|
import { UserProfile } from "@/models/entities/user-profile.js";
|
|
|
|
import { IsNull } from "typeorm";
|
|
|
|
import { genId } from "@/misc/gen-id.js";
|
|
|
|
import { UserKeypair } from "@/models/entities/user-keypair.js";
|
|
|
|
import { UsedUsername } from "@/models/entities/used-username.js";
|
|
|
|
import { db } from "@/db/postgre.js";
|
2023-04-03 11:23:51 +02:00
|
|
|
import { hashPassword } from "@/misc/password.js";
|
2020-05-10 11:42:31 +02:00
|
|
|
|
|
|
|
export async function createSystemUser(username: string) {
|
|
|
|
const password = uuid();
|
|
|
|
|
|
|
|
// Generate hash of password
|
2023-04-03 11:23:51 +02:00
|
|
|
const hash = await hashPassword(password);
|
2020-05-10 11:42:31 +02:00
|
|
|
|
|
|
|
// Generate secret
|
|
|
|
const secret = generateNativeUserToken();
|
|
|
|
|
|
|
|
const keyPair = await genRsaKeyPair(4096);
|
|
|
|
|
2022-03-26 07:34:00 +01:00
|
|
|
let account!: User;
|
2020-05-10 11:42:31 +02:00
|
|
|
|
|
|
|
// Start transaction
|
2023-01-13 05:40:33 +01:00
|
|
|
await db.transaction(async (transactionalEntityManager) => {
|
2022-03-26 07:34:00 +01:00
|
|
|
const exist = await transactionalEntityManager.findOneBy(User, {
|
2021-02-06 03:43:12 +01:00
|
|
|
usernameLower: username.toLowerCase(),
|
2022-03-26 07:34:00 +01:00
|
|
|
host: IsNull(),
|
2021-02-06 03:43:12 +01:00
|
|
|
});
|
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
if (exist) throw new Error("the user is already exists");
|
2021-02-06 03:43:12 +01:00
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
account = await transactionalEntityManager
|
|
|
|
.insert(User, {
|
|
|
|
id: genId(),
|
|
|
|
createdAt: new Date(),
|
|
|
|
username: username,
|
|
|
|
usernameLower: username.toLowerCase(),
|
|
|
|
host: null,
|
|
|
|
token: secret,
|
|
|
|
isAdmin: false,
|
|
|
|
isLocked: true,
|
|
|
|
isExplorable: false,
|
|
|
|
isBot: true,
|
|
|
|
})
|
|
|
|
.then((x) =>
|
|
|
|
transactionalEntityManager.findOneByOrFail(User, x.identifiers[0]),
|
|
|
|
);
|
2020-05-10 11:42:31 +02:00
|
|
|
|
2021-02-06 03:43:12 +01:00
|
|
|
await transactionalEntityManager.insert(UserKeypair, {
|
2020-05-10 11:42:31 +02:00
|
|
|
publicKey: keyPair.publicKey,
|
|
|
|
privateKey: keyPair.privateKey,
|
2021-12-09 15:58:30 +01:00
|
|
|
userId: account.id,
|
2021-02-06 03:43:12 +01:00
|
|
|
});
|
2020-05-10 11:42:31 +02:00
|
|
|
|
2021-02-06 03:43:12 +01:00
|
|
|
await transactionalEntityManager.insert(UserProfile, {
|
2020-05-10 11:42:31 +02:00
|
|
|
userId: account.id,
|
|
|
|
autoAcceptFollowed: false,
|
|
|
|
password: hash,
|
2021-02-06 03:43:12 +01:00
|
|
|
});
|
2020-05-10 11:42:31 +02:00
|
|
|
|
2021-02-06 03:43:12 +01:00
|
|
|
await transactionalEntityManager.insert(UsedUsername, {
|
2020-05-10 11:42:31 +02:00
|
|
|
createdAt: new Date(),
|
|
|
|
username: username.toLowerCase(),
|
2021-02-06 03:43:12 +01:00
|
|
|
});
|
2020-05-10 11:42:31 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
return account;
|
|
|
|
}
|