bewcloud/pages/api/auth/multi-factor/passkey/setup-begin.ts
Bruno Bernardino c26cae625e
Remove fresh
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
2026-02-20 10:54:31 +00:00

61 lines
1.6 KiB
TypeScript

import page, { RequestHandlerParams } from '/lib/page.ts';
import { PublicKeyCredentialCreationOptionsJSON } from '@simplewebauthn/server';
import { AppConfig } from '/lib/config.ts';
import { PasskeyModel } from '/lib/models/multi-factor-auth/passkey.ts';
import { MultiFactorAuthModel } from '/lib/models/multi-factor-auth.ts';
export interface ResponseBody {
success: boolean;
error?: string;
options?: PublicKeyCredentialCreationOptionsJSON;
sessionData?: {
challenge: string;
methodId: string;
userId: string;
};
}
async function post({ user }: RequestHandlerParams) {
const isMultiFactorAuthEnabled = await AppConfig.isMultiFactorAuthEnabled();
if (!isMultiFactorAuthEnabled) {
const responseBody: ResponseBody = {
success: false,
error: 'Passwordless passkey login requires multi-factor authentication to be enabled.',
};
return new Response(JSON.stringify(responseBody), { status: 403 });
}
const methodId = MultiFactorAuthModel.generateMethodId();
const config = await AppConfig.getConfig();
const existingCredentials = PasskeyModel.getCredentialsFromUser(user!);
const options = await PasskeyModel.generateRegistrationOptions(
user!.id,
user!.email,
config.auth.baseUrl,
existingCredentials,
);
const sessionData: ResponseBody['sessionData'] = {
challenge: options.challenge,
methodId,
userId: user!.id,
};
const responseBody: ResponseBody = {
success: true,
options,
sessionData,
};
return new Response(JSON.stringify(responseBody));
}
export default page({
post,
accessMode: 'user',
});