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
98 lines
2.8 KiB
TypeScript
98 lines
2.8 KiB
TypeScript
import page, { RequestHandlerParams } from '/lib/page.ts';
|
|
import { isoBase64URL } from '@simplewebauthn/server/helpers';
|
|
import { RegistrationResponseJSON } from '@simplewebauthn/server';
|
|
|
|
import { PasskeyModel } from '/lib/models/multi-factor-auth/passkey.ts';
|
|
import { UserModel } from '/lib/models/user.ts';
|
|
import { AppConfig } from '/lib/config.ts';
|
|
|
|
export interface RequestBody {
|
|
methodId: string;
|
|
challenge: string;
|
|
registrationResponse: RegistrationResponseJSON;
|
|
}
|
|
|
|
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: 'Passwordless passkey login requires multi-factor authentication to be enabled.',
|
|
};
|
|
|
|
return new Response(JSON.stringify(responseBody), { status: 403 });
|
|
}
|
|
|
|
const body = await request.clone().json() as RequestBody;
|
|
const { methodId, challenge, registrationResponse } = body;
|
|
|
|
if (!methodId || !challenge || !registrationResponse) {
|
|
const responseBody: ResponseBody = {
|
|
success: false,
|
|
error: 'Method ID, challenge, and registration response are required',
|
|
};
|
|
|
|
return new Response(JSON.stringify(responseBody), { status: 400 });
|
|
}
|
|
|
|
const config = await AppConfig.getConfig();
|
|
const expectedOrigin = config.auth.baseUrl;
|
|
const expectedRPID = new URL(config.auth.baseUrl).hostname;
|
|
|
|
const verification = await PasskeyModel.verifyRegistration(
|
|
registrationResponse,
|
|
challenge,
|
|
expectedOrigin,
|
|
expectedRPID,
|
|
);
|
|
|
|
if (!verification.verified || !verification.registrationInfo) {
|
|
const responseBody: ResponseBody = {
|
|
success: false,
|
|
error: 'Passkey registration verification failed',
|
|
};
|
|
|
|
return new Response(JSON.stringify(responseBody), { status: 400 });
|
|
}
|
|
|
|
const { registrationInfo } = verification;
|
|
const credentialID = registrationInfo.credential.id;
|
|
const credentialPublicKey = isoBase64URL.fromBuffer(registrationInfo.credential.publicKey);
|
|
|
|
const method = PasskeyModel.createMethod(
|
|
methodId,
|
|
'Passkey',
|
|
credentialID,
|
|
credentialPublicKey,
|
|
registrationInfo.credential.counter,
|
|
registrationInfo.credentialDeviceType,
|
|
registrationInfo.credentialBackedUp,
|
|
// @ts-expect-error SimpleWebAuthn supports a few more transports, and that's OK
|
|
registrationResponse.response?.transports || [],
|
|
);
|
|
|
|
if (!user!.extra.multi_factor_auth_methods) {
|
|
user!.extra.multi_factor_auth_methods = [];
|
|
}
|
|
|
|
user!.extra.multi_factor_auth_methods.push(method);
|
|
|
|
await UserModel.update(user!);
|
|
|
|
const responseBody: ResponseBody = {
|
|
success: true,
|
|
};
|
|
|
|
return new Response(JSON.stringify(responseBody));
|
|
}
|
|
|
|
export default page({
|
|
post,
|
|
accessMode: 'user',
|
|
});
|