2023-07-27 07:31:52 +02:00
|
|
|
/*
|
2024-02-13 16:59:27 +01:00
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 07:31:52 +02:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2023-07-15 11:39:38 +02:00
|
|
|
import { randomUUID } from 'node:crypto';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import bcrypt from 'bcryptjs';
|
|
|
|
import { IsNull, DataSource } from 'typeorm';
|
|
|
|
import { genRsaKeyPair } from '@/misc/gen-key-pair.js';
|
2023-09-20 04:33:36 +02:00
|
|
|
import { MiUser } from '@/models/User.js';
|
|
|
|
import { MiUserProfile } from '@/models/UserProfile.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { IdService } from '@/core/IdService.js';
|
2023-09-20 04:33:36 +02:00
|
|
|
import { MiUserKeypair } from '@/models/UserKeypair.js';
|
|
|
|
import { MiUsedUsername } from '@/models/UsedUsername.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { DI } from '@/di-symbols.js';
|
|
|
|
import generateNativeUserToken from '@/misc/generate-native-user-token.js';
|
2022-12-04 07:03:09 +01:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class CreateSystemUserService {
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.db)
|
|
|
|
private db: DataSource,
|
|
|
|
|
|
|
|
private idService: IdService,
|
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2023-08-16 10:51:28 +02:00
|
|
|
public async createSystemUser(username: string): Promise<MiUser> {
|
2023-07-15 11:39:38 +02:00
|
|
|
const password = randomUUID();
|
2023-07-08 00:08:16 +02:00
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
// Generate hash of password
|
|
|
|
const salt = await bcrypt.genSalt(8);
|
|
|
|
const hash = await bcrypt.hash(password, salt);
|
2023-07-08 00:08:16 +02:00
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
// Generate secret
|
|
|
|
const secret = generateNativeUserToken();
|
2023-07-08 00:08:16 +02:00
|
|
|
|
2023-07-21 04:59:00 +02:00
|
|
|
const keyPair = await genRsaKeyPair();
|
2023-07-08 00:08:16 +02:00
|
|
|
|
2023-08-16 10:51:28 +02:00
|
|
|
let account!: MiUser;
|
2023-07-08 00:08:16 +02:00
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
// Start transaction
|
|
|
|
await this.db.transaction(async transactionalEntityManager => {
|
2023-08-16 10:51:28 +02:00
|
|
|
const exist = await transactionalEntityManager.findOneBy(MiUser, {
|
2022-09-17 20:27:08 +02:00
|
|
|
usernameLower: username.toLowerCase(),
|
|
|
|
host: IsNull(),
|
|
|
|
});
|
2023-07-08 00:08:16 +02:00
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
if (exist) throw new Error('the user is already exists');
|
2023-07-08 00:08:16 +02:00
|
|
|
|
2023-08-16 10:51:28 +02:00
|
|
|
account = await transactionalEntityManager.insert(MiUser, {
|
2023-10-16 03:45:22 +02:00
|
|
|
id: this.idService.gen(),
|
2022-09-17 20:27:08 +02:00
|
|
|
username: username,
|
|
|
|
usernameLower: username.toLowerCase(),
|
|
|
|
host: null,
|
|
|
|
token: secret,
|
2023-01-12 13:02:26 +01:00
|
|
|
isRoot: false,
|
2022-09-17 20:27:08 +02:00
|
|
|
isLocked: true,
|
|
|
|
isExplorable: false,
|
|
|
|
isBot: true,
|
2023-08-16 10:51:28 +02:00
|
|
|
}).then(x => transactionalEntityManager.findOneByOrFail(MiUser, x.identifiers[0]));
|
2023-07-08 00:08:16 +02:00
|
|
|
|
2023-08-16 10:51:28 +02:00
|
|
|
await transactionalEntityManager.insert(MiUserKeypair, {
|
2022-09-17 20:27:08 +02:00
|
|
|
publicKey: keyPair.publicKey,
|
|
|
|
privateKey: keyPair.privateKey,
|
|
|
|
userId: account.id,
|
|
|
|
});
|
2023-07-08 00:08:16 +02:00
|
|
|
|
2023-08-16 10:51:28 +02:00
|
|
|
await transactionalEntityManager.insert(MiUserProfile, {
|
2022-09-17 20:27:08 +02:00
|
|
|
userId: account.id,
|
|
|
|
autoAcceptFollowed: false,
|
|
|
|
password: hash,
|
|
|
|
});
|
2023-07-08 00:08:16 +02:00
|
|
|
|
2023-08-16 10:51:28 +02:00
|
|
|
await transactionalEntityManager.insert(MiUsedUsername, {
|
2022-09-17 20:27:08 +02:00
|
|
|
createdAt: new Date(),
|
|
|
|
username: username.toLowerCase(),
|
|
|
|
});
|
|
|
|
});
|
2023-07-08 00:08:16 +02:00
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
return account;
|
|
|
|
}
|
|
|
|
}
|