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
193 lines
5.8 KiB
TypeScript
193 lines
5.8 KiB
TypeScript
import { renderToString } from 'preact-render-to-string';
|
|
|
|
import page, { RequestHandlerParams } from '/lib/page.ts';
|
|
import { MultiFactorAuthMethodType } from '/lib/types.ts';
|
|
import { UserModel } from '/lib/models/user.ts';
|
|
import { createSessionResponse } from '/lib/auth.ts';
|
|
import { getFormDataField } from '/public/ts/utils/form.ts';
|
|
import { AppConfig } from '/lib/config.ts';
|
|
import { MultiFactorAuthModel } from '/lib/models/multi-factor-auth.ts';
|
|
import {
|
|
getEnabledMultiFactorAuthMethodsFromUser,
|
|
isMultiFactorAuthEnabledForUser,
|
|
} from '/public/ts/utils/multi-factor-auth.ts';
|
|
import { TOTPModel } from '/lib/models/multi-factor-auth/totp.ts';
|
|
import { EmailModel } from '/lib/models/multi-factor-auth/email.ts';
|
|
import { html } from '/public/ts/utils/misc.ts';
|
|
import { basicLayoutResponse } from '/lib/utils/layout.tsx';
|
|
import MultiFactorAuthVerifyForm from '/components/auth/MultiFactorAuthVerifyForm.tsx';
|
|
|
|
const titlePrefix = 'Multi-Factor Authentication Verification';
|
|
|
|
async function get({ request, match, session, isRunningLocally }: RequestHandlerParams) {
|
|
const isMultiFactorAuthEnabled = await AppConfig.isMultiFactorAuthEnabled();
|
|
|
|
if (!isMultiFactorAuthEnabled) {
|
|
return new Response('Redirect', { status: 303, headers: { 'Location': '/login' } });
|
|
}
|
|
|
|
const searchParams = new URL(request.url).searchParams;
|
|
const redirectUrl = searchParams.get('redirect') || '/';
|
|
|
|
const { user } = (await MultiFactorAuthModel.getDataFromRequest(request)) || {};
|
|
|
|
if (!user) {
|
|
return new Response('Redirect', { status: 303, headers: { 'Location': '/login' } });
|
|
}
|
|
|
|
const hasMultiFactorAuthEnabled = isMultiFactorAuthEnabledForUser(user);
|
|
|
|
if (!hasMultiFactorAuthEnabled) {
|
|
return new Response('Redirect', { status: 303, headers: { 'Location': '/login' } });
|
|
}
|
|
|
|
const enabledMethods = getEnabledMultiFactorAuthMethodsFromUser(user);
|
|
const availableMethods = enabledMethods.map((method) => method.type);
|
|
|
|
const htmlContent = defaultHtmlContent({
|
|
email: user.email,
|
|
redirectUrl,
|
|
availableMethods,
|
|
});
|
|
|
|
return basicLayoutResponse(htmlContent, {
|
|
currentPath: match.pathname.input,
|
|
titlePrefix,
|
|
match,
|
|
request,
|
|
session,
|
|
isRunningLocally,
|
|
});
|
|
}
|
|
|
|
async function post({ request, match, session, isRunningLocally }: RequestHandlerParams) {
|
|
const isMultiFactorAuthEnabled = await AppConfig.isMultiFactorAuthEnabled();
|
|
|
|
if (!isMultiFactorAuthEnabled) {
|
|
return new Response('Redirect', { status: 303, headers: { 'Location': '/login' } });
|
|
}
|
|
|
|
const searchParams = new URL(request.url).searchParams;
|
|
const redirectUrl = searchParams.get('redirect') || '/';
|
|
|
|
const { user } = (await MultiFactorAuthModel.getDataFromRequest(request)) || {};
|
|
|
|
if (!user) {
|
|
return new Response('Redirect', { status: 303, headers: { 'Location': '/login' } });
|
|
}
|
|
|
|
const hasMultiFactorAuthEnabled = isMultiFactorAuthEnabledForUser(user);
|
|
|
|
if (!hasMultiFactorAuthEnabled) {
|
|
return new Response('Redirect', { status: 303, headers: { 'Location': '/login' } });
|
|
}
|
|
|
|
const enabledMethods = getEnabledMultiFactorAuthMethodsFromUser(user);
|
|
const availableMethods = enabledMethods.map((method) => method.type);
|
|
|
|
try {
|
|
const formData = await request.formData();
|
|
const code = getFormDataField(formData, 'code');
|
|
const token = getFormDataField(formData, 'token');
|
|
|
|
if (!code && !token) {
|
|
throw new Error('Authentication code/token is required');
|
|
}
|
|
|
|
let isValid = false;
|
|
let updateUser = false;
|
|
|
|
for (const method of enabledMethods) {
|
|
// Passkey verification is handled in a separate process
|
|
if (method.type === 'passkey') {
|
|
continue;
|
|
}
|
|
|
|
if (method.type === 'totp') {
|
|
const verification = await TOTPModel.verifyMethodToken(method.metadata, token);
|
|
if (verification.isValid) {
|
|
isValid = true;
|
|
|
|
if (verification.remainingCodes && method.type === 'totp' && method.metadata.totp) {
|
|
method.metadata.totp.hashed_backup_codes = verification.remainingCodes;
|
|
updateUser = true;
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (method.type === 'email') {
|
|
const verification = await EmailModel.verifyCode(method.id, code, user);
|
|
if (verification) {
|
|
isValid = true;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!isValid) {
|
|
throw new Error('Invalid authentication code/token or backup code');
|
|
}
|
|
|
|
if (updateUser) {
|
|
await UserModel.update(user);
|
|
}
|
|
|
|
return await createSessionResponse(request, user, { urlToRedirectTo: redirectUrl });
|
|
} catch (error) {
|
|
console.error('Multi-factor authentication verification error:', error);
|
|
|
|
const htmlContent = defaultHtmlContent({
|
|
error: {
|
|
title: 'Verification Failed',
|
|
message: (error as Error).message,
|
|
},
|
|
email: user.email,
|
|
redirectUrl,
|
|
availableMethods,
|
|
});
|
|
|
|
return basicLayoutResponse(htmlContent, {
|
|
currentPath: match.pathname.input,
|
|
titlePrefix,
|
|
match,
|
|
request,
|
|
session,
|
|
isRunningLocally,
|
|
});
|
|
}
|
|
}
|
|
|
|
function defaultHtmlContent({ email, redirectUrl, availableMethods, error }: {
|
|
error?: {
|
|
title: string;
|
|
message: string;
|
|
};
|
|
email?: string;
|
|
redirectUrl?: string;
|
|
availableMethods?: MultiFactorAuthMethodType[];
|
|
}) {
|
|
const multiFactorAuthVerifyFormReactNode = (
|
|
<MultiFactorAuthVerifyForm
|
|
email={email || ''}
|
|
redirectUrl={redirectUrl || '/'}
|
|
availableMethods={availableMethods || []}
|
|
error={error}
|
|
/>
|
|
);
|
|
const multiFactorAuthVerifyFormHtml = renderToString(multiFactorAuthVerifyFormReactNode);
|
|
|
|
return html`
|
|
<main id="main">
|
|
<section class="max-w-3xl mx-auto flex flex-col items-center justify-center">
|
|
${multiFactorAuthVerifyFormHtml}
|
|
</section>
|
|
</main>
|
|
`;
|
|
}
|
|
|
|
export default page({
|
|
get,
|
|
post,
|
|
accessMode: 'public',
|
|
});
|