2023-07-27 14:31:52 +09:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-02-27 11:07:39 +09:00
|
|
|
import bcrypt from 'bcryptjs';
|
2022-09-18 03:27:08 +09:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2023-09-15 14:28:29 +09:00
|
|
|
import type { UserProfilesRepository, PasswordResetRequestsRepository } from '@/models/_.js';
|
2022-09-18 03:27:08 +09:00
|
|
|
import { Endpoint } from '@/server/api/endpoint-base.js';
|
|
|
|
import { DI } from '@/di-symbols.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: 'Complete the password reset that was previously requested.',
|
|
|
|
|
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: {
|
|
|
|
token: { type: 'string' },
|
|
|
|
password: { type: 'string' },
|
2021-12-09 23:58:30 +09:00
|
|
|
},
|
2022-02-19 14:05:32 +09:00
|
|
|
required: ['token', 'password'],
|
2022-01-18 22:27:10 +09:00
|
|
|
} as const;
|
2021-05-04 15:05:34 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
@Injectable()
|
2023-08-17 21:20:58 +09:00
|
|
|
export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-disable-line import/no-default-export
|
2022-09-18 03:27:08 +09:00
|
|
|
constructor(
|
|
|
|
@Inject(DI.passwordResetRequestsRepository)
|
|
|
|
private passwordResetRequestsRepository: PasswordResetRequestsRepository,
|
|
|
|
|
|
|
|
@Inject(DI.userProfilesRepository)
|
|
|
|
private userProfilesRepository: UserProfilesRepository,
|
|
|
|
) {
|
|
|
|
super(meta, paramDef, async (ps, me) => {
|
|
|
|
const req = await this.passwordResetRequestsRepository.findOneByOrFail({
|
|
|
|
token: ps.token,
|
|
|
|
});
|
|
|
|
|
|
|
|
// 発行してから30分以上経過していたら無効
|
|
|
|
if (Date.now() - req.createdAt.getTime() > 1000 * 60 * 30) {
|
|
|
|
throw new Error(); // TODO
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate hash of password
|
|
|
|
const salt = await bcrypt.genSalt(8);
|
|
|
|
const hash = await bcrypt.hash(ps.password, salt);
|
|
|
|
|
|
|
|
await this.userProfilesRepository.update(req.userId, {
|
|
|
|
password: hash,
|
|
|
|
});
|
|
|
|
|
|
|
|
this.passwordResetRequestsRepository.delete(req.id);
|
|
|
|
});
|
2021-05-04 15:05:34 +09:00
|
|
|
}
|
2022-09-18 03:27:08 +09:00
|
|
|
}
|