2023-07-27 05:31:52 +00:00
|
|
|
/*
|
2024-02-13 15:59:27 +00:00
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 05:31:52 +00:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2023-09-21 22:21:57 +00:00
|
|
|
//import bcrypt from 'bcryptjs';
|
|
|
|
import * as argon2 from 'argon2';
|
2022-09-17 18:27:08 +00:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import { Endpoint } from '@/server/api/endpoint-base.js';
|
2023-09-15 05:28:29 +00:00
|
|
|
import type { UserProfilesRepository } from '@/models/_.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
import { DI } from '@/di-symbols.js';
|
2023-09-08 05:05:03 +00:00
|
|
|
import { WebAuthnService } from '@/core/WebAuthnService.js';
|
|
|
|
import { ApiError } from '@/server/api/error.js';
|
2023-09-22 05:12:33 +00:00
|
|
|
import { UserAuthService } from '@/core/UserAuthService.js';
|
2019-07-03 11:18:07 +00:00
|
|
|
|
|
|
|
export const meta = {
|
2022-01-18 13:27:10 +00:00
|
|
|
requireCredential: true,
|
2019-07-03 11:18:07 +00:00
|
|
|
|
|
|
|
secure: true,
|
2023-09-08 05:05:03 +00: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',
|
|
|
|
},
|
|
|
|
},
|
2023-12-21 07:57:05 +00:00
|
|
|
|
|
|
|
res: {
|
|
|
|
type: 'object',
|
|
|
|
nullable: false,
|
|
|
|
optional: false,
|
|
|
|
properties: {
|
|
|
|
rp: {
|
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
id: {
|
|
|
|
type: 'string',
|
2024-02-16 05:27:33 +00:00
|
|
|
optional: true,
|
2023-12-21 07:57:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
user: {
|
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
id: {
|
|
|
|
type: 'string',
|
|
|
|
},
|
|
|
|
name: {
|
|
|
|
type: 'string',
|
|
|
|
},
|
|
|
|
displayName: {
|
|
|
|
type: 'string',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
challenge: {
|
|
|
|
type: 'string',
|
|
|
|
},
|
|
|
|
pubKeyCredParams: {
|
|
|
|
type: 'array',
|
|
|
|
items: {
|
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
type: {
|
|
|
|
type: 'string',
|
|
|
|
},
|
|
|
|
alg: {
|
|
|
|
type: 'number',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
timeout: {
|
|
|
|
type: 'number',
|
|
|
|
nullable: true,
|
|
|
|
},
|
|
|
|
excludeCredentials: {
|
|
|
|
type: 'array',
|
|
|
|
nullable: true,
|
|
|
|
items: {
|
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
id: {
|
|
|
|
type: 'string',
|
|
|
|
},
|
|
|
|
type: {
|
|
|
|
type: 'string',
|
|
|
|
},
|
|
|
|
transports: {
|
|
|
|
type: 'array',
|
|
|
|
items: {
|
|
|
|
type: 'string',
|
|
|
|
enum: [
|
2024-01-13 07:54:25 +00:00
|
|
|
'ble',
|
|
|
|
'cable',
|
|
|
|
'hybrid',
|
|
|
|
'internal',
|
|
|
|
'nfc',
|
|
|
|
'smart-card',
|
|
|
|
'usb',
|
2023-12-21 07:57:05 +00:00
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
authenticatorSelection: {
|
|
|
|
type: 'object',
|
|
|
|
nullable: true,
|
|
|
|
properties: {
|
|
|
|
authenticatorAttachment: {
|
|
|
|
type: 'string',
|
|
|
|
enum: [
|
2024-01-13 07:54:25 +00:00
|
|
|
'cross-platform',
|
|
|
|
'platform',
|
2023-12-21 07:57:05 +00:00
|
|
|
],
|
|
|
|
},
|
|
|
|
requireResidentKey: {
|
|
|
|
type: 'boolean',
|
|
|
|
},
|
|
|
|
userVerification: {
|
|
|
|
type: 'string',
|
|
|
|
enum: [
|
2024-01-13 07:54:25 +00:00
|
|
|
'discouraged',
|
|
|
|
'preferred',
|
|
|
|
'required',
|
2023-12-21 07:57:05 +00:00
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
attestation: {
|
|
|
|
type: 'string',
|
|
|
|
nullable: true,
|
|
|
|
enum: [
|
2024-01-13 07:54:25 +00:00
|
|
|
'direct',
|
|
|
|
'enterprise',
|
|
|
|
'indirect',
|
|
|
|
'none',
|
|
|
|
null,
|
2023-12-21 07:57:05 +00:00
|
|
|
],
|
|
|
|
},
|
|
|
|
extensions: {
|
|
|
|
type: 'object',
|
|
|
|
nullable: true,
|
|
|
|
properties: {
|
|
|
|
appid: {
|
|
|
|
type: 'string',
|
|
|
|
nullable: true,
|
|
|
|
},
|
|
|
|
credProps: {
|
|
|
|
type: 'boolean',
|
|
|
|
nullable: true,
|
|
|
|
},
|
|
|
|
hmacCreateSecret: {
|
|
|
|
type: 'boolean',
|
|
|
|
nullable: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-02-19 05:05:32 +00:00
|
|
|
} as const;
|
2019-07-03 11:18:07 +00:00
|
|
|
|
2022-02-20 04:15:40 +00:00
|
|
|
export const paramDef = {
|
2022-02-19 05:05:32 +00:00
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
password: { type: 'string' },
|
2023-09-22 05:12:33 +00:00
|
|
|
token: { type: 'string', nullable: true },
|
2021-12-09 14:58:30 +00:00
|
|
|
},
|
2022-02-19 05:05:32 +00:00
|
|
|
required: ['password'],
|
2022-01-18 13:27:10 +00:00
|
|
|
} as const;
|
2019-07-03 11:18:07 +00:00
|
|
|
|
2023-09-08 05:05:03 +00:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2022-09-17 18:27:08 +00:00
|
|
|
@Injectable()
|
2023-09-08 05:05:03 +00:00
|
|
|
export default class extends Endpoint<typeof meta, typeof paramDef> {
|
2022-09-17 18:27:08 +00:00
|
|
|
constructor(
|
|
|
|
@Inject(DI.userProfilesRepository)
|
|
|
|
private userProfilesRepository: UserProfilesRepository,
|
2019-07-03 11:18:07 +00:00
|
|
|
|
2023-09-08 05:05:03 +00:00
|
|
|
private webAuthnService: WebAuthnService,
|
2023-09-22 05:12:33 +00:00
|
|
|
private userAuthService: UserAuthService,
|
2022-09-17 18:27:08 +00:00
|
|
|
) {
|
|
|
|
super(meta, paramDef, async (ps, me) => {
|
2023-09-22 05:12:33 +00:00
|
|
|
const token = ps.token;
|
2023-09-08 05:05:03 +00: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 11:18:07 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
// Compare password
|
2023-09-22 05:12:33 +00:00
|
|
|
if (profile.twoFactorEnabled) {
|
|
|
|
if (token == null) {
|
|
|
|
throw new Error('authentication failed');
|
|
|
|
}
|
2022-09-17 18:27:08 +00:00
|
|
|
|
2023-09-22 05:12:33 +00:00
|
|
|
try {
|
|
|
|
await this.userAuthService.twoFactorAuthenticate(profile, token);
|
|
|
|
} catch (e) {
|
|
|
|
throw new Error('authentication failed');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-26 00:34:42 +00:00
|
|
|
const passwordMatched = await argon2.verify(profile.password ?? '', ps.password);
|
2023-09-22 05:12:33 +00:00
|
|
|
if (!passwordMatched) {
|
2023-09-08 05:05:03 +00:00
|
|
|
throw new ApiError(meta.errors.incorrectPassword);
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
2019-07-03 11:18:07 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
if (!profile.twoFactorEnabled) {
|
2023-09-08 05:05:03 +00:00
|
|
|
throw new ApiError(meta.errors.twoFactorNotEnabled);
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
|
|
|
|
2023-09-08 05:05:03 +00:00
|
|
|
return await this.webAuthnService.initiateRegistration(
|
|
|
|
me.id,
|
|
|
|
profile.user?.username ?? me.id,
|
|
|
|
profile.user?.name ?? undefined,
|
|
|
|
);
|
2022-09-17 18:27:08 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|