Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion app/actions/_userActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
NotFoundError,
} from "@/lib/errors";
import { prisma } from "@/lib/prisma";
import { createVault } from "./_vaultActions";

export const finishOnboarding = withErrorHandling(
withAuth(
Expand All @@ -18,6 +19,7 @@ export const finishOnboarding = withErrorHandling(
salt: string;
publicKey: string;
wrappedPrivateKey: string;
wrappedDefaultVaultKey: string;
}
) => {
const client = await clerkClient();
Expand All @@ -26,7 +28,8 @@ export const finishOnboarding = withErrorHandling(
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const email = user.primaryEmailAddress!.emailAddress;

const { salt, publicKey, wrappedPrivateKey } = data;
const { salt, publicKey, wrappedPrivateKey, wrappedDefaultVaultKey } =
data;

try {
// Check if user already exists
Expand Down Expand Up @@ -54,6 +57,11 @@ export const finishOnboarding = withErrorHandling(
},
});

Comment thread
ErionKsv marked this conversation as resolved.
Outdated
await createVault({
name: "Private",
wrappedKey: wrappedDefaultVaultKey,
});

await client.users.updateUser(user.id, {
publicMetadata: {
onboardingComplete: true,
Expand Down
3 changes: 2 additions & 1 deletion components/auth/onboarding-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,14 @@ export function SignUpForm({
}

try {
const { publicKey, wrappedPrivateKey, salt } =
const { publicKey, wrappedPrivateKey, salt, wrappedDefaultVaultKey } =
await cryptoService.onboarding(password);

const response = await finishOnboarding({
salt,
publicKey,
wrappedPrivateKey,
wrappedDefaultVaultKey,
});

// Handle error responses
Expand Down
15 changes: 11 additions & 4 deletions lib/crypto.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
export class CryptoService {
public async onboarding(
password: string
): Promise<{ publicKey: string; wrappedPrivateKey: string; salt: string }> {
public async onboarding(password: string): Promise<{
publicKey: string;
wrappedPrivateKey: string;
salt: string;
wrappedDefaultVaultKey: string;
}> {
const { publicKey, privateKey } = await this.generateKeyPair();
const salt = crypto.getRandomValues(new Uint8Array(16));
const kek = await this.deriveKek(password, salt);
const wrappedPrivateKey = await this.wrapPrivateKey(privateKey, kek);
const publicKeyBuffer = await crypto.subtle.exportKey("spki", publicKey);
const { wrappedKey: wrappedDefaultVaultKey } =
await this.generateAndWrapVaultKey(publicKey);

return {
publicKey: BufferTransformer.arrayBufferToBase64(publicKeyBuffer),
wrappedPrivateKey:
BufferTransformer.arrayBufferToBase64(wrappedPrivateKey),
salt: BufferTransformer.arrayBufferToBase64(salt.buffer),
wrappedDefaultVaultKey,
};
}

Expand Down Expand Up @@ -184,7 +191,7 @@ export class CryptoService {
hash: "SHA-256",
},
true,
["encrypt", "decrypt"]
["encrypt", "decrypt", "wrapKey"]
);
}

Expand Down