2021-05-04 08:05:34 +02:00
|
|
|
import rndstr from 'rndstr';
|
2021-11-12 11:47:04 +01:00
|
|
|
import ms from 'ms';
|
2022-06-14 11:01:23 +02:00
|
|
|
import { IsNull } from 'typeorm';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2022-09-20 22:33:11 +02:00
|
|
|
import type { PasswordResetRequestsRepository, UserProfilesRepository, UsersRepository } from '@/models/index.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { Endpoint } from '@/server/api/endpoint-base.js';
|
|
|
|
import { IdService } from '@/core/IdService.js';
|
2022-09-20 22:33:11 +02:00
|
|
|
import type { Config } from '@/config.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { DI } from '@/di-symbols.js';
|
|
|
|
import { EmailService } from '@/core/EmailService.js';
|
2021-05-04 08:05:34 +02:00
|
|
|
|
|
|
|
export const meta = {
|
2022-06-10 07:25:20 +02:00
|
|
|
tags: ['reset password'],
|
|
|
|
|
2022-01-18 14:27:10 +01:00
|
|
|
requireCredential: false,
|
2021-05-04 08:05:34 +02:00
|
|
|
|
2022-06-10 07:25:20 +02:00
|
|
|
description: 'Request a users password to be reset.',
|
|
|
|
|
2021-05-04 08:05:34 +02:00
|
|
|
limit: {
|
|
|
|
duration: ms('1hour'),
|
2021-12-09 15:58:30 +01:00
|
|
|
max: 3,
|
2021-05-04 08:05:34 +02:00
|
|
|
},
|
|
|
|
|
2022-02-19 06:05:32 +01:00
|
|
|
errors: {
|
2021-05-04 08:05:34 +02:00
|
|
|
|
|
|
|
},
|
2022-02-19 06:05:32 +01:00
|
|
|
} as const;
|
2021-05-04 08:05:34 +02:00
|
|
|
|
2022-02-20 05:15:40 +01:00
|
|
|
export const paramDef = {
|
2022-02-19 06:05:32 +01:00
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
username: { type: 'string' },
|
|
|
|
email: { type: 'string' },
|
2021-12-09 15:58:30 +01:00
|
|
|
},
|
2022-02-19 06:05:32 +01:00
|
|
|
required: ['username', 'email'],
|
2022-01-18 14:27:10 +01:00
|
|
|
} as const;
|
2021-05-04 08:05:34 +02:00
|
|
|
|
2022-01-02 18:12:50 +01:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2022-09-17 20:27:08 +02: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 08:05:34 +02:00
|
|
|
}
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|