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
|
|
|
|
*/
|
|
|
|
|
2024-07-20 14:33:20 +02:00
|
|
|
import { generateKeyPair } from 'node:crypto';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import bcrypt from 'bcryptjs';
|
|
|
|
import { DataSource, IsNull } from 'typeorm';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2024-09-22 05:53:13 +02:00
|
|
|
import type { MiMeta, UsedUsernamesRepository, UsersRepository } from '@/models/_.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 generateUserToken from '@/misc/generate-native-user-token.js';
|
2022-12-04 02:16:03 +01:00
|
|
|
import { UserEntityService } from '@/core/entities/UserEntityService.js';
|
2024-02-04 12:46:28 +01:00
|
|
|
import { InstanceActorService } from '@/core/InstanceActorService.js';
|
2022-12-04 07:03:09 +01:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2023-04-29 10:03:14 +02:00
|
|
|
import UsersChart from '@/core/chart/charts/users.js';
|
|
|
|
import { UtilityService } from '@/core/UtilityService.js';
|
2024-07-29 14:31:32 +02:00
|
|
|
import { UserService } from '@/core/UserService.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class SignupService {
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.db)
|
|
|
|
private db: DataSource,
|
|
|
|
|
2024-09-22 05:53:13 +02:00
|
|
|
@Inject(DI.meta)
|
|
|
|
private meta: MiMeta,
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
@Inject(DI.usersRepository)
|
|
|
|
private usersRepository: UsersRepository,
|
|
|
|
|
|
|
|
@Inject(DI.usedUsernamesRepository)
|
|
|
|
private usedUsernamesRepository: UsedUsernamesRepository,
|
|
|
|
|
|
|
|
private utilityService: UtilityService,
|
2024-07-29 14:31:32 +02:00
|
|
|
private userService: UserService,
|
2022-09-17 20:27:08 +02:00
|
|
|
private userEntityService: UserEntityService,
|
|
|
|
private idService: IdService,
|
2024-02-04 12:46:28 +01:00
|
|
|
private instanceActorService: InstanceActorService,
|
2022-09-17 20:27:08 +02:00
|
|
|
private usersChart: UsersChart,
|
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2022-09-17 20:27:08 +02:00
|
|
|
public async signup(opts: {
|
2023-08-16 10:51:28 +02:00
|
|
|
username: MiUser['username'];
|
2022-09-17 20:27:08 +02:00
|
|
|
password?: string | null;
|
2023-08-16 10:51:28 +02:00
|
|
|
passwordHash?: MiUserProfile['password'] | null;
|
2022-09-17 20:27:08 +02:00
|
|
|
host?: string | null;
|
2023-04-29 10:03:14 +02:00
|
|
|
ignorePreservedUsernames?: boolean;
|
2022-09-17 20:27:08 +02:00
|
|
|
}) {
|
|
|
|
const { username, password, passwordHash, host } = opts;
|
|
|
|
let hash = passwordHash;
|
2023-07-08 00:08:16 +02:00
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
// Validate username
|
|
|
|
if (!this.userEntityService.validateLocalUsername(username)) {
|
|
|
|
throw new Error('INVALID_USERNAME');
|
|
|
|
}
|
2023-07-08 00:08:16 +02:00
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
if (password != null && passwordHash == null) {
|
|
|
|
// Validate password
|
|
|
|
if (!this.userEntityService.validatePassword(password)) {
|
|
|
|
throw new Error('INVALID_PASSWORD');
|
|
|
|
}
|
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);
|
|
|
|
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 = generateUserToken();
|
2023-07-08 00:08:16 +02:00
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
// Check username duplication
|
2024-02-08 08:04:41 +01:00
|
|
|
if (await this.usersRepository.exists({ where: { usernameLower: username.toLowerCase(), host: IsNull() } })) {
|
2022-09-17 20:27:08 +02:00
|
|
|
throw new Error('DUPLICATED_USERNAME');
|
|
|
|
}
|
2023-07-08 00:08:16 +02:00
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
// Check deleted username duplication
|
2024-02-08 08:04:41 +01:00
|
|
|
if (await this.usedUsernamesRepository.exists({ where: { username: username.toLowerCase() } })) {
|
2022-09-17 20:27:08 +02:00
|
|
|
throw new Error('USED_USERNAME');
|
|
|
|
}
|
2023-04-29 10:03:14 +02:00
|
|
|
|
2024-02-04 12:46:28 +01:00
|
|
|
const isTheFirstUser = !await this.instanceActorService.realLocalUsersPresent();
|
2023-05-02 12:26:18 +02:00
|
|
|
|
2023-05-02 18:21:18 +02:00
|
|
|
if (!opts.ignorePreservedUsernames && !isTheFirstUser) {
|
2024-09-22 05:53:13 +02:00
|
|
|
const isPreserved = this.meta.preservedUsernames.map(x => x.toLowerCase()).includes(username.toLowerCase());
|
2023-04-29 10:03:14 +02:00
|
|
|
if (isPreserved) {
|
|
|
|
throw new Error('USED_USERNAME');
|
|
|
|
}
|
|
|
|
}
|
2023-05-02 12:26:18 +02:00
|
|
|
|
2024-07-20 14:33:20 +02:00
|
|
|
const keyPair = await new Promise<string[]>((res, rej) =>
|
|
|
|
generateKeyPair('rsa', {
|
|
|
|
modulusLength: 2048,
|
|
|
|
publicKeyEncoding: {
|
|
|
|
type: 'spki',
|
|
|
|
format: 'pem',
|
|
|
|
},
|
|
|
|
privateKeyEncoding: {
|
|
|
|
type: 'pkcs8',
|
|
|
|
format: 'pem',
|
|
|
|
cipher: undefined,
|
|
|
|
passphrase: undefined,
|
|
|
|
},
|
|
|
|
}, (err, publicKey, privateKey) =>
|
|
|
|
err ? rej(err) : res([publicKey, privateKey]),
|
|
|
|
));
|
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 username is already used');
|
2023-07-08 00:08:16 +02:00
|
|
|
|
2023-08-16 10:51:28 +02:00
|
|
|
account = await transactionalEntityManager.save(new 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: this.utilityService.toPunyNullable(host),
|
|
|
|
token: secret,
|
2023-05-02 12:26:18 +02:00
|
|
|
isRoot: isTheFirstUser,
|
2022-09-17 20:27:08 +02:00
|
|
|
}));
|
2023-07-08 00:08:16 +02:00
|
|
|
|
2023-08-16 10:51:28 +02:00
|
|
|
await transactionalEntityManager.save(new MiUserKeypair({
|
2024-07-20 14:33:20 +02:00
|
|
|
publicKey: keyPair[0],
|
|
|
|
privateKey: keyPair[1],
|
2022-09-17 20:27:08 +02:00
|
|
|
userId: account.id,
|
|
|
|
}));
|
2023-07-08 00:08:16 +02:00
|
|
|
|
2023-08-16 10:51:28 +02:00
|
|
|
await transactionalEntityManager.save(new MiUserProfile({
|
2022-09-17 20:27:08 +02:00
|
|
|
userId: account.id,
|
|
|
|
autoAcceptFollowed: true,
|
|
|
|
password: hash,
|
|
|
|
}));
|
2023-07-08 00:08:16 +02:00
|
|
|
|
2023-08-16 10:51:28 +02:00
|
|
|
await transactionalEntityManager.save(new MiUsedUsername({
|
2022-09-17 20:27:08 +02:00
|
|
|
createdAt: new Date(),
|
|
|
|
username: username.toLowerCase(),
|
|
|
|
}));
|
|
|
|
});
|
2023-07-08 00:08:16 +02:00
|
|
|
|
2024-07-29 14:31:32 +02:00
|
|
|
this.usersChart.update(account, true).then();
|
|
|
|
this.userService.notifySystemWebhook(account, 'userCreated').then();
|
2023-07-08 00:08:16 +02:00
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
return { account, secret };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|