server: refactor errors in signin endpoint

This commit is contained in:
Johann150 2022-11-08 19:40:58 +01:00
parent 7939d130aa
commit 609312bb82
Signed by untrusted user: Johann150
GPG key ID: 9EE6577A2A06F8F1

View file

@ -11,48 +11,51 @@ import { getIpHash } from '@/misc/get-ip-hash.js';
import signin from '../common/signin.js'; import signin from '../common/signin.js';
import { verifyLogin, hash } from '../2fa.js'; import { verifyLogin, hash } from '../2fa.js';
import { limiter } from '../limiter.js'; import { limiter } from '../limiter.js';
import { ApiError } from '../error.js';
export default async (ctx: Koa.Context) => { export default async (ctx: Koa.Context) => {
ctx.set('Access-Control-Allow-Origin', config.url); ctx.set('Access-Control-Allow-Origin', config.url);
ctx.set('Access-Control-Allow-Credentials', 'true'); ctx.set('Access-Control-Allow-Credentials', 'true');
const body = ctx.request.body as any; const body = ctx.request.body as any;
const username = body['username']; const { username, password, token } = body;
const password = body['password'];
const token = body['token'];
function error(status: number, error: { id: string }) { // taken from @server/api/api-handler.ts
ctx.status = status; function error (e: ApiError): void {
ctx.body = { error }; ctx.status = e.httpStatusCode;
if (e.httpStatusCode === 401) {
ctx.response.set('WWW-Authenticate', 'Bearer');
}
ctx.body = {
error: {
message: e!.message,
code: e!.code,
...(e!.info ? { info: e!.info } : {}),
endpoint: endpoint.name,
},
};
} }
try { try {
// not more than 1 attempt per second and not more than 10 attempts per hour // not more than 1 attempt per second and not more than 10 attempts per hour
await limiter({ key: 'signin', duration: 60 * 60 * 1000, max: 10, minInterval: 1000 }, getIpHash(ctx.ip)); await limiter({ key: 'signin', duration: 60 * 60 * 1000, max: 10, minInterval: 1000 }, getIpHash(ctx.ip));
} catch (err) { } catch (err) {
ctx.status = 429; error(new ApiError('RATE_LIMIT_EXCEEDED'));
ctx.body = {
error: {
message: 'Too many failed attempts to sign in. Try again later.',
code: 'TOO_MANY_AUTHENTICATION_FAILURES',
id: '22d05606-fbcf-421a-a2db-b32610dcfd1b',
},
};
return; return;
} }
if (typeof username !== 'string') { if (typeof username !== 'string') {
ctx.status = 400; error(new ApiError('INVALID_PARAM', { param: 'username', reason: 'not a string' }));
return; return;
} }
if (typeof password !== 'string') { if (typeof password !== 'string') {
ctx.status = 400; error(new ApiError('INVALID_PARAM', { param: 'password', reason: 'not a string' }));
return; return;
} }
if (token != null && typeof token !== 'string') { if (token != null && typeof token !== 'string') {
ctx.status = 400; error(new ApiError('INVALID_PARAM', { param: 'token', reason: 'provided but not a string' }));
return; return;
} }
@ -63,16 +66,12 @@ export default async (ctx: Koa.Context) => {
}) as ILocalUser; }) as ILocalUser;
if (user == null) { if (user == null) {
error(404, { error(new ApiError('NO_SUCH_USER'));
id: '6cc579cc-885d-43d8-95c2-b8c7fc963280',
});
return; return;
} }
if (user.isSuspended) { if (user.isSuspended) {
error(403, { error(new ApiError('SUSPENDED'));
id: 'e03a5f46-d309-4865-9b69-56282d94e1eb',
});
return; return;
} }
@ -81,7 +80,7 @@ export default async (ctx: Koa.Context) => {
// Compare password // Compare password
const same = await bcrypt.compare(password, profile.password!); const same = await bcrypt.compare(password, profile.password!);
async function fail(status?: number, failure?: { id: string }) { async function fail(): void {
// Append signin history // Append signin history
await Signins.insert({ await Signins.insert({
id: genId(), id: genId(),
@ -92,7 +91,7 @@ export default async (ctx: Koa.Context) => {
success: false, success: false,
}); });
error(status || 500, failure || { id: '4e30e80c-e338-45a0-8c8f-44455efa3b76' }); error(new ApiError('ACCESS_DENIED'));
} }
if (!profile.twoFactorEnabled) { if (!profile.twoFactorEnabled) {
@ -100,18 +99,14 @@ export default async (ctx: Koa.Context) => {
signin(ctx, user); signin(ctx, user);
return; return;
} else { } else {
await fail(403, { await fail();
id: '932c904e-9460-45b7-9ce6-7ed33be7eb2c',
});
return; return;
} }
} }
if (token) { if (token) {
if (!same) { if (!same) {
await fail(403, { await fail();
id: '932c904e-9460-45b7-9ce6-7ed33be7eb2c',
});
return; return;
} }
@ -126,16 +121,12 @@ export default async (ctx: Koa.Context) => {
signin(ctx, user); signin(ctx, user);
return; return;
} else { } else {
await fail(403, { await fail();
id: 'cdf1235b-ac71-46d4-a3a6-84ccce48df6f',
});
return; return;
} }
} else if (body.credentialId) { } else if (body.credentialId) {
if (!same && !profile.usePasswordLessLogin) { if (!same && !profile.usePasswordLessLogin) {
await fail(403, { await fail();
id: '932c904e-9460-45b7-9ce6-7ed33be7eb2c',
});
return; return;
} }
@ -149,9 +140,7 @@ export default async (ctx: Koa.Context) => {
}); });
if (!challenge) { if (!challenge) {
await fail(403, { await fail();
id: '2715a88a-2125-4013-932f-aa6fe72792da',
});
return; return;
} }
@ -161,9 +150,7 @@ export default async (ctx: Koa.Context) => {
}); });
if (new Date().getTime() - challenge.createdAt.getTime() >= 5 * 60 * 1000) { if (new Date().getTime() - challenge.createdAt.getTime() >= 5 * 60 * 1000) {
await fail(403, { await fail();
id: '2715a88a-2125-4013-932f-aa6fe72792da',
});
return; return;
} }
@ -177,9 +164,7 @@ export default async (ctx: Koa.Context) => {
}); });
if (!securityKey) { if (!securityKey) {
await fail(403, { await fail();
id: '66269679-aeaf-4474-862b-eb761197e046',
});
return; return;
} }
@ -196,16 +181,12 @@ export default async (ctx: Koa.Context) => {
signin(ctx, user); signin(ctx, user);
return; return;
} else { } else {
await fail(403, { await fail();
id: '93b86c4b-72f9-40eb-9815-798928603d1e',
});
return; return;
} }
} else { } else {
if (!same && !profile.usePasswordLessLogin) { if (!same && !profile.usePasswordLessLogin) {
await fail(403, { await fail();
id: '932c904e-9460-45b7-9ce6-7ed33be7eb2c',
});
return; return;
} }
@ -214,9 +195,7 @@ export default async (ctx: Koa.Context) => {
}); });
if (keys.length === 0) { if (keys.length === 0) {
await fail(403, { await fail();
id: 'f27fd449-9af4-4841-9249-1f989b9fa4a4',
});
return; return;
} }