From b054c070adee69d61bc11767b55b9fca8d9cf368 Mon Sep 17 00:00:00 2001 From: Manav Rathi Date: Mon, 30 Dec 2024 16:45:14 +0530 Subject: [PATCH] Parse --- .../accounts/src/pages/passkeys/verify.tsx | 2 +- web/apps/accounts/src/services/passkey.ts | 2 +- web/packages/accounts/services/passkey.ts | 2 +- web/packages/accounts/services/user.ts | 30 +++++++++++-------- web/packages/shared/user/types.ts | 8 ++--- 5 files changed, 25 insertions(+), 19 deletions(-) diff --git a/web/apps/accounts/src/pages/passkeys/verify.tsx b/web/apps/accounts/src/pages/passkeys/verify.tsx index b27086868e..9fcefb689e 100644 --- a/web/apps/accounts/src/pages/passkeys/verify.tsx +++ b/web/apps/accounts/src/pages/passkeys/verify.tsx @@ -1,4 +1,4 @@ -import type { TwoFactorAuthorizationResponse } from "@/accounts/services/user"; +import { TwoFactorAuthorizationResponse } from "@/accounts/services/user"; import { ActivityIndicator } from "@/base/components/mui/ActivityIndicator"; import { FocusVisibleButton } from "@/base/components/mui/FocusVisibleButton"; import log from "@/base/log"; diff --git a/web/apps/accounts/src/services/passkey.ts b/web/apps/accounts/src/services/passkey.ts index fdd6a8bbd5..ee5f0c40c8 100644 --- a/web/apps/accounts/src/services/passkey.ts +++ b/web/apps/accounts/src/services/passkey.ts @@ -1,4 +1,4 @@ -import type { TwoFactorAuthorizationResponse } from "@/accounts/services/user"; +import { TwoFactorAuthorizationResponse } from "@/accounts/services/user"; import { clientPackageName } from "@/base/app"; import { fromB64URLSafeNoPadding, diff --git a/web/packages/accounts/services/passkey.ts b/web/packages/accounts/services/passkey.ts index c766e7d243..f6f4ae1d68 100644 --- a/web/packages/accounts/services/passkey.ts +++ b/web/packages/accounts/services/passkey.ts @@ -1,3 +1,4 @@ +import { TwoFactorAuthorizationResponse } from "@/accounts/services/user"; import { clientPackageName, isDesktop } from "@/base/app"; import { sharedCryptoWorker } from "@/base/crypto"; import { encryptToB64, generateEncryptionKey } from "@/base/crypto/libsodium"; @@ -9,7 +10,6 @@ import { } from "@/base/http"; import log from "@/base/log"; import { accountsAppOrigin, apiURL } from "@/base/origins"; -import type { TwoFactorAuthorizationResponse } from "@/accounts/services/user"; import { nullToUndefined } from "@/utils/transform"; import { getRecoveryKey } from "@ente/shared/crypto/helpers"; import HTTPService from "@ente/shared/network/HTTPService"; diff --git a/web/packages/accounts/services/user.ts b/web/packages/accounts/services/user.ts index 0aa2d99338..f16e1d91e7 100644 --- a/web/packages/accounts/services/user.ts +++ b/web/packages/accounts/services/user.ts @@ -13,18 +13,18 @@ import { z } from "zod"; export interface UserVerificationResponse { id: number; - keyAttributes?: KeyAttributes; - encryptedToken?: string; + keyAttributes?: KeyAttributes | undefined; + encryptedToken?: string | undefined; token?: string; - twoFactorSessionID: string; - passkeySessionID: string; + twoFactorSessionID?: string | undefined; + passkeySessionID?: string | undefined; /** * If both passkeys and TOTP based two factors are enabled, then {@link * twoFactorSessionIDV2} will be set to the TOTP session ID instead of * {@link twoFactorSessionID}. */ twoFactorSessionIDV2?: string | undefined; - srpM2?: string; + srpM2?: string | undefined; } export interface TwoFactorVerificationResponse { @@ -101,12 +101,18 @@ export const verifyEmail = async ( email: string, ott: string, source: string | undefined, -) => { - return HTTPService.post(await apiURL("/users/verify-email"), { - email, - ott, - ...(source ? { source } : {}), +): Promise => { + const res = await fetch(await apiURL("/users/verify-email"), { + method: "POST", + headers: publicRequestHeaders(), + body: JSON.stringify({ + email, + ott, + ...(source ? { source } : {}), + }), }); + ensureOk(res); + return EmailOrSRPAuthorizationResponse.parse(await res.json()); }; /** @@ -141,7 +147,7 @@ const RemoteKeyAttributes = z.object({ * (`passkeySessionID`, `twoFactorSessionID` / `twoFactorSessionIDV2`) will be * set. Otherwise `keyAttributes` and `encryptedToken` will be set. */ -export const RemoteUserVerificationResponse = z.object({ +export const EmailOrSRPAuthorizationResponse = z.object({ id: z.number(), keyAttributes: RemoteKeyAttributes.nullish().transform(nullToUndefined), encryptedToken: z.string().nullish().transform(nullToUndefined), @@ -166,7 +172,7 @@ export const RemoteUserVerificationResponse = z.object({ /** * The result of a successful two factor verification (totp or passkey). */ -const TwoFactorAuthorizationResponse = z.object({ +export const TwoFactorAuthorizationResponse = z.object({ id: z.number(), /** TODO: keyAttributes is guaranteed to be returned by museum, update the * types to reflect that. */ diff --git a/web/packages/shared/user/types.ts b/web/packages/shared/user/types.ts index 38608ecc87..86b1c1c261 100644 --- a/web/packages/shared/user/types.ts +++ b/web/packages/shared/user/types.ts @@ -11,11 +11,11 @@ export interface KeyAttributes { encryptedSecretKey: string; secretKeyDecryptionNonce: string; /** Doesn't change after being initially created. */ - masterKeyEncryptedWithRecoveryKey: string; - masterKeyDecryptionNonce: string; + masterKeyEncryptedWithRecoveryKey?: string; + masterKeyDecryptionNonce?: string; /** Doesn't change after being initially created. */ - recoveryKeyEncryptedWithMasterKey: string; - recoveryKeyDecryptionNonce: string; + recoveryKeyEncryptedWithMasterKey?: string; + recoveryKeyDecryptionNonce?: string; } export interface User {