mirror of
https://github.com/bewcloud/bewcloud.git
synced 2026-03-11 08:54:49 +00:00
This implements a huge change, where Fresh is removed as a framework and serving files, allowing more control over importing, bundling, and serving files and components. The biggest challenge was to continue making sure that there weren't too many places to look into for import versions, and `PasswordlessPasskeyLogin.tsx` became a prototype in migrating a component to fully SSR, no need for frontend parsing (via Babel) or bundling (via a custom-script, downloading frontend dependencies from esm.sh). Still, there are too many components to migrate like that, and it's all working, so I likely won't even attempt it unless there's some bug, new feature, or security vulnerability to address that warrants a rewrite of those. This also updates all dependencies (except `@libs/xml` because that still causes some breaking in DAV endpoints), including Deno! All other advantages can be seen in the related issues, and the breaking change this (v4.0.0) introduces is related simply to `config.email.tlsMode` (which had a deprecation warning throughout v3), and because, while I tested many things exhaustively, it's not impossible something broke that I didn't see. Closes #141 Closes #132
111 lines
2.8 KiB
TypeScript
111 lines
2.8 KiB
TypeScript
import page, { RequestHandlerParams } from '/lib/page.ts';
|
|
|
|
import { PASSWORD_SALT } from '/lib/auth.ts';
|
|
import { generateHash } from '/public/ts/utils/misc.ts';
|
|
import { UserModel } from '/lib/models/user.ts';
|
|
import { getMultiFactorAuthMethodByIdFromUser } from '/public/ts/utils/multi-factor-auth.ts';
|
|
import { AppConfig } from '/lib/config.ts';
|
|
import { MultiFactorAuthModel } from '/lib/models/multi-factor-auth.ts';
|
|
|
|
export interface RequestBody {
|
|
methodId?: string;
|
|
password: string;
|
|
disableAll?: boolean;
|
|
}
|
|
|
|
export interface ResponseBody {
|
|
success: boolean;
|
|
error?: string;
|
|
}
|
|
|
|
async function post({ request, user }: RequestHandlerParams) {
|
|
const isMultiFactorAuthEnabled = await AppConfig.isMultiFactorAuthEnabled();
|
|
|
|
if (!isMultiFactorAuthEnabled) {
|
|
const responseBody: ResponseBody = {
|
|
success: false,
|
|
error: 'Multi-factor authentication is not enabled on this server',
|
|
};
|
|
|
|
return new Response(JSON.stringify(responseBody), { status: 403 });
|
|
}
|
|
|
|
const body = await request.clone().json() as RequestBody;
|
|
const { methodId, password, disableAll } = body;
|
|
|
|
if (!password) {
|
|
const responseBody: ResponseBody = {
|
|
success: false,
|
|
error: 'Password is required',
|
|
};
|
|
|
|
return new Response(JSON.stringify(responseBody), { status: 400 });
|
|
}
|
|
|
|
const hashedPassword = await generateHash(`${password}:${PASSWORD_SALT}`, 'SHA-256');
|
|
|
|
if (user!.hashed_password !== hashedPassword) {
|
|
const responseBody: ResponseBody = {
|
|
success: false,
|
|
error: 'Invalid password',
|
|
};
|
|
|
|
return new Response(JSON.stringify(responseBody), { status: 400 });
|
|
}
|
|
|
|
if (disableAll) {
|
|
user!.extra.multi_factor_auth_methods = [];
|
|
|
|
await UserModel.update(user!);
|
|
|
|
const responseBody: ResponseBody = {
|
|
success: true,
|
|
};
|
|
|
|
return new Response(JSON.stringify(responseBody));
|
|
}
|
|
|
|
if (!methodId) {
|
|
const responseBody: ResponseBody = {
|
|
success: false,
|
|
error: 'Method ID is required when not disabling all methods',
|
|
};
|
|
|
|
return new Response(JSON.stringify(responseBody), { status: 400 });
|
|
}
|
|
|
|
const method = getMultiFactorAuthMethodByIdFromUser(user!, methodId);
|
|
|
|
if (!method) {
|
|
const responseBody: ResponseBody = {
|
|
success: false,
|
|
error: 'Multi-factor authentication method not found',
|
|
};
|
|
|
|
return new Response(JSON.stringify(responseBody), { status: 404 });
|
|
}
|
|
|
|
if (!method.enabled) {
|
|
const responseBody: ResponseBody = {
|
|
success: false,
|
|
error: 'Multi-factor authentication method is not enabled',
|
|
};
|
|
|
|
return new Response(JSON.stringify(responseBody), { status: 400 });
|
|
}
|
|
|
|
MultiFactorAuthModel.disableMethodFromUser(user!, methodId);
|
|
|
|
await UserModel.update(user!);
|
|
|
|
const responseBody: ResponseBody = {
|
|
success: true,
|
|
};
|
|
|
|
return new Response(JSON.stringify(responseBody));
|
|
}
|
|
|
|
export default page({
|
|
post,
|
|
accessMode: 'user',
|
|
});
|