2022-02-26 20:07:39 -06:00
|
|
|
import bcrypt from 'bcryptjs';
|
2020-05-10 04:42:31 -05:00
|
|
|
import { v4 as uuid } from 'uuid';
|
2022-02-26 20:07:39 -06:00
|
|
|
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';
|
2022-02-19 08:21:28 -06:00
|
|
|
import { getConnection, ObjectLiteral } from 'typeorm';
|
2022-02-26 20:07:39 -06:00
|
|
|
import { genId } from '@/misc/gen-id.js';
|
|
|
|
import { UserKeypair } from '@/models/entities/user-keypair.js';
|
|
|
|
import { UsedUsername } from '@/models/entities/used-username.js';
|
2020-05-10 04:42:31 -05:00
|
|
|
|
|
|
|
export async function createSystemUser(username: string) {
|
|
|
|
const password = uuid();
|
|
|
|
|
|
|
|
// Generate hash of password
|
|
|
|
const salt = await bcrypt.genSalt(8);
|
|
|
|
const hash = await bcrypt.hash(password, salt);
|
|
|
|
|
|
|
|
// Generate secret
|
|
|
|
const secret = generateNativeUserToken();
|
|
|
|
|
|
|
|
const keyPair = await genRsaKeyPair(4096);
|
|
|
|
|
2022-02-19 08:21:28 -06:00
|
|
|
let account!: User | ObjectLiteral;
|
2020-05-10 04:42:31 -05:00
|
|
|
|
|
|
|
// Start transaction
|
|
|
|
await getConnection().transaction(async transactionalEntityManager => {
|
2021-02-05 20:43:12 -06:00
|
|
|
const exist = await transactionalEntityManager.findOne(User, {
|
|
|
|
usernameLower: username.toLowerCase(),
|
2021-12-09 08:58:30 -06:00
|
|
|
host: null,
|
2021-02-05 20:43:12 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
if (exist) throw new Error('the user is already exists');
|
|
|
|
|
|
|
|
account = await transactionalEntityManager.insert(User, {
|
2020-05-10 04:42:31 -05:00
|
|
|
id: genId(),
|
|
|
|
createdAt: new Date(),
|
|
|
|
username: username,
|
|
|
|
usernameLower: username.toLowerCase(),
|
|
|
|
host: null,
|
|
|
|
token: secret,
|
|
|
|
isAdmin: false,
|
|
|
|
isLocked: true,
|
2020-12-11 06:16:20 -06:00
|
|
|
isExplorable: false,
|
2020-05-10 04:42:31 -05:00
|
|
|
isBot: true,
|
2021-02-05 20:43:12 -06:00
|
|
|
}).then(x => transactionalEntityManager.findOneOrFail(User, x.identifiers[0]));
|
2020-05-10 04:42:31 -05:00
|
|
|
|
2021-02-05 20:43:12 -06:00
|
|
|
await transactionalEntityManager.insert(UserKeypair, {
|
2020-05-10 04:42:31 -05:00
|
|
|
publicKey: keyPair.publicKey,
|
|
|
|
privateKey: keyPair.privateKey,
|
2021-12-09 08:58:30 -06:00
|
|
|
userId: account.id,
|
2021-02-05 20:43:12 -06:00
|
|
|
});
|
2020-05-10 04:42:31 -05:00
|
|
|
|
2021-02-05 20:43:12 -06:00
|
|
|
await transactionalEntityManager.insert(UserProfile, {
|
2020-05-10 04:42:31 -05:00
|
|
|
userId: account.id,
|
|
|
|
autoAcceptFollowed: false,
|
|
|
|
password: hash,
|
2021-02-05 20:43:12 -06:00
|
|
|
});
|
2020-05-10 04:42:31 -05:00
|
|
|
|
2021-02-05 20:43:12 -06:00
|
|
|
await transactionalEntityManager.insert(UsedUsername, {
|
2020-05-10 04:42:31 -05:00
|
|
|
createdAt: new Date(),
|
|
|
|
username: username.toLowerCase(),
|
2021-02-05 20:43:12 -06:00
|
|
|
});
|
2020-05-10 04:42:31 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
return account;
|
|
|
|
}
|