mirror of
https://github.com/bitwarden/browser
synced 2025-12-17 08:43:33 +00:00
[EC-598] feat: transition to using exceptions
This commit is contained in:
@@ -7,7 +7,7 @@ s.src = chrome.runtime.getURL("content/webauthn/page-script.js");
|
|||||||
|
|
||||||
const messenger = Messenger.forDOMCommunication(window);
|
const messenger = Messenger.forDOMCommunication(window);
|
||||||
|
|
||||||
messenger.addHandler(async (message) => {
|
messenger.handler = async (message, abortController) => {
|
||||||
if (message.type === MessageType.CredentialCreationRequest) {
|
if (message.type === MessageType.CredentialCreationRequest) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
chrome.runtime.sendMessage(
|
chrome.runtime.sendMessage(
|
||||||
@@ -16,10 +16,13 @@ messenger.addHandler(async (message) => {
|
|||||||
data: message.data,
|
data: message.data,
|
||||||
},
|
},
|
||||||
(response) => {
|
(response) => {
|
||||||
|
if (response.error !== undefined) {
|
||||||
|
return reject(response.error);
|
||||||
|
}
|
||||||
|
|
||||||
resolve({
|
resolve({
|
||||||
type: MessageType.CredentialCreationResponse,
|
type: MessageType.CredentialCreationResponse,
|
||||||
result: response.result,
|
result: response.result,
|
||||||
error: response.error,
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
@@ -34,10 +37,13 @@ messenger.addHandler(async (message) => {
|
|||||||
data: message.data,
|
data: message.data,
|
||||||
},
|
},
|
||||||
(response) => {
|
(response) => {
|
||||||
|
if (response.error !== undefined) {
|
||||||
|
return reject(response.error);
|
||||||
|
}
|
||||||
|
|
||||||
resolve({
|
resolve({
|
||||||
type: MessageType.CredentialGetResponse,
|
type: MessageType.CredentialGetResponse,
|
||||||
result: response.result,
|
result: response.result,
|
||||||
error: response.error,
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
@@ -45,4 +51,4 @@ messenger.addHandler(async (message) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return undefined;
|
return undefined;
|
||||||
});
|
};
|
||||||
|
|||||||
@@ -1,10 +0,0 @@
|
|||||||
export enum MessageErrorType {
|
|
||||||
RequestAborted,
|
|
||||||
}
|
|
||||||
|
|
||||||
export type RequestAbortedMessageError = {
|
|
||||||
fallbackRequested: boolean;
|
|
||||||
message: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
export type MessageError = RequestAbortedMessageError;
|
|
||||||
@@ -5,8 +5,6 @@ import {
|
|||||||
CredentialRegistrationResult,
|
CredentialRegistrationResult,
|
||||||
} from "@bitwarden/common/abstractions/fido2/fido2.service.abstraction";
|
} from "@bitwarden/common/abstractions/fido2/fido2.service.abstraction";
|
||||||
|
|
||||||
import { MessageError } from "./error";
|
|
||||||
|
|
||||||
export enum MessageType {
|
export enum MessageType {
|
||||||
CredentialCreationRequest,
|
CredentialCreationRequest,
|
||||||
CredentialCreationResponse,
|
CredentialCreationResponse,
|
||||||
@@ -25,7 +23,6 @@ export type CredentialCreationRequest = {
|
|||||||
export type CredentialCreationResponse = {
|
export type CredentialCreationResponse = {
|
||||||
type: MessageType.CredentialCreationResponse;
|
type: MessageType.CredentialCreationResponse;
|
||||||
result?: CredentialRegistrationResult;
|
result?: CredentialRegistrationResult;
|
||||||
error?: MessageError;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export type CredentialGetRequest = {
|
export type CredentialGetRequest = {
|
||||||
@@ -36,7 +33,6 @@ export type CredentialGetRequest = {
|
|||||||
export type CredentialGetResponse = {
|
export type CredentialGetResponse = {
|
||||||
type: MessageType.CredentialGetResponse;
|
type: MessageType.CredentialGetResponse;
|
||||||
result?: CredentialAssertResult;
|
result?: CredentialAssertResult;
|
||||||
error?: MessageError;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export type AbortRequest = {
|
export type AbortRequest = {
|
||||||
|
|||||||
@@ -10,48 +10,60 @@ const browserCredentials = {
|
|||||||
|
|
||||||
const messenger = Messenger.forDOMCommunication(window);
|
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") {
|
if (options.publicKey?.authenticatorSelection?.authenticatorAttachment === "platform") {
|
||||||
return await browserCredentials.create(options);
|
return await browserCredentials.create(options);
|
||||||
}
|
}
|
||||||
|
|
||||||
const response = await messenger.request({
|
try {
|
||||||
type: MessageType.CredentialCreationRequest,
|
const response = await messenger.request(
|
||||||
data: WebauthnUtils.mapCredentialCreationOptions(options, window.location.origin),
|
{
|
||||||
});
|
type: MessageType.CredentialCreationRequest,
|
||||||
|
data: WebauthnUtils.mapCredentialCreationOptions(options, window.location.origin),
|
||||||
|
},
|
||||||
|
abortController
|
||||||
|
);
|
||||||
|
|
||||||
if (response.type !== MessageType.CredentialCreationResponse) {
|
if (response.type !== MessageType.CredentialCreationResponse) {
|
||||||
return await browserCredentials.create(options);
|
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> => {
|
navigator.credentials.get = async (
|
||||||
const response = await messenger.request({
|
options?: CredentialRequestOptions,
|
||||||
type: MessageType.CredentialGetRequest,
|
abortController?: AbortController
|
||||||
data: WebauthnUtils.mapCredentialRequestOptions(options, window.location.origin),
|
): Promise<Credential> => {
|
||||||
});
|
try {
|
||||||
|
const response = await messenger.request(
|
||||||
|
{
|
||||||
|
type: MessageType.CredentialGetRequest,
|
||||||
|
data: WebauthnUtils.mapCredentialRequestOptions(options, window.location.origin),
|
||||||
|
},
|
||||||
|
abortController
|
||||||
|
);
|
||||||
|
|
||||||
if (response.type !== MessageType.CredentialGetResponse) {
|
if (response.type !== MessageType.CredentialGetResponse) {
|
||||||
return await browserCredentials.get(options);
|
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);
|
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user