2023-07-27 14:31:52 +09:00
|
|
|
/*
|
2024-02-13 15:59:27 +00:00
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 14:31:52 +09:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
*/
|
|
|
|
|
|
2023-09-22 00:21:57 +02:00
|
|
|
//import bcrypt from 'bcryptjs';
|
|
|
|
|
import * as argon2 from 'argon2';
|
2022-09-18 03:27:08 +09:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
|
import { Endpoint } from '@/server/api/endpoint-base.js';
|
2023-09-15 14:28:29 +09:00
|
|
|
import type { UserProfilesRepository, UserSecurityKeysRepository } from '@/models/_.js';
|
2022-09-18 03:27:08 +09:00
|
|
|
import { UserEntityService } from '@/core/entities/UserEntityService.js';
|
|
|
|
|
import { GlobalEventService } from '@/core/GlobalEventService.js';
|
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2023-09-08 14:05:03 +09:00
|
|
|
import { ApiError } from '@/server/api/error.js';
|
2023-09-22 14:12:33 +09:00
|
|
|
import { UserAuthService } from '@/core/UserAuthService.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: {
|
|
|
|
|
incorrectPassword: {
|
|
|
|
|
message: 'Incorrect password.',
|
|
|
|
|
code: 'INCORRECT_PASSWORD',
|
|
|
|
|
id: '141c598d-a825-44c8-9173-cfb9d92be493',
|
|
|
|
|
},
|
|
|
|
|
},
|
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' },
|
2023-09-22 14:12:33 +09:00
|
|
|
token: { type: 'string', nullable: true },
|
2022-02-19 14:05:32 +09:00
|
|
|
credentialId: { type: 'string' },
|
2021-12-09 23:58:30 +09:00
|
|
|
},
|
2022-02-19 14:05:32 +09:00
|
|
|
required: ['password', 'credentialId'],
|
2022-01-18 22:27:10 +09:00
|
|
|
} as const;
|
2019-07-03 07:18:07 -04: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.userSecurityKeysRepository)
|
|
|
|
|
private userSecurityKeysRepository: UserSecurityKeysRepository,
|
|
|
|
|
|
|
|
|
|
@Inject(DI.userProfilesRepository)
|
|
|
|
|
private userProfilesRepository: UserProfilesRepository,
|
|
|
|
|
|
|
|
|
|
private userEntityService: UserEntityService,
|
2023-09-22 14:12:33 +09:00
|
|
|
private userAuthService: UserAuthService,
|
2022-09-18 03:27:08 +09:00
|
|
|
private globalEventService: GlobalEventService,
|
|
|
|
|
) {
|
|
|
|
|
super(meta, paramDef, async (ps, me) => {
|
2023-09-22 14:12:33 +09:00
|
|
|
const token = ps.token;
|
2022-09-18 03:27:08 +09:00
|
|
|
const profile = await this.userProfilesRepository.findOneByOrFail({ userId: me.id });
|
|
|
|
|
|
|
|
|
|
// Compare password
|
2023-09-22 14:12:33 +09:00
|
|
|
if (profile.twoFactorEnabled) {
|
|
|
|
|
if (token == null) {
|
|
|
|
|
throw new Error('authentication failed');
|
|
|
|
|
}
|
2022-09-18 03:27:08 +09:00
|
|
|
|
2023-09-22 14:12:33 +09:00
|
|
|
try {
|
|
|
|
|
await this.userAuthService.twoFactorAuthenticate(profile, token);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
throw new Error('authentication failed');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-26 02:26:30 +02:00
|
|
|
const passwordMatched = await argon2.verify(profile.password ?? '', ps.password);
|
2023-09-22 14:12:33 +09:00
|
|
|
if (!passwordMatched) {
|
2023-09-08 14:05:03 +09:00
|
|
|
throw new ApiError(meta.errors.incorrectPassword);
|
2022-09-18 03:27:08 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Make sure we only delete the user's own creds
|
|
|
|
|
await this.userSecurityKeysRepository.delete({
|
|
|
|
|
userId: me.id,
|
|
|
|
|
id: ps.credentialId,
|
|
|
|
|
});
|
|
|
|
|
|
2023-02-20 16:40:24 +09:00
|
|
|
// 使われているキーがなくなったらパスワードレスログインをやめる
|
|
|
|
|
const keyCount = await this.userSecurityKeysRepository.count({
|
|
|
|
|
where: {
|
|
|
|
|
userId: me.id,
|
|
|
|
|
},
|
|
|
|
|
select: {
|
|
|
|
|
id: true,
|
|
|
|
|
name: true,
|
|
|
|
|
lastUsed: true,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (keyCount === 0) {
|
|
|
|
|
await this.userProfilesRepository.update(me.id, {
|
|
|
|
|
usePasswordLessLogin: false,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
// Publish meUpdated event
|
|
|
|
|
this.globalEventService.publishMainStream(me.id, 'meUpdated', await this.userEntityService.pack(me.id, me, {
|
2024-01-31 15:45:35 +09:00
|
|
|
schema: 'MeDetailed',
|
2022-09-18 03:27:08 +09:00
|
|
|
includeSecrets: true,
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
return {};
|
|
|
|
|
});
|
2019-07-03 07:18:07 -04:00
|
|
|
}
|
2022-09-18 03:27:08 +09:00
|
|
|
}
|