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 bcrypt from 'bcryptjs';
|
|
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
|
import { Endpoint } from '@/server/api/endpoint-base.js';
|
2023-09-08 14:05:03 +09:00
|
|
|
import type { UserProfilesRepository } from '@/models/index.js';
|
2022-09-18 03:27:08 +09:00
|
|
|
import { DI } from '@/di-symbols.js';
|
2023-09-08 14:05:03 +09:00
|
|
|
import { WebAuthnService } from '@/core/WebAuthnService.js';
|
|
|
|
|
import { ApiError } from '@/server/api/error.js';
|
2019-07-03 07:18:07 -04:00
|
|
|
|
|
|
|
|
export const meta = {
|
2022-01-18 22:27:10 +09:00
|
|
|
requireCredential: true,
|
2019-07-03 07:18:07 -04:00
|
|
|
|
|
|
|
|
secure: true,
|
2023-09-08 14:05:03 +09:00
|
|
|
|
|
|
|
|
errors: {
|
|
|
|
|
userNotFound: {
|
|
|
|
|
message: 'User not found.',
|
|
|
|
|
code: 'USER_NOT_FOUND',
|
|
|
|
|
id: '652f899f-66d4-490e-993e-6606c8ec04c3',
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
incorrectPassword: {
|
|
|
|
|
message: 'Incorrect password.',
|
|
|
|
|
code: 'INCORRECT_PASSWORD',
|
|
|
|
|
id: '38769596-efe2-4faf-9bec-abbb3f2cd9ba',
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
twoFactorNotEnabled: {
|
|
|
|
|
message: '2fa not enabled.',
|
|
|
|
|
code: 'TWO_FACTOR_NOT_ENABLED',
|
|
|
|
|
id: 'bf32b864-449b-47b8-974e-f9a5468546f1',
|
|
|
|
|
},
|
|
|
|
|
},
|
2022-02-19 14:05:32 +09:00
|
|
|
} as const;
|
2019-07-03 07:18:07 -04:00
|
|
|
|
2022-02-20 13:15:40 +09:00
|
|
|
export const paramDef = {
|
2022-02-19 14:05:32 +09:00
|
|
|
type: 'object',
|
|
|
|
|
properties: {
|
|
|
|
|
password: { type: 'string' },
|
2021-12-09 23:58:30 +09:00
|
|
|
},
|
2022-02-19 14:05:32 +09:00
|
|
|
required: ['password'],
|
2022-01-18 22:27:10 +09:00
|
|
|
} as const;
|
2019-07-03 07:18:07 -04:00
|
|
|
|
2023-09-08 14:05:03 +09:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2022-09-18 03:27:08 +09:00
|
|
|
@Injectable()
|
2023-09-08 14:05:03 +09:00
|
|
|
export default class extends Endpoint<typeof meta, typeof paramDef> {
|
2022-09-18 03:27:08 +09:00
|
|
|
constructor(
|
|
|
|
|
@Inject(DI.userProfilesRepository)
|
|
|
|
|
private userProfilesRepository: UserProfilesRepository,
|
2019-07-03 07:18:07 -04:00
|
|
|
|
2023-09-08 14:05:03 +09:00
|
|
|
private webAuthnService: WebAuthnService,
|
2022-09-18 03:27:08 +09:00
|
|
|
) {
|
|
|
|
|
super(meta, paramDef, async (ps, me) => {
|
2023-09-08 14:05:03 +09:00
|
|
|
const profile = await this.userProfilesRepository.findOne({
|
|
|
|
|
where: {
|
|
|
|
|
userId: me.id,
|
|
|
|
|
},
|
|
|
|
|
relations: ['user'],
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (profile == null) {
|
|
|
|
|
throw new ApiError(meta.errors.userNotFound);
|
|
|
|
|
}
|
2019-07-03 07:18:07 -04:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
// Compare password
|
2023-09-08 14:05:03 +09:00
|
|
|
const same = await bcrypt.compare(ps.password, profile.password ?? '');
|
2022-09-18 03:27:08 +09:00
|
|
|
|
|
|
|
|
if (!same) {
|
2023-09-08 14:05:03 +09:00
|
|
|
throw new ApiError(meta.errors.incorrectPassword);
|
2022-09-18 03:27:08 +09:00
|
|
|
}
|
2019-07-03 07:18:07 -04:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
if (!profile.twoFactorEnabled) {
|
2023-09-08 14:05:03 +09:00
|
|
|
throw new ApiError(meta.errors.twoFactorNotEnabled);
|
2022-09-18 03:27:08 +09:00
|
|
|
}
|
|
|
|
|
|
2023-09-08 14:05:03 +09:00
|
|
|
return await this.webAuthnService.initiateRegistration(
|
|
|
|
|
me.id,
|
|
|
|
|
profile.user?.username ?? me.id,
|
|
|
|
|
profile.user?.name ?? undefined,
|
|
|
|
|
);
|
2022-09-18 03:27:08 +09:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|