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 { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import bcrypt from 'bcryptjs';
|
2023-04-29 17:03:14 +09:00
|
|
|
import { IsNull } from 'typeorm';
|
2022-09-18 03:27:08 +09:00
|
|
|
import { DI } from '@/di-symbols.js';
|
2023-08-16 17:51:28 +09:00
|
|
|
import type { RegistrationTicketsRepository, UsedUsernamesRepository, UserPendingsRepository, UserProfilesRepository, UsersRepository, MiRegistrationTicket } from '@/models/index.js';
|
2022-09-21 05:33:11 +09:00
|
|
|
import type { Config } from '@/config.js';
|
2022-09-18 03:27:08 +09:00
|
|
|
import { MetaService } from '@/core/MetaService.js';
|
|
|
|
import { CaptchaService } from '@/core/CaptchaService.js';
|
|
|
|
import { IdService } from '@/core/IdService.js';
|
|
|
|
import { SignupService } from '@/core/SignupService.js';
|
|
|
|
import { UserEntityService } from '@/core/entities/UserEntityService.js';
|
|
|
|
import { EmailService } from '@/core/EmailService.js';
|
2023-08-16 17:51:28 +09:00
|
|
|
import { MiLocalUser } from '@/models/entities/User.js';
|
2022-12-03 19:42:05 +09:00
|
|
|
import { FastifyReplyError } from '@/misc/fastify-reply-error.js';
|
2022-12-04 15:03:09 +09:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2023-07-19 11:27:50 +09:00
|
|
|
import { L_CHARS, secureRndstr } from '@/misc/secure-rndstr.js';
|
2023-01-13 17:59:40 +09:00
|
|
|
import { SigninService } from './SigninService.js';
|
2022-12-14 00:01:45 +09:00
|
|
|
import type { FastifyRequest, FastifyReply } from 'fastify';
|
2022-09-18 03:27:08 +09:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class SignupApiService {
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.config)
|
|
|
|
private config: Config,
|
|
|
|
|
|
|
|
@Inject(DI.usersRepository)
|
|
|
|
private usersRepository: UsersRepository,
|
|
|
|
|
|
|
|
@Inject(DI.userProfilesRepository)
|
|
|
|
private userProfilesRepository: UserProfilesRepository,
|
|
|
|
|
|
|
|
@Inject(DI.userPendingsRepository)
|
|
|
|
private userPendingsRepository: UserPendingsRepository,
|
|
|
|
|
2023-03-09 12:57:34 +09:00
|
|
|
@Inject(DI.usedUsernamesRepository)
|
|
|
|
private usedUsernamesRepository: UsedUsernamesRepository,
|
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
@Inject(DI.registrationTicketsRepository)
|
2022-12-25 08:30:13 +09:00
|
|
|
private registrationTicketsRepository: RegistrationTicketsRepository,
|
2022-09-18 03:27:08 +09:00
|
|
|
|
|
|
|
private userEntityService: UserEntityService,
|
|
|
|
private idService: IdService,
|
|
|
|
private metaService: MetaService,
|
|
|
|
private captchaService: CaptchaService,
|
|
|
|
private signupService: SignupService,
|
|
|
|
private signinService: SigninService,
|
|
|
|
private emailService: EmailService,
|
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
2022-12-04 15:03:09 +09:00
|
|
|
@bindThis
|
2022-12-03 19:42:05 +09:00
|
|
|
public async signup(
|
|
|
|
request: FastifyRequest<{
|
|
|
|
Body: {
|
|
|
|
username: string;
|
|
|
|
password: string;
|
|
|
|
host?: string;
|
|
|
|
invitationCode?: string;
|
|
|
|
emailAddress?: string;
|
|
|
|
'hcaptcha-response'?: string;
|
|
|
|
'g-recaptcha-response'?: string;
|
|
|
|
'turnstile-response'?: string;
|
|
|
|
}
|
|
|
|
}>,
|
|
|
|
reply: FastifyReply,
|
|
|
|
) {
|
|
|
|
const body = request.body;
|
2022-09-18 03:27:08 +09:00
|
|
|
|
|
|
|
const instance = await this.metaService.fetch(true);
|
2023-06-25 11:04:33 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
// Verify *Captcha
|
|
|
|
// ただしテスト時はこの機構は障害となるため無効にする
|
|
|
|
if (process.env.NODE_ENV !== 'test') {
|
|
|
|
if (instance.enableHcaptcha && instance.hcaptchaSecretKey) {
|
2022-12-03 19:42:05 +09:00
|
|
|
await this.captchaService.verifyHcaptcha(instance.hcaptchaSecretKey, body['hcaptcha-response']).catch(err => {
|
|
|
|
throw new FastifyReplyError(400, err);
|
2022-09-18 03:27:08 +09:00
|
|
|
});
|
|
|
|
}
|
2023-06-25 11:04:33 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
if (instance.enableRecaptcha && instance.recaptchaSecretKey) {
|
2022-12-03 19:42:05 +09:00
|
|
|
await this.captchaService.verifyRecaptcha(instance.recaptchaSecretKey, body['g-recaptcha-response']).catch(err => {
|
|
|
|
throw new FastifyReplyError(400, err);
|
2022-09-18 03:27:08 +09:00
|
|
|
});
|
|
|
|
}
|
2022-10-13 09:19:57 +09:00
|
|
|
|
|
|
|
if (instance.enableTurnstile && instance.turnstileSecretKey) {
|
2022-12-03 19:42:05 +09:00
|
|
|
await this.captchaService.verifyTurnstile(instance.turnstileSecretKey, body['turnstile-response']).catch(err => {
|
|
|
|
throw new FastifyReplyError(400, err);
|
2022-10-13 09:19:57 +09:00
|
|
|
});
|
|
|
|
}
|
2022-09-18 03:27:08 +09:00
|
|
|
}
|
2023-06-25 11:04:33 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
const username = body['username'];
|
|
|
|
const password = body['password'];
|
|
|
|
const host: string | null = process.env.NODE_ENV === 'test' ? (body['host'] ?? null) : null;
|
|
|
|
const invitationCode = body['invitationCode'];
|
|
|
|
const emailAddress = body['emailAddress'];
|
2023-06-25 11:04:33 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
if (instance.emailRequiredForSignup) {
|
|
|
|
if (emailAddress == null || typeof emailAddress !== 'string') {
|
2022-12-03 19:42:05 +09:00
|
|
|
reply.code(400);
|
2022-09-18 03:27:08 +09:00
|
|
|
return;
|
|
|
|
}
|
2023-06-25 11:04:33 +09:00
|
|
|
|
2022-12-03 19:42:05 +09:00
|
|
|
const res = await this.emailService.validateEmailForAccount(emailAddress);
|
|
|
|
if (!res.available) {
|
|
|
|
reply.code(400);
|
2022-09-18 03:27:08 +09:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2023-06-25 11:04:33 +09:00
|
|
|
|
2023-08-16 17:51:28 +09:00
|
|
|
let ticket: MiRegistrationTicket | null = null;
|
2023-07-15 09:57:58 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
if (instance.disableRegistration) {
|
|
|
|
if (invitationCode == null || typeof invitationCode !== 'string') {
|
2022-12-03 19:42:05 +09:00
|
|
|
reply.code(400);
|
2022-09-18 03:27:08 +09:00
|
|
|
return;
|
|
|
|
}
|
2023-06-25 11:04:33 +09:00
|
|
|
|
2023-07-15 09:57:58 +09:00
|
|
|
ticket = await this.registrationTicketsRepository.findOneBy({
|
2022-09-18 03:27:08 +09:00
|
|
|
code: invitationCode,
|
|
|
|
});
|
2023-06-25 11:04:33 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
if (ticket == null) {
|
2022-12-03 19:42:05 +09:00
|
|
|
reply.code(400);
|
2022-09-18 03:27:08 +09:00
|
|
|
return;
|
|
|
|
}
|
2023-06-25 11:04:33 +09:00
|
|
|
|
2023-07-15 09:57:58 +09:00
|
|
|
if (ticket.expiresAt && ticket.expiresAt < new Date()) {
|
|
|
|
reply.code(400);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ticket.usedAt) {
|
|
|
|
reply.code(400);
|
|
|
|
return;
|
|
|
|
}
|
2022-09-18 03:27:08 +09:00
|
|
|
}
|
2023-06-25 11:04:33 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
if (instance.emailRequiredForSignup) {
|
2023-07-11 14:58:58 +09:00
|
|
|
if (await this.usersRepository.exist({ where: { usernameLower: username.toLowerCase(), host: IsNull() } })) {
|
2023-03-09 12:57:34 +09:00
|
|
|
throw new FastifyReplyError(400, 'DUPLICATED_USERNAME');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check deleted username duplication
|
2023-07-11 14:58:58 +09:00
|
|
|
if (await this.usedUsernamesRepository.exist({ where: { username: username.toLowerCase() } })) {
|
2023-03-09 12:57:34 +09:00
|
|
|
throw new FastifyReplyError(400, 'USED_USERNAME');
|
2023-04-29 17:03:14 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
const isPreserved = instance.preservedUsernames.map(x => x.toLowerCase()).includes(username.toLowerCase());
|
|
|
|
if (isPreserved) {
|
2023-05-02 19:18:57 +09:00
|
|
|
throw new FastifyReplyError(400, 'DENIED_USERNAME');
|
2023-03-09 12:57:34 +09:00
|
|
|
}
|
|
|
|
|
2023-06-25 11:04:33 +09:00
|
|
|
const code = secureRndstr(16, { chars: L_CHARS });
|
2023-03-09 12:57:34 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
// Generate hash of password
|
|
|
|
const salt = await bcrypt.genSalt(8);
|
|
|
|
const hash = await bcrypt.hash(password, salt);
|
2023-03-09 12:57:34 +09:00
|
|
|
|
2023-07-15 09:57:58 +09:00
|
|
|
const pendingUser = await this.userPendingsRepository.insert({
|
2022-09-18 03:27:08 +09:00
|
|
|
id: this.idService.genId(),
|
|
|
|
createdAt: new Date(),
|
|
|
|
code,
|
2022-12-03 19:42:05 +09:00
|
|
|
email: emailAddress!,
|
2022-09-18 03:27:08 +09:00
|
|
|
username: username,
|
|
|
|
password: hash,
|
2023-07-15 09:57:58 +09:00
|
|
|
}).then(x => this.userPendingsRepository.findOneByOrFail(x.identifiers[0]));
|
2023-03-09 12:57:34 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
const link = `${this.config.url}/signup-complete/${code}`;
|
2023-03-09 12:57:34 +09:00
|
|
|
|
2022-12-03 19:42:05 +09:00
|
|
|
this.emailService.sendEmail(emailAddress!, 'Signup',
|
2022-09-18 03:27:08 +09:00
|
|
|
`To complete signup, please click this link:<br><a href="${link}">${link}</a>`,
|
|
|
|
`To complete signup, please click this link: ${link}`);
|
2023-03-09 12:57:34 +09:00
|
|
|
|
2023-07-15 09:57:58 +09:00
|
|
|
if (ticket) {
|
|
|
|
await this.registrationTicketsRepository.update(ticket.id, {
|
|
|
|
usedAt: new Date(),
|
|
|
|
pendingUserId: pendingUser.id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-12-03 19:42:05 +09:00
|
|
|
reply.code(204);
|
2023-02-09 11:02:37 +09:00
|
|
|
return;
|
2022-09-18 03:27:08 +09:00
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
const { account, secret } = await this.signupService.signup({
|
|
|
|
username, password, host,
|
|
|
|
});
|
2023-06-25 11:04:33 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
const res = await this.userEntityService.pack(account, account, {
|
|
|
|
detail: true,
|
|
|
|
includeSecrets: true,
|
|
|
|
});
|
2023-06-25 11:04:33 +09:00
|
|
|
|
2023-07-15 09:57:58 +09:00
|
|
|
if (ticket) {
|
|
|
|
await this.registrationTicketsRepository.update(ticket.id, {
|
|
|
|
usedAt: new Date(),
|
|
|
|
usedBy: account,
|
|
|
|
usedById: account.id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-12-03 19:42:05 +09:00
|
|
|
return {
|
|
|
|
...res,
|
|
|
|
token: secret,
|
|
|
|
};
|
|
|
|
} catch (err) {
|
2023-02-09 10:46:01 +09:00
|
|
|
throw new FastifyReplyError(400, typeof err === 'string' ? err : (err as Error).toString());
|
2022-09-18 03:27:08 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-04 15:03:09 +09:00
|
|
|
@bindThis
|
2022-12-03 19:42:05 +09:00
|
|
|
public async signupPending(request: FastifyRequest<{ Body: { code: string; } }>, reply: FastifyReply) {
|
|
|
|
const body = request.body;
|
2022-09-18 03:27:08 +09:00
|
|
|
|
|
|
|
const code = body['code'];
|
|
|
|
|
|
|
|
try {
|
|
|
|
const pendingUser = await this.userPendingsRepository.findOneByOrFail({ code });
|
|
|
|
|
|
|
|
const { account, secret } = await this.signupService.signup({
|
|
|
|
username: pendingUser.username,
|
|
|
|
passwordHash: pendingUser.password,
|
|
|
|
});
|
|
|
|
|
|
|
|
this.userPendingsRepository.delete({
|
|
|
|
id: pendingUser.id,
|
|
|
|
});
|
|
|
|
|
|
|
|
const profile = await this.userProfilesRepository.findOneByOrFail({ userId: account.id });
|
|
|
|
|
|
|
|
await this.userProfilesRepository.update({ userId: profile.userId }, {
|
|
|
|
email: pendingUser.email,
|
|
|
|
emailVerified: true,
|
|
|
|
emailVerifyCode: null,
|
|
|
|
});
|
|
|
|
|
2023-07-15 09:57:58 +09:00
|
|
|
const ticket = await this.registrationTicketsRepository.findOneBy({ pendingUserId: pendingUser.id });
|
|
|
|
if (ticket) {
|
|
|
|
await this.registrationTicketsRepository.update(ticket.id, {
|
|
|
|
usedBy: account,
|
|
|
|
usedById: account.id,
|
|
|
|
pendingUserId: null,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-08-16 17:51:28 +09:00
|
|
|
return this.signinService.signin(request, reply, account as MiLocalUser);
|
2022-12-03 19:42:05 +09:00
|
|
|
} catch (err) {
|
2023-02-09 10:46:01 +09:00
|
|
|
throw new FastifyReplyError(400, typeof err === 'string' ? err : (err as Error).toString());
|
2022-09-18 03:27:08 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|