2023-07-27 00:31:52 -05:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-09-17 13:27:08 -05:00
|
|
|
import bcrypt from 'bcryptjs';
|
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import { Endpoint } from '@/server/api/endpoint-base.js';
|
|
|
|
import { UserEntityService } from '@/core/entities/UserEntityService.js';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
|
|
|
import { GlobalEventService } from '@/core/GlobalEventService.js';
|
2023-09-15 00:28:29 -05:00
|
|
|
import type { UserProfilesRepository, UserSecurityKeysRepository } from '@/models/_.js';
|
2023-09-08 00:05:03 -05:00
|
|
|
import { WebAuthnService } from '@/core/WebAuthnService.js';
|
|
|
|
import { ApiError } from '@/server/api/error.js';
|
2019-07-03 06:18:07 -05:00
|
|
|
|
|
|
|
export const meta = {
|
2022-01-18 07:27:10 -06:00
|
|
|
requireCredential: true,
|
2019-07-03 06:18:07 -05:00
|
|
|
|
|
|
|
secure: true,
|
2023-09-08 00:05:03 -05:00
|
|
|
|
|
|
|
errors: {
|
|
|
|
incorrectPassword: {
|
|
|
|
message: 'Incorrect password.',
|
|
|
|
code: 'INCORRECT_PASSWORD',
|
|
|
|
id: '0d7ec6d2-e652-443e-a7bf-9ee9a0cd77b0',
|
|
|
|
},
|
|
|
|
|
|
|
|
twoFactorNotEnabled: {
|
|
|
|
message: '2fa not enabled.',
|
|
|
|
code: 'TWO_FACTOR_NOT_ENABLED',
|
|
|
|
id: '798d6847-b1ed-4f9c-b1f9-163c42655995',
|
|
|
|
},
|
|
|
|
},
|
2022-02-18 23:05:32 -06:00
|
|
|
} as const;
|
2019-07-03 06:18:07 -05:00
|
|
|
|
2022-02-19 22:15:40 -06:00
|
|
|
export const paramDef = {
|
2022-02-18 23:05:32 -06:00
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
password: { type: 'string' },
|
2023-02-20 01:40:24 -06:00
|
|
|
name: { type: 'string', minLength: 1, maxLength: 30 },
|
2023-09-08 00:05:03 -05:00
|
|
|
credential: { type: 'object' },
|
2021-12-09 08:58:30 -06:00
|
|
|
},
|
2023-09-08 00:05:03 -05:00
|
|
|
required: ['password', 'name', 'credential'],
|
2022-01-18 07:27:10 -06:00
|
|
|
} as const;
|
2019-07-03 06:18:07 -05:00
|
|
|
|
2023-09-08 00:05:03 -05:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2022-09-17 13:27:08 -05:00
|
|
|
@Injectable()
|
2023-09-08 00:05:03 -05:00
|
|
|
export default class extends Endpoint<typeof meta, typeof paramDef> {
|
2022-09-17 13:27:08 -05:00
|
|
|
constructor(
|
|
|
|
@Inject(DI.userProfilesRepository)
|
|
|
|
private userProfilesRepository: UserProfilesRepository,
|
2019-07-03 06:18:07 -05:00
|
|
|
|
2022-09-17 13:27:08 -05:00
|
|
|
@Inject(DI.userSecurityKeysRepository)
|
|
|
|
private userSecurityKeysRepository: UserSecurityKeysRepository,
|
2019-07-03 06:18:07 -05:00
|
|
|
|
2023-09-08 00:05:03 -05:00
|
|
|
private webAuthnService: WebAuthnService,
|
2022-09-17 13:27:08 -05:00
|
|
|
private userEntityService: UserEntityService,
|
|
|
|
private globalEventService: GlobalEventService,
|
2019-07-03 06:18:07 -05:00
|
|
|
) {
|
2022-09-17 13:27:08 -05:00
|
|
|
super(meta, paramDef, async (ps, me) => {
|
|
|
|
const profile = await this.userProfilesRepository.findOneByOrFail({ userId: me.id });
|
|
|
|
|
|
|
|
// Compare password
|
2023-09-08 00:05:03 -05:00
|
|
|
const same = await bcrypt.compare(ps.password, profile.password ?? '');
|
2022-09-17 13:27:08 -05:00
|
|
|
|
|
|
|
if (!same) {
|
2023-09-08 00:05:03 -05:00
|
|
|
throw new ApiError(meta.errors.incorrectPassword);
|
2022-09-17 13:27:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!profile.twoFactorEnabled) {
|
2023-09-08 00:05:03 -05:00
|
|
|
throw new ApiError(meta.errors.twoFactorNotEnabled);
|
2022-09-17 13:27:08 -05:00
|
|
|
}
|
|
|
|
|
2023-09-08 00:05:03 -05:00
|
|
|
const keyInfo = await this.webAuthnService.verifyRegistration(me.id, ps.credential);
|
2022-09-17 13:27:08 -05:00
|
|
|
|
2023-09-08 00:05:03 -05:00
|
|
|
const credentialId = Buffer.from(keyInfo.credentialID).toString('base64url');
|
2022-09-17 13:27:08 -05:00
|
|
|
await this.userSecurityKeysRepository.insert({
|
2023-09-08 00:05:03 -05:00
|
|
|
id: credentialId,
|
2022-09-17 13:27:08 -05:00
|
|
|
userId: me.id,
|
|
|
|
name: ps.name,
|
2023-09-08 00:05:03 -05:00
|
|
|
publicKey: Buffer.from(keyInfo.credentialPublicKey).toString('base64url'),
|
|
|
|
counter: keyInfo.counter,
|
|
|
|
credentialDeviceType: keyInfo.credentialDeviceType,
|
|
|
|
credentialBackedUp: keyInfo.credentialBackedUp,
|
|
|
|
transports: keyInfo.transports,
|
2022-09-17 13:27:08 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
// Publish meUpdated event
|
|
|
|
this.globalEventService.publishMainStream(me.id, 'meUpdated', await this.userEntityService.pack(me.id, me, {
|
|
|
|
detail: true,
|
|
|
|
includeSecrets: true,
|
|
|
|
}));
|
|
|
|
|
|
|
|
return {
|
2023-09-08 00:05:03 -05:00
|
|
|
id: credentialId,
|
2022-09-17 13:27:08 -05:00
|
|
|
name: ps.name,
|
|
|
|
};
|
|
|
|
});
|
2019-07-03 06:18:07 -05:00
|
|
|
}
|
2022-09-17 13:27:08 -05:00
|
|
|
}
|