mirror of
https://github.com/bewcloud/bewcloud.git
synced 2026-03-11 08:54:49 +00:00
Also upgrade all dependencies, make some unnecessary, and some other small tweaks. Because this has a big potential of unexpectedly breaking things, I'm doing a "breaking release", v3.
67 lines
1.8 KiB
TypeScript
67 lines
1.8 KiB
TypeScript
import { RouteHandler } from 'fresh';
|
|
import { PublicKeyCredentialCreationOptionsJSON } from '@simplewebauthn/server';
|
|
|
|
import { FreshContextState } from '/lib/types.ts';
|
|
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 RequestBody {}
|
|
|
|
export interface ResponseBody {
|
|
success: boolean;
|
|
error?: string;
|
|
options?: PublicKeyCredentialCreationOptionsJSON;
|
|
sessionData?: {
|
|
challenge: string;
|
|
methodId: string;
|
|
userId: string;
|
|
};
|
|
}
|
|
|
|
export const handler: RouteHandler<unknown, FreshContextState> = {
|
|
async POST(context) {
|
|
if (!context.state.user) {
|
|
return new Response('Unauthorized', { status: 401 });
|
|
}
|
|
|
|
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 { user } = context.state;
|
|
|
|
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));
|
|
},
|
|
};
|