mirror of
https://github.com/documenso/documenso.git
synced 2025-11-10 04:22:32 +10:00
## Description Add the following document action auth options: - 2FA - Passkey If the user does not have the required auth setup, we onboard them directly. ## Changes made Note: Added secondaryId to the VerificationToken schema ## Testing Performed Tested locally, pending preview tests ## Checklist - [X] I have tested these changes locally and they work as expected. - [X] I have added/updated tests that prove the effectiveness of these changes. - [X] I have followed the project's coding style guidelines. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Introduced components for 2FA, account, and passkey authentication during document signing. - Added "Require passkey" option to document settings and signer authentication settings. - Enhanced form submission and loading states for improved user experience. - **Refactor** - Optimized authentication components to efficiently support multiple authentication methods. - **Chores** - Updated and renamed functions and components for clarity and consistency across the authentication system. - Refined sorting options and database schema to support new authentication features. - **Bug Fixes** - Adjusted SignInForm to verify browser support for WebAuthn before proceeding. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
107 lines
2.8 KiB
TypeScript
107 lines
2.8 KiB
TypeScript
import { verifyRegistrationResponse } from '@simplewebauthn/server';
|
|
import type { RegistrationResponseJSON } from '@simplewebauthn/types';
|
|
|
|
import { prisma } from '@documenso/prisma';
|
|
import { UserSecurityAuditLogType } from '@documenso/prisma/client';
|
|
|
|
import { MAXIMUM_PASSKEYS } from '../../constants/auth';
|
|
import { AppError, AppErrorCode } from '../../errors/app-error';
|
|
import type { RequestMetadata } from '../../universal/extract-request-metadata';
|
|
import { getAuthenticatorOptions } from '../../utils/authenticator';
|
|
|
|
type CreatePasskeyOptions = {
|
|
userId: number;
|
|
passkeyName: string;
|
|
verificationResponse: RegistrationResponseJSON;
|
|
requestMetadata?: RequestMetadata;
|
|
};
|
|
|
|
export const createPasskey = async ({
|
|
userId,
|
|
passkeyName,
|
|
verificationResponse,
|
|
requestMetadata,
|
|
}: CreatePasskeyOptions) => {
|
|
const { _count } = await prisma.user.findFirstOrThrow({
|
|
where: {
|
|
id: userId,
|
|
},
|
|
include: {
|
|
_count: {
|
|
select: {
|
|
passkeys: true,
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
if (_count.passkeys >= MAXIMUM_PASSKEYS) {
|
|
throw new AppError('TOO_MANY_PASSKEYS');
|
|
}
|
|
|
|
const verificationToken = await prisma.verificationToken.findFirst({
|
|
where: {
|
|
userId,
|
|
identifier: 'PASSKEY_CHALLENGE',
|
|
},
|
|
orderBy: {
|
|
createdAt: 'desc',
|
|
},
|
|
});
|
|
|
|
if (!verificationToken) {
|
|
throw new AppError(AppErrorCode.NOT_FOUND, 'Challenge token not found');
|
|
}
|
|
|
|
await prisma.verificationToken.deleteMany({
|
|
where: {
|
|
userId,
|
|
identifier: 'PASSKEY_CHALLENGE',
|
|
},
|
|
});
|
|
|
|
if (verificationToken.expires < new Date()) {
|
|
throw new AppError(AppErrorCode.EXPIRED_CODE, 'Challenge token expired');
|
|
}
|
|
|
|
const { rpId: expectedRPID, origin: expectedOrigin } = getAuthenticatorOptions();
|
|
|
|
const verification = await verifyRegistrationResponse({
|
|
response: verificationResponse,
|
|
expectedChallenge: verificationToken.token,
|
|
expectedOrigin,
|
|
expectedRPID,
|
|
});
|
|
|
|
if (!verification.verified || !verification.registrationInfo) {
|
|
throw new AppError(AppErrorCode.UNAUTHORIZED, 'Verification failed');
|
|
}
|
|
|
|
const { credentialPublicKey, credentialID, counter, credentialDeviceType, credentialBackedUp } =
|
|
verification.registrationInfo;
|
|
|
|
await prisma.$transaction(async (tx) => {
|
|
await tx.passkey.create({
|
|
data: {
|
|
userId,
|
|
name: passkeyName,
|
|
credentialId: Buffer.from(credentialID),
|
|
credentialPublicKey: Buffer.from(credentialPublicKey),
|
|
counter,
|
|
credentialDeviceType,
|
|
credentialBackedUp,
|
|
transports: verificationResponse.response.transports,
|
|
},
|
|
});
|
|
|
|
await tx.userSecurityAuditLog.create({
|
|
data: {
|
|
userId,
|
|
type: UserSecurityAuditLogType.PASSKEY_CREATED,
|
|
userAgent: requestMetadata?.userAgent,
|
|
ipAddress: requestMetadata?.ipAddress,
|
|
},
|
|
});
|
|
});
|
|
};
|