2022-09-17 18:27:08 +00:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import rndstr from 'rndstr';
|
|
|
|
import bcrypt from 'bcryptjs';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2022-12-24 23:30:13 +00:00
|
|
|
import type { RegistrationTicketsRepository, UserPendingsRepository, UserProfilesRepository, UsersRepository } from '@/models/index.js';
|
2022-09-20 20:33:11 +00:00
|
|
|
import type { Config } from '@/config.js';
|
2022-09-17 18:27:08 +00: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';
|
2022-09-24 00:03:04 +00:00
|
|
|
import { ILocalUser } from '@/models/entities/User.js';
|
2022-12-03 10:42:05 +00:00
|
|
|
import { FastifyReplyError } from '@/misc/fastify-reply-error.js';
|
2022-12-04 06:03:09 +00:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2023-01-13 08:59:40 +00:00
|
|
|
import { SigninService } from './SigninService.js';
|
2022-12-13 15:01:45 +00:00
|
|
|
import type { FastifyRequest, FastifyReply } from 'fastify';
|
2022-09-17 18:27:08 +00: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,
|
|
|
|
|
|
|
|
@Inject(DI.registrationTicketsRepository)
|
2022-12-24 23:30:13 +00:00
|
|
|
private registrationTicketsRepository: RegistrationTicketsRepository,
|
2022-09-17 18:27:08 +00: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 06:03:09 +00:00
|
|
|
@bindThis
|
2022-12-03 10:42:05 +00: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-17 18:27:08 +00:00
|
|
|
|
|
|
|
const instance = await this.metaService.fetch(true);
|
|
|
|
|
|
|
|
// Verify *Captcha
|
|
|
|
// ただしテスト時はこの機構は障害となるため無効にする
|
|
|
|
if (process.env.NODE_ENV !== 'test') {
|
|
|
|
if (instance.enableHcaptcha && instance.hcaptchaSecretKey) {
|
2022-12-03 10:42:05 +00:00
|
|
|
await this.captchaService.verifyHcaptcha(instance.hcaptchaSecretKey, body['hcaptcha-response']).catch(err => {
|
|
|
|
throw new FastifyReplyError(400, err);
|
2022-09-17 18:27:08 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (instance.enableRecaptcha && instance.recaptchaSecretKey) {
|
2022-12-03 10:42:05 +00:00
|
|
|
await this.captchaService.verifyRecaptcha(instance.recaptchaSecretKey, body['g-recaptcha-response']).catch(err => {
|
|
|
|
throw new FastifyReplyError(400, err);
|
2022-09-17 18:27:08 +00:00
|
|
|
});
|
|
|
|
}
|
2022-10-13 00:19:57 +00:00
|
|
|
|
|
|
|
if (instance.enableTurnstile && instance.turnstileSecretKey) {
|
2022-12-03 10:42:05 +00:00
|
|
|
await this.captchaService.verifyTurnstile(instance.turnstileSecretKey, body['turnstile-response']).catch(err => {
|
|
|
|
throw new FastifyReplyError(400, err);
|
2022-10-13 00:19:57 +00:00
|
|
|
});
|
|
|
|
}
|
2022-09-17 18:27:08 +00: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'];
|
|
|
|
|
|
|
|
if (instance.emailRequiredForSignup) {
|
|
|
|
if (emailAddress == null || typeof emailAddress !== 'string') {
|
2022-12-03 10:42:05 +00:00
|
|
|
reply.code(400);
|
2022-09-17 18:27:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-12-03 10:42:05 +00:00
|
|
|
const res = await this.emailService.validateEmailForAccount(emailAddress);
|
|
|
|
if (!res.available) {
|
|
|
|
reply.code(400);
|
2022-09-17 18:27:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (instance.disableRegistration) {
|
|
|
|
if (invitationCode == null || typeof invitationCode !== 'string') {
|
2022-12-03 10:42:05 +00:00
|
|
|
reply.code(400);
|
2022-09-17 18:27:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ticket = await this.registrationTicketsRepository.findOneBy({
|
|
|
|
code: invitationCode,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (ticket == null) {
|
2022-12-03 10:42:05 +00:00
|
|
|
reply.code(400);
|
2022-09-17 18:27:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.registrationTicketsRepository.delete(ticket.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (instance.emailRequiredForSignup) {
|
|
|
|
const code = rndstr('a-z0-9', 16);
|
|
|
|
|
|
|
|
// Generate hash of password
|
|
|
|
const salt = await bcrypt.genSalt(8);
|
|
|
|
const hash = await bcrypt.hash(password, salt);
|
|
|
|
|
|
|
|
await this.userPendingsRepository.insert({
|
|
|
|
id: this.idService.genId(),
|
|
|
|
createdAt: new Date(),
|
|
|
|
code,
|
2022-12-03 10:42:05 +00:00
|
|
|
email: emailAddress!,
|
2022-09-17 18:27:08 +00:00
|
|
|
username: username,
|
|
|
|
password: hash,
|
|
|
|
});
|
|
|
|
|
|
|
|
const link = `${this.config.url}/signup-complete/${code}`;
|
|
|
|
|
2022-12-03 10:42:05 +00:00
|
|
|
this.emailService.sendEmail(emailAddress!, 'Signup',
|
2022-09-17 18:27:08 +00:00
|
|
|
`To complete signup, please click this link:<br><a href="${link}">${link}</a>`,
|
|
|
|
`To complete signup, please click this link: ${link}`);
|
|
|
|
|
2022-12-03 10:42:05 +00:00
|
|
|
reply.code(204);
|
2023-02-09 02:02:37 +00:00
|
|
|
return;
|
2022-09-17 18:27:08 +00:00
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
const { account, secret } = await this.signupService.signup({
|
|
|
|
username, password, host,
|
|
|
|
});
|
|
|
|
|
|
|
|
const res = await this.userEntityService.pack(account, account, {
|
|
|
|
detail: true,
|
|
|
|
includeSecrets: true,
|
|
|
|
});
|
|
|
|
|
2022-12-03 10:42:05 +00:00
|
|
|
return {
|
|
|
|
...res,
|
|
|
|
token: secret,
|
|
|
|
};
|
|
|
|
} catch (err) {
|
2023-02-09 01:46:01 +00:00
|
|
|
throw new FastifyReplyError(400, typeof err === 'string' ? err : (err as Error).toString());
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2022-12-03 10:42:05 +00:00
|
|
|
public async signupPending(request: FastifyRequest<{ Body: { code: string; } }>, reply: FastifyReply) {
|
|
|
|
const body = request.body;
|
2022-09-17 18:27:08 +00: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-01-13 08:59:40 +00:00
|
|
|
return this.signinService.signin(request, reply, account as ILocalUser);
|
2022-12-03 10:42:05 +00:00
|
|
|
} catch (err) {
|
2023-02-09 01:46:01 +00:00
|
|
|
throw new FastifyReplyError(400, typeof err === 'string' ? err : (err as Error).toString());
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|