1
0
mirror of https://github.com/bitwarden/browser.git synced 2025-01-20 21:01:29 +01:00

[EC-598] feat: transition to using exceptions

This commit is contained in:
Andreas Coroiu 2023-01-27 16:28:37 +01:00
parent 547c29970d
commit f6eae754b3
No known key found for this signature in database
GPG Key ID: E70B5FFC81DFEC1A
4 changed files with 56 additions and 52 deletions

View File

@ -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;
});
};

View File

@ -1,10 +0,0 @@
export enum MessageErrorType {
RequestAborted,
}
export type RequestAbortedMessageError = {
fallbackRequested: boolean;
message: string;
};
export type MessageError = RequestAbortedMessageError;

View File

@ -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 = {

View File

@ -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);
};