Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Split

Manav Rathie1f36b8f8c9b
diff --git a/web/apps/accounts/src/pages/passkeys/verify.tsx b/web/apps/accounts/src/pages/passkeys/verify.tsx
index 8a2c190abb..04da1987a9 100644
--- a/web/apps/accounts/src/pages/passkeys/verify.tsx
+++ b/web/apps/accounts/src/pages/passkeys/verify.tsx
@@ -9,7 +9,6 @@ import HTTPService from "@ente/shared/network/HTTPService";
import InfoIcon from "@mui/icons-material/Info";
import { Paper, Typography, styled } from "@mui/material";
import { t } from "i18next";
-import _sodium from "libsodium-wrappers";
import { useEffect, useState } from "react";
import {
attestChallenge,
@@ -17,6 +16,7 @@ import {
finishPasskeyAuthentication,
isWebAuthnSupported,
isWhitelistedRedirect,
+ redirectAfterPasskeyAuthentication,
type BeginPasskeyAuthenticationResponse,
} from "services/passkey";
@@ -88,7 +88,6 @@ const Page = () => {
}
let beginData: BeginPasskeyAuthenticationResponse;
-
try {
beginData = await beginPasskeyAuthentication(passkeySessionID);
} catch (e) {
@@ -100,7 +99,6 @@ const Page = () => {
setStatus("waitingForUser");
const credential = await attestChallenge(beginData.options.publicKey);
-
if (!credential) {
setStatus("failed");
return;
@@ -108,13 +106,12 @@ const Page = () => {
setStatus("loading");
- let finishData;
-
+ let authenticationResult;
try {
- finishData = await finishPasskeyAuthentication(
- credential,
+ authenticationResult = await finishPasskeyAuthentication(
passkeySessionID,
beginData.ceremonySessionID,
+ credential,
);
} catch (e) {
log.error("Couldn't finish passkey authentication", e);
@@ -125,10 +122,7 @@ const Page = () => {
// Conceptually we can `setStatus("done")` at this point, but we'll
// leave this page anyway, so no need to tickle React.
- const encodedResponse = _sodium.to_base64(JSON.stringify(finishData));
-
- // TODO-PK: Shouldn't this be URL encoded?
- window.location.href = `${redirect}?response=${encodedResponse}`;
+ redirectAfterPasskeyAuthentication(redirectURL, authenticationResult);
};
useEffect(() => {
diff --git a/web/apps/accounts/src/services/passkey.ts b/web/apps/accounts/src/services/passkey.ts
index 69b01121e7..3f37208385 100644
--- a/web/apps/accounts/src/services/passkey.ts
+++ b/web/apps/accounts/src/services/passkey.ts
@@ -1,6 +1,5 @@
import { isDevBuild } from "@/next/env";
import { clientPackageHeaderIfPresent } from "@/next/http";
-import log from "@/next/log";
import { ensure } from "@/utils/ensure";
import { nullToUndefined } from "@/utils/transform";
import {
@@ -10,6 +9,7 @@ import {
import HTTPService from "@ente/shared/network/HTTPService";
import { apiOrigin, getEndpoint } from "@ente/shared/network/api";
import { getToken } from "@ente/shared/storage/localStorage/helpers";
+import _sodium from "libsodium-wrappers";
import { z } from "zod";
const ENDPOINT = getEndpoint();
@@ -389,8 +389,8 @@ export const beginPasskeyAuthentication = async (
* Authenticate the user by asking them to use a Passkey that the they had
* previously created for the current domain to attest a challenge.
*
- * This function implements step 2 and 3 of the passkey authentication flow. See
- * [Note: WebAuthn authentication flow].
+ * This function implements steps 2 and 3 of the passkey authentication flow.
+ * See [Note: WebAuthn authentication flow].
*
* @param publicKey A challenge and a list of public key credentials
* ("passkeys") that can be used to attest that challenge.
@@ -423,50 +423,78 @@ export const attestChallenge = async (
return await navigator.credentials.get({ publicKey });
};
+/**
+ * Finish the authentication by providing the attested challenge to the backend.
+ *
+ * This function implements steps 4 and 5 of the passkey authentication flow.
+ * See [Note: WebAuthn authentication flow].
+ *
+ * @returns The result of successful authentication. This is an meant to be an
+ * opaque JavaScript that should be returned back to the calling app by invoking
+ * {@link redirectAfterPasskeyAuthentication}.
+ */
export const finishPasskeyAuthentication = async (
+ passkeySessionID: string,
+ ceremonySessionID: string,
credential: Credential,
- sessionId: string,
- ceremonySessionId: string,
) => {
- try {
- const data = await HTTPService.post(
- `${ENDPOINT}/users/two-factor/passkeys/finish`,
- {
- id: credential.id,
- rawId: credential.id,
- type: credential.type,
- response: {
- authenticatorData: await toB64URLSafeNoPadding(
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
- // @ts-ignore
- new Uint8Array(credential.response.authenticatorData),
- ),
- clientDataJSON: await toB64URLSafeNoPadding(
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
- // @ts-ignore
- new Uint8Array(credential.response.clientDataJSON),
- ),
- signature: await toB64URLSafeNoPadding(
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
- // @ts-ignore
- new Uint8Array(credential.response.signature),
- ),
- userHandle: await toB64URLSafeNoPadding(
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
- // @ts-ignore
- new Uint8Array(credential.response.userHandle),
- ),
- },
- },
- {
- sessionID: sessionId,
- ceremonySessionID: ceremonySessionId,
+ const data = await HTTPService.post(
+ `${ENDPOINT}/users/two-factor/passkeys/finish`,
+ {
+ id: credential.id,
+ rawId: credential.id,
+ type: credential.type,
+ response: {
+ authenticatorData: await toB64URLSafeNoPadding(
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
+ // @ts-ignore
+ new Uint8Array(credential.response.authenticatorData),
+ ),
+ clientDataJSON: await toB64URLSafeNoPadding(
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
+ // @ts-ignore
+ new Uint8Array(credential.response.clientDataJSON),
+ ),
+ signature: await toB64URLSafeNoPadding(
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
+ // @ts-ignore
+ new Uint8Array(credential.response.signature),
+ ),
+ userHandle: await toB64URLSafeNoPadding(
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
+ // @ts-ignore
+ new Uint8Array(credential.response.userHandle),
+ ),
},
- );
+ },
+ {
+ sessionID: passkeySessionID,
+ ceremonySessionID: ceremonySessionID,
+ },
+ );
- return data.data;
- } catch (e) {
- log.error("finish passkey authentication failed", e);
- throw e;
- }
+ return data.data;
+};
+
+/**
+ * Redirect back to the calling app that initiated the passkey authentication
+ * flow with the result of the authentication (Ente "credentials" that the
+ * calling app that calling app can use to continue with the user journey).
+ *
+ * @param redirectURL The URL to redirect to. Provided by the calling app that
+ * initiated the passkey authentication.
+ *
+ * @param authenticationResult The result of {@link finishPasskeyAuthentication}
+ * returned by the backend.
+ */
+export const redirectAfterPasskeyAuthentication = (
+ redirectURL: URL,
+ authenticationResult: any,
+) => {
+ const encodedResponse = _sodium.to_base64(
+ JSON.stringify(authenticationResult),
+ );
+
+ // TODO-PK: Shouldn't this be URL encoded?
+ window.location.href = `${redirectURL}?response=${encodedResponse}`;
};