2023-07-27 05:31:52 +00:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import bcrypt from 'bcryptjs';
|
2023-02-22 05:53:36 +00:00
|
|
|
import * as OTPAuth from 'otpauth';
|
2022-09-17 18:27:08 +00:00
|
|
|
import { IsNull } from 'typeorm';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2023-09-08 05:05:03 +00:00
|
|
|
import type {
|
|
|
|
SigninsRepository,
|
|
|
|
UserProfilesRepository,
|
|
|
|
UsersRepository,
|
2023-09-15 05:28:29 +00:00
|
|
|
} from '@/models/_.js';
|
2022-09-20 20:33:11 +00:00
|
|
|
import type { Config } from '@/config.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
import { getIpHash } from '@/misc/get-ip-hash.js';
|
2023-08-16 08:51:28 +00:00
|
|
|
import type { MiLocalUser } from '@/models/entities/User.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
import { IdService } from '@/core/IdService.js';
|
2023-02-09 01:46:01 +00:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2023-09-08 05:05:03 +00:00
|
|
|
import { WebAuthnService } from '@/core/WebAuthnService.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
import { RateLimiterService } from './RateLimiterService.js';
|
|
|
|
import { SigninService } from './SigninService.js';
|
2023-09-08 05:05:03 +00:00
|
|
|
import type { AuthenticationResponseJSON } from '@simplewebauthn/typescript-types';
|
|
|
|
import type { FastifyReply, FastifyRequest } from 'fastify';
|
2022-09-17 18:27:08 +00:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class SigninApiService {
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.config)
|
|
|
|
private config: Config,
|
|
|
|
|
|
|
|
@Inject(DI.usersRepository)
|
|
|
|
private usersRepository: UsersRepository,
|
|
|
|
|
|
|
|
@Inject(DI.userProfilesRepository)
|
|
|
|
private userProfilesRepository: UserProfilesRepository,
|
|
|
|
|
|
|
|
@Inject(DI.signinsRepository)
|
|
|
|
private signinsRepository: SigninsRepository,
|
|
|
|
|
|
|
|
private idService: IdService,
|
|
|
|
private rateLimiterService: RateLimiterService,
|
|
|
|
private signinService: SigninService,
|
2023-09-08 05:05:03 +00:00
|
|
|
private webAuthnService: WebAuthnService,
|
2022-09-17 18:27:08 +00:00
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2022-12-03 10:42:05 +00:00
|
|
|
public async signin(
|
|
|
|
request: FastifyRequest<{
|
|
|
|
Body: {
|
|
|
|
username: string;
|
|
|
|
password: string;
|
|
|
|
token?: string;
|
2023-09-08 05:05:03 +00:00
|
|
|
credential?: AuthenticationResponseJSON;
|
2022-12-03 10:42:05 +00:00
|
|
|
};
|
|
|
|
}>,
|
|
|
|
reply: FastifyReply,
|
|
|
|
) {
|
|
|
|
reply.header('Access-Control-Allow-Origin', this.config.url);
|
|
|
|
reply.header('Access-Control-Allow-Credentials', 'true');
|
2022-09-17 18:27:08 +00:00
|
|
|
|
2022-12-03 10:42:05 +00:00
|
|
|
const body = request.body;
|
2022-09-17 18:27:08 +00:00
|
|
|
const username = body['username'];
|
|
|
|
const password = body['password'];
|
|
|
|
const token = body['token'];
|
|
|
|
|
|
|
|
function error(status: number, error: { id: string }) {
|
2022-12-03 10:42:05 +00:00
|
|
|
reply.code(status);
|
|
|
|
return { error };
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
// not more than 1 attempt per second and not more than 10 attempts per hour
|
2022-12-03 10:42:05 +00:00
|
|
|
await this.rateLimiterService.limit({ key: 'signin', duration: 60 * 60 * 1000, max: 10, minInterval: 1000 }, getIpHash(request.ip));
|
2022-09-17 18:27:08 +00:00
|
|
|
} catch (err) {
|
2022-12-03 10:42:05 +00:00
|
|
|
reply.code(429);
|
|
|
|
return {
|
2022-09-17 18:27:08 +00:00
|
|
|
error: {
|
|
|
|
message: 'Too many failed attempts to sign in. Try again later.',
|
|
|
|
code: 'TOO_MANY_AUTHENTICATION_FAILURES',
|
|
|
|
id: '22d05606-fbcf-421a-a2db-b32610dcfd1b',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof username !== 'string') {
|
2022-12-03 10:42:05 +00:00
|
|
|
reply.code(400);
|
2022-09-17 18:27:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof password !== 'string') {
|
2022-12-03 10:42:05 +00:00
|
|
|
reply.code(400);
|
2022-09-17 18:27:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (token != null && typeof token !== 'string') {
|
2022-12-03 10:42:05 +00:00
|
|
|
reply.code(400);
|
2022-09-17 18:27:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch user
|
|
|
|
const user = await this.usersRepository.findOneBy({
|
|
|
|
usernameLower: username.toLowerCase(),
|
|
|
|
host: IsNull(),
|
2023-08-16 08:51:28 +00:00
|
|
|
}) as MiLocalUser;
|
2022-09-17 18:27:08 +00:00
|
|
|
|
|
|
|
if (user == null) {
|
2022-12-03 10:42:05 +00:00
|
|
|
return error(404, {
|
2022-09-17 18:27:08 +00:00
|
|
|
id: '6cc579cc-885d-43d8-95c2-b8c7fc963280',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (user.isSuspended) {
|
2022-12-03 10:42:05 +00:00
|
|
|
return error(403, {
|
2022-09-17 18:27:08 +00:00
|
|
|
id: 'e03a5f46-d309-4865-9b69-56282d94e1eb',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const profile = await this.userProfilesRepository.findOneByOrFail({ userId: user.id });
|
|
|
|
|
|
|
|
// Compare password
|
|
|
|
const same = await bcrypt.compare(password, profile.password!);
|
|
|
|
|
|
|
|
const fail = async (status?: number, failure?: { id: string }) => {
|
|
|
|
// Append signin history
|
|
|
|
await this.signinsRepository.insert({
|
|
|
|
id: this.idService.genId(),
|
|
|
|
createdAt: new Date(),
|
|
|
|
userId: user.id,
|
2022-12-03 10:42:05 +00:00
|
|
|
ip: request.ip,
|
2023-02-09 01:46:01 +00:00
|
|
|
headers: request.headers as any,
|
2022-09-17 18:27:08 +00:00
|
|
|
success: false,
|
|
|
|
});
|
|
|
|
|
2022-12-03 10:42:05 +00:00
|
|
|
return error(status ?? 500, failure ?? { id: '4e30e80c-e338-45a0-8c8f-44455efa3b76' });
|
2022-09-17 18:27:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if (!profile.twoFactorEnabled) {
|
|
|
|
if (same) {
|
2022-12-03 10:42:05 +00:00
|
|
|
return this.signinService.signin(request, reply, user);
|
2022-09-17 18:27:08 +00:00
|
|
|
} else {
|
2022-12-03 10:42:05 +00:00
|
|
|
return await fail(403, {
|
2022-09-17 18:27:08 +00:00
|
|
|
id: '932c904e-9460-45b7-9ce6-7ed33be7eb2c',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (token) {
|
|
|
|
if (!same) {
|
2022-12-03 10:42:05 +00:00
|
|
|
return await fail(403, {
|
2022-09-17 18:27:08 +00:00
|
|
|
id: '932c904e-9460-45b7-9ce6-7ed33be7eb2c',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-08-28 09:25:31 +00:00
|
|
|
if (profile.twoFactorBackupSecret?.includes(token)) {
|
|
|
|
await this.userProfilesRepository.update({ userId: profile.userId }, {
|
|
|
|
twoFactorBackupSecret: profile.twoFactorBackupSecret.filter((secret) => secret !== token),
|
|
|
|
});
|
|
|
|
return this.signinService.signin(request, reply, user);
|
|
|
|
}
|
|
|
|
|
2023-02-20 07:40:24 +00:00
|
|
|
const delta = OTPAuth.TOTP.validate({
|
2023-02-22 05:53:36 +00:00
|
|
|
secret: OTPAuth.Secret.fromBase32(profile.twoFactorSecret!),
|
2023-02-20 07:40:24 +00:00
|
|
|
digits: 6,
|
|
|
|
token,
|
|
|
|
window: 1,
|
2022-09-17 18:27:08 +00:00
|
|
|
});
|
|
|
|
|
2023-02-20 07:40:24 +00:00
|
|
|
if (delta === null) {
|
2022-12-03 10:42:05 +00:00
|
|
|
return await fail(403, {
|
2022-09-17 18:27:08 +00:00
|
|
|
id: 'cdf1235b-ac71-46d4-a3a6-84ccce48df6f',
|
|
|
|
});
|
2023-02-20 07:40:24 +00:00
|
|
|
} else {
|
|
|
|
return this.signinService.signin(request, reply, user);
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
2023-09-08 05:05:03 +00:00
|
|
|
} else if (body.credential) {
|
2022-09-17 18:27:08 +00:00
|
|
|
if (!same && !profile.usePasswordLessLogin) {
|
2022-12-03 10:42:05 +00:00
|
|
|
return await fail(403, {
|
2022-09-17 18:27:08 +00:00
|
|
|
id: '932c904e-9460-45b7-9ce6-7ed33be7eb2c',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-09-08 05:05:03 +00:00
|
|
|
const authorized = await this.webAuthnService.verifyAuthentication(user.id, body.credential);
|
2022-09-17 18:27:08 +00:00
|
|
|
|
2023-09-08 05:05:03 +00:00
|
|
|
if (authorized) {
|
2022-12-03 10:42:05 +00:00
|
|
|
return this.signinService.signin(request, reply, user);
|
2022-09-17 18:27:08 +00:00
|
|
|
} else {
|
2022-12-03 10:42:05 +00:00
|
|
|
return await fail(403, {
|
2022-09-17 18:27:08 +00:00
|
|
|
id: '93b86c4b-72f9-40eb-9815-798928603d1e',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!same && !profile.usePasswordLessLogin) {
|
2022-12-03 10:42:05 +00:00
|
|
|
return await fail(403, {
|
2022-09-17 18:27:08 +00:00
|
|
|
id: '932c904e-9460-45b7-9ce6-7ed33be7eb2c',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-09-08 05:05:03 +00:00
|
|
|
const authRequest = await this.webAuthnService.initiateAuthentication(user.id);
|
2022-09-17 18:27:08 +00:00
|
|
|
|
2022-12-03 10:42:05 +00:00
|
|
|
reply.code(200);
|
2023-09-08 05:05:03 +00:00
|
|
|
return authRequest;
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
|
|
|
// never get here
|
|
|
|
}
|
|
|
|
}
|