2023-07-27 14:31:52 +09:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
import { generateKeyPair } from 'node:crypto';
|
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import bcrypt from 'bcryptjs';
|
|
|
|
import { DataSource, IsNull } from 'typeorm';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2022-09-24 07:12:11 +09:00
|
|
|
import type { UsedUsernamesRepository, UsersRepository } from '@/models/index.js';
|
2022-09-18 03:27:08 +09:00
|
|
|
import { User } from '@/models/entities/User.js';
|
|
|
|
import { UserProfile } from '@/models/entities/UserProfile.js';
|
|
|
|
import { IdService } from '@/core/IdService.js';
|
|
|
|
import { UserKeypair } from '@/models/entities/UserKeypair.js';
|
|
|
|
import { UsedUsername } from '@/models/entities/UsedUsername.js';
|
|
|
|
import generateUserToken from '@/misc/generate-native-user-token.js';
|
2022-12-04 10:16:03 +09:00
|
|
|
import { UserEntityService } from '@/core/entities/UserEntityService.js';
|
2022-12-04 15:03:09 +09:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2023-04-29 17:03:14 +09:00
|
|
|
import UsersChart from '@/core/chart/charts/users.js';
|
|
|
|
import { UtilityService } from '@/core/UtilityService.js';
|
|
|
|
import { MetaService } from '@/core/MetaService.js';
|
2022-09-18 03:27:08 +09:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class SignupService {
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.db)
|
|
|
|
private db: DataSource,
|
|
|
|
|
|
|
|
@Inject(DI.usersRepository)
|
|
|
|
private usersRepository: UsersRepository,
|
|
|
|
|
|
|
|
@Inject(DI.usedUsernamesRepository)
|
|
|
|
private usedUsernamesRepository: UsedUsernamesRepository,
|
|
|
|
|
|
|
|
private utilityService: UtilityService,
|
|
|
|
private userEntityService: UserEntityService,
|
|
|
|
private idService: IdService,
|
2023-04-29 17:03:14 +09:00
|
|
|
private metaService: MetaService,
|
2022-09-18 03:27:08 +09:00
|
|
|
private usersChart: UsersChart,
|
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
2022-12-04 15:03:09 +09:00
|
|
|
@bindThis
|
2022-09-18 03:27:08 +09:00
|
|
|
public async signup(opts: {
|
|
|
|
username: User['username'];
|
|
|
|
password?: string | null;
|
|
|
|
passwordHash?: UserProfile['password'] | null;
|
|
|
|
host?: string | null;
|
2023-04-29 17:03:14 +09:00
|
|
|
ignorePreservedUsernames?: boolean;
|
2022-09-18 03:27:08 +09:00
|
|
|
}) {
|
|
|
|
const { username, password, passwordHash, host } = opts;
|
|
|
|
let hash = passwordHash;
|
2023-07-08 07:08:16 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
// Validate username
|
|
|
|
if (!this.userEntityService.validateLocalUsername(username)) {
|
|
|
|
throw new Error('INVALID_USERNAME');
|
|
|
|
}
|
2023-07-08 07:08:16 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
if (password != null && passwordHash == null) {
|
|
|
|
// Validate password
|
|
|
|
if (!this.userEntityService.validatePassword(password)) {
|
|
|
|
throw new Error('INVALID_PASSWORD');
|
|
|
|
}
|
2023-07-08 07:08:16 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
// Generate hash of password
|
|
|
|
const salt = await bcrypt.genSalt(8);
|
|
|
|
hash = await bcrypt.hash(password, salt);
|
|
|
|
}
|
2023-07-08 07:08:16 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
// Generate secret
|
|
|
|
const secret = generateUserToken();
|
2023-07-08 07:08:16 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
// Check username duplication
|
2023-07-11 14:58:58 +09:00
|
|
|
if (await this.usersRepository.exist({ where: { usernameLower: username.toLowerCase(), host: IsNull() } })) {
|
2022-09-18 03:27:08 +09:00
|
|
|
throw new Error('DUPLICATED_USERNAME');
|
|
|
|
}
|
2023-07-08 07:08:16 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
// Check deleted username duplication
|
2023-07-11 14:58:58 +09:00
|
|
|
if (await this.usedUsernamesRepository.exist({ where: { username: username.toLowerCase() } })) {
|
2022-09-18 03:27:08 +09:00
|
|
|
throw new Error('USED_USERNAME');
|
|
|
|
}
|
2023-04-29 17:03:14 +09:00
|
|
|
|
2023-05-02 19:26:18 +09:00
|
|
|
const isTheFirstUser = (await this.usersRepository.countBy({ host: IsNull() })) === 0;
|
|
|
|
|
2023-05-03 01:21:18 +09:00
|
|
|
if (!opts.ignorePreservedUsernames && !isTheFirstUser) {
|
2023-04-29 17:03:14 +09:00
|
|
|
const instance = await this.metaService.fetch(true);
|
|
|
|
const isPreserved = instance.preservedUsernames.map(x => x.toLowerCase()).includes(username.toLowerCase());
|
|
|
|
if (isPreserved) {
|
|
|
|
throw new Error('USED_USERNAME');
|
|
|
|
}
|
|
|
|
}
|
2023-05-02 19:26:18 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
const keyPair = await new Promise<string[]>((res, rej) =>
|
|
|
|
generateKeyPair('rsa', {
|
2023-07-21 11:59:00 +09:00
|
|
|
modulusLength: 2048,
|
2022-09-18 03:27:08 +09:00
|
|
|
publicKeyEncoding: {
|
|
|
|
type: 'spki',
|
|
|
|
format: 'pem',
|
|
|
|
},
|
|
|
|
privateKeyEncoding: {
|
|
|
|
type: 'pkcs8',
|
|
|
|
format: 'pem',
|
|
|
|
cipher: undefined,
|
|
|
|
passphrase: undefined,
|
|
|
|
},
|
2023-03-11 08:51:37 +09:00
|
|
|
}, (err, publicKey, privateKey) =>
|
2022-09-18 03:27:08 +09:00
|
|
|
err ? rej(err) : res([publicKey, privateKey]),
|
|
|
|
));
|
2023-07-08 07:08:16 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
let account!: User;
|
2023-07-08 07:08:16 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
// Start transaction
|
|
|
|
await this.db.transaction(async transactionalEntityManager => {
|
|
|
|
const exist = await transactionalEntityManager.findOneBy(User, {
|
|
|
|
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 username is already used');
|
2023-07-08 07:08:16 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
account = await transactionalEntityManager.save(new User({
|
|
|
|
id: this.idService.genId(),
|
|
|
|
createdAt: new Date(),
|
|
|
|
username: username,
|
|
|
|
usernameLower: username.toLowerCase(),
|
|
|
|
host: this.utilityService.toPunyNullable(host),
|
|
|
|
token: secret,
|
2023-05-02 19:26:18 +09:00
|
|
|
isRoot: isTheFirstUser,
|
2022-09-18 03:27:08 +09:00
|
|
|
}));
|
2023-07-08 07:08:16 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
await transactionalEntityManager.save(new UserKeypair({
|
|
|
|
publicKey: keyPair[0],
|
|
|
|
privateKey: keyPair[1],
|
|
|
|
userId: account.id,
|
|
|
|
}));
|
2023-07-08 07:08:16 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
await transactionalEntityManager.save(new UserProfile({
|
|
|
|
userId: account.id,
|
|
|
|
autoAcceptFollowed: true,
|
|
|
|
password: hash,
|
|
|
|
}));
|
2023-07-08 07:08:16 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
await transactionalEntityManager.save(new UsedUsername({
|
|
|
|
createdAt: new Date(),
|
|
|
|
username: username.toLowerCase(),
|
|
|
|
}));
|
|
|
|
});
|
2023-07-08 07:08:16 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
this.usersChart.update(account, true);
|
2023-07-08 07:08:16 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
return { account, secret };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|