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