2020-05-10 18:42:31 +09:00
|
|
|
import * as bcrypt from 'bcryptjs';
|
|
|
|
import { v4 as uuid } from 'uuid';
|
2021-08-19 21:55:45 +09:00
|
|
|
import generateNativeUserToken from '../server/api/common/generate-native-user-token';
|
|
|
|
import { genRsaKeyPair } from '@/misc/gen-key-pair';
|
|
|
|
import { User } from '@/models/entities/user';
|
|
|
|
import { UserProfile } from '@/models/entities/user-profile';
|
2020-05-10 18:42:31 +09:00
|
|
|
import { getConnection } from 'typeorm';
|
2021-08-19 21:55:45 +09:00
|
|
|
import { genId } from '@/misc/gen-id';
|
|
|
|
import { UserKeypair } from '@/models/entities/user-keypair';
|
|
|
|
import { UsedUsername } from '@/models/entities/used-username';
|
2020-05-10 18:42:31 +09: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);
|
|
|
|
|
|
|
|
let account!: User;
|
|
|
|
|
|
|
|
// Start transaction
|
|
|
|
await getConnection().transaction(async transactionalEntityManager => {
|
2021-02-06 11:43:12 +09:00
|
|
|
const exist = await transactionalEntityManager.findOne(User, {
|
|
|
|
usernameLower: username.toLowerCase(),
|
|
|
|
host: null
|
|
|
|
});
|
|
|
|
|
|
|
|
if (exist) throw new Error('the user is already exists');
|
|
|
|
|
|
|
|
account = await transactionalEntityManager.insert(User, {
|
2020-05-10 18:42:31 +09:00
|
|
|
id: genId(),
|
|
|
|
createdAt: new Date(),
|
|
|
|
username: username,
|
|
|
|
usernameLower: username.toLowerCase(),
|
|
|
|
host: null,
|
|
|
|
token: secret,
|
|
|
|
isAdmin: false,
|
|
|
|
isLocked: true,
|
2020-12-11 21:16:20 +09:00
|
|
|
isExplorable: false,
|
2020-05-10 18:42:31 +09:00
|
|
|
isBot: true,
|
2021-02-06 11:43:12 +09:00
|
|
|
}).then(x => transactionalEntityManager.findOneOrFail(User, x.identifiers[0]));
|
2020-05-10 18:42:31 +09:00
|
|
|
|
2021-02-06 11:43:12 +09:00
|
|
|
await transactionalEntityManager.insert(UserKeypair, {
|
2020-05-10 18:42:31 +09:00
|
|
|
publicKey: keyPair.publicKey,
|
|
|
|
privateKey: keyPair.privateKey,
|
|
|
|
userId: account.id
|
2021-02-06 11:43:12 +09:00
|
|
|
});
|
2020-05-10 18:42:31 +09:00
|
|
|
|
2021-02-06 11:43:12 +09:00
|
|
|
await transactionalEntityManager.insert(UserProfile, {
|
2020-05-10 18:42:31 +09:00
|
|
|
userId: account.id,
|
|
|
|
autoAcceptFollowed: false,
|
|
|
|
password: hash,
|
2021-02-06 11:43:12 +09:00
|
|
|
});
|
2020-05-10 18:42:31 +09:00
|
|
|
|
2021-02-06 11:43:12 +09:00
|
|
|
await transactionalEntityManager.insert(UsedUsername, {
|
2020-05-10 18:42:31 +09:00
|
|
|
createdAt: new Date(),
|
|
|
|
username: username.toLowerCase(),
|
2021-02-06 11:43:12 +09:00
|
|
|
});
|
2020-05-10 18:42:31 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
return account;
|
|
|
|
}
|