2021-05-04 15:05:34 +09:00
|
|
|
import rndstr from 'rndstr';
|
2021-11-12 19:47:04 +09:00
|
|
|
import ms from 'ms';
|
2022-06-14 18:01:23 +09:00
|
|
|
import { IsNull } from 'typeorm';
|
2022-09-18 03:27:08 +09:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2022-09-21 05:33:11 +09:00
|
|
|
import type { PasswordResetRequestsRepository, UserProfilesRepository, UsersRepository } from '@/models/index.js';
|
2022-09-18 03:27:08 +09:00
|
|
|
import { Endpoint } from '@/server/api/endpoint-base.js';
|
|
|
|
import { IdService } from '@/core/IdService.js';
|
2022-09-21 05:33:11 +09:00
|
|
|
import type { Config } from '@/config.js';
|
2022-09-18 03:27:08 +09:00
|
|
|
import { DI } from '@/di-symbols.js';
|
|
|
|
import { EmailService } from '@/core/EmailService.js';
|
2021-05-04 15:05:34 +09:00
|
|
|
|
|
|
|
export const meta = {
|
2022-06-10 14:25:20 +09:00
|
|
|
tags: ['reset password'],
|
|
|
|
|
2022-01-18 22:27:10 +09:00
|
|
|
requireCredential: false,
|
2021-05-04 15:05:34 +09:00
|
|
|
|
2022-06-10 14:25:20 +09:00
|
|
|
description: 'Request a users password to be reset.',
|
|
|
|
|
2021-05-04 15:05:34 +09:00
|
|
|
limit: {
|
|
|
|
duration: ms('1hour'),
|
2021-12-09 23:58:30 +09:00
|
|
|
max: 3,
|
2021-05-04 15:05:34 +09:00
|
|
|
},
|
|
|
|
|
2022-02-19 14:05:32 +09:00
|
|
|
errors: {
|
2021-05-04 15:05:34 +09:00
|
|
|
|
|
|
|
},
|
2022-02-19 14:05:32 +09:00
|
|
|
} as const;
|
2021-05-04 15:05:34 +09:00
|
|
|
|
2022-02-20 13:15:40 +09:00
|
|
|
export const paramDef = {
|
2022-02-19 14:05:32 +09:00
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
username: { type: 'string' },
|
|
|
|
email: { type: 'string' },
|
2021-12-09 23:58:30 +09:00
|
|
|
},
|
2022-02-19 14:05:32 +09:00
|
|
|
required: ['username', 'email'],
|
2022-01-18 22:27:10 +09:00
|
|
|
} as const;
|
2021-05-04 15:05:34 +09:00
|
|
|
|
2022-01-03 02:12:50 +09:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2022-09-18 03:27:08 +09:00
|
|
|
@Injectable()
|
|
|
|
export default class extends Endpoint<typeof meta, typeof paramDef> {
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.config)
|
|
|
|
private config: Config,
|
|
|
|
|
|
|
|
@Inject(DI.usersRepository)
|
|
|
|
private usersRepository: UsersRepository,
|
|
|
|
|
|
|
|
@Inject(DI.userProfilesRepository)
|
|
|
|
private userProfilesRepository: UserProfilesRepository,
|
|
|
|
|
|
|
|
@Inject(DI.passwordResetRequestsRepository)
|
|
|
|
private passwordResetRequestsRepository: PasswordResetRequestsRepository,
|
|
|
|
|
|
|
|
private idService: IdService,
|
|
|
|
private emailService: EmailService,
|
|
|
|
) {
|
|
|
|
super(meta, paramDef, async (ps, me) => {
|
|
|
|
const user = await this.usersRepository.findOneBy({
|
|
|
|
usernameLower: ps.username.toLowerCase(),
|
|
|
|
host: IsNull(),
|
|
|
|
});
|
|
|
|
|
|
|
|
// 合致するユーザーが登録されていなかったら無視
|
|
|
|
if (user == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const profile = await this.userProfilesRepository.findOneByOrFail({ userId: user.id });
|
|
|
|
|
|
|
|
// 合致するメアドが登録されていなかったら無視
|
|
|
|
if (profile.email !== ps.email) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// メアドが認証されていなかったら無視
|
|
|
|
if (!profile.emailVerified) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const token = rndstr('a-z0-9', 64);
|
|
|
|
|
|
|
|
await this.passwordResetRequestsRepository.insert({
|
|
|
|
id: this.idService.genId(),
|
|
|
|
createdAt: new Date(),
|
|
|
|
userId: profile.userId,
|
|
|
|
token,
|
|
|
|
});
|
|
|
|
|
|
|
|
const link = `${this.config.url}/reset-password/${token}`;
|
|
|
|
|
|
|
|
this.emailService.sendEmail(ps.email, 'Password reset requested',
|
|
|
|
`To reset password, please click this link:<br><a href="${link}">${link}</a>`,
|
|
|
|
`To reset password, please click this link: ${link}`);
|
|
|
|
});
|
2021-05-04 15:05:34 +09:00
|
|
|
}
|
2022-09-18 03:27:08 +09:00
|
|
|
}
|