diff --git a/apps/browser/src/content/webauthn/content-script.ts b/apps/browser/src/content/webauthn/content-script.ts index 3183654a03..4c59914762 100644 --- a/apps/browser/src/content/webauthn/content-script.ts +++ b/apps/browser/src/content/webauthn/content-script.ts @@ -7,7 +7,7 @@ s.src = chrome.runtime.getURL("content/webauthn/page-script.js"); const messenger = Messenger.forDOMCommunication(window); -messenger.addHandler(async (message) => { +messenger.handler = async (message, abortController) => { if (message.type === MessageType.CredentialCreationRequest) { return new Promise((resolve, reject) => { chrome.runtime.sendMessage( @@ -16,10 +16,13 @@ messenger.addHandler(async (message) => { data: message.data, }, (response) => { + if (response.error !== undefined) { + return reject(response.error); + } + resolve({ type: MessageType.CredentialCreationResponse, result: response.result, - error: response.error, }); } ); @@ -34,10 +37,13 @@ messenger.addHandler(async (message) => { data: message.data, }, (response) => { + if (response.error !== undefined) { + return reject(response.error); + } + resolve({ type: MessageType.CredentialGetResponse, result: response.result, - error: response.error, }); } ); @@ -45,4 +51,4 @@ messenger.addHandler(async (message) => { } return undefined; -}); +}; diff --git a/apps/browser/src/content/webauthn/messaging/error.ts b/apps/browser/src/content/webauthn/messaging/error.ts deleted file mode 100644 index 1513777f3c..0000000000 --- a/apps/browser/src/content/webauthn/messaging/error.ts +++ /dev/null @@ -1,10 +0,0 @@ -export enum MessageErrorType { - RequestAborted, -} - -export type RequestAbortedMessageError = { - fallbackRequested: boolean; - message: string; -}; - -export type MessageError = RequestAbortedMessageError; diff --git a/apps/browser/src/content/webauthn/messaging/message.ts b/apps/browser/src/content/webauthn/messaging/message.ts index 36b907c467..17b952eccc 100644 --- a/apps/browser/src/content/webauthn/messaging/message.ts +++ b/apps/browser/src/content/webauthn/messaging/message.ts @@ -5,8 +5,6 @@ import { CredentialRegistrationResult, } from "@bitwarden/common/abstractions/fido2/fido2.service.abstraction"; -import { MessageError } from "./error"; - export enum MessageType { CredentialCreationRequest, CredentialCreationResponse, @@ -25,7 +23,6 @@ export type CredentialCreationRequest = { export type CredentialCreationResponse = { type: MessageType.CredentialCreationResponse; result?: CredentialRegistrationResult; - error?: MessageError; }; export type CredentialGetRequest = { @@ -36,7 +33,6 @@ export type CredentialGetRequest = { export type CredentialGetResponse = { type: MessageType.CredentialGetResponse; result?: CredentialAssertResult; - error?: MessageError; }; export type AbortRequest = { diff --git a/apps/browser/src/content/webauthn/page-script.ts b/apps/browser/src/content/webauthn/page-script.ts index 950e8eebce..e6e2f07812 100644 --- a/apps/browser/src/content/webauthn/page-script.ts +++ b/apps/browser/src/content/webauthn/page-script.ts @@ -10,48 +10,60 @@ const browserCredentials = { const messenger = Messenger.forDOMCommunication(window); -navigator.credentials.create = async (options?: CredentialCreationOptions): Promise<Credential> => { +navigator.credentials.create = async ( + options?: CredentialCreationOptions, + abortController?: AbortController +): Promise<Credential> => { if (options.publicKey?.authenticatorSelection?.authenticatorAttachment === "platform") { return await browserCredentials.create(options); } - const response = await messenger.request({ - type: MessageType.CredentialCreationRequest, - data: WebauthnUtils.mapCredentialCreationOptions(options, window.location.origin), - }); + try { + const response = await messenger.request( + { + type: MessageType.CredentialCreationRequest, + data: WebauthnUtils.mapCredentialCreationOptions(options, window.location.origin), + }, + abortController + ); - if (response.type !== MessageType.CredentialCreationResponse) { - return await browserCredentials.create(options); + if (response.type !== MessageType.CredentialCreationResponse) { + throw new Error("Something went wrong."); + } + + return WebauthnUtils.mapCredentialRegistrationResult(response.result); + } catch (error) { + if (error && error.fallbackRequested) { + return await browserCredentials.create(options); + } + + throw error; } - - if (response.error && response.error.fallbackRequested) { - return await browserCredentials.create(options); - } - - if (response.error) { - throw new Error(response.error.message ?? "The request was aborted."); - } - - return WebauthnUtils.mapCredentialRegistrationResult(response.result); }; -navigator.credentials.get = async (options?: CredentialRequestOptions): Promise<Credential> => { - const response = await messenger.request({ - type: MessageType.CredentialGetRequest, - data: WebauthnUtils.mapCredentialRequestOptions(options, window.location.origin), - }); +navigator.credentials.get = async ( + options?: CredentialRequestOptions, + abortController?: AbortController +): Promise<Credential> => { + try { + const response = await messenger.request( + { + type: MessageType.CredentialGetRequest, + data: WebauthnUtils.mapCredentialRequestOptions(options, window.location.origin), + }, + abortController + ); - if (response.type !== MessageType.CredentialGetResponse) { - return await browserCredentials.get(options); + if (response.type !== MessageType.CredentialGetResponse) { + throw new Error("Something went wrong."); + } + + return WebauthnUtils.mapCredentialAssertResult(response.result); + } catch (error) { + if (error && error.fallbackRequested) { + return await browserCredentials.get(options); + } + + throw error; } - - if (response.error && response.error.fallbackRequested) { - return await browserCredentials.get(options); - } - - if (response.error) { - throw new Error(response.error.message ?? "The request was aborted."); - } - - return WebauthnUtils.mapCredentialAssertResult(response.result); };