mirror of
https://github.com/bitwarden/browser
synced 2026-01-30 16:23:53 +00:00
Remove ts-strict-ignore from fido2 page (#18146)
* Remove `ts-strict-ignore` from fido2 page * Update typing issue * Fix AssertCredentialResult type issue * Remove non null assertions and add type guard * Addresses topWindow non null assertion * remove redundant check and remove ts strict from messenger --------- Co-authored-by: Jonathan Prusik <jprusik@users.noreply.github.com> Co-authored-by: Daniel Riera <driera@livefront.com>
This commit is contained in:
@@ -1,12 +1,10 @@
|
||||
// FIXME: Update this file to be type safe and remove this and next line
|
||||
// @ts-strict-ignore
|
||||
import { WebauthnUtils } from "../utils/webauthn-utils";
|
||||
|
||||
import { MessageTypes } from "./messaging/message";
|
||||
import { Messenger } from "./messaging/messenger";
|
||||
|
||||
(function (globalContext) {
|
||||
if (globalContext.document.currentScript) {
|
||||
if (globalContext.document.currentScript?.parentNode) {
|
||||
globalContext.document.currentScript.parentNode.removeChild(
|
||||
globalContext.document.currentScript,
|
||||
);
|
||||
@@ -86,7 +84,7 @@ import { Messenger } from "./messaging/messenger";
|
||||
*/
|
||||
async function createWebAuthnCredential(
|
||||
options?: CredentialCreationOptions,
|
||||
): Promise<Credential> {
|
||||
): Promise<Credential | null> {
|
||||
if (!isWebauthnCall(options)) {
|
||||
return await browserCredentials.create(options);
|
||||
}
|
||||
@@ -106,13 +104,18 @@ import { Messenger } from "./messaging/messenger";
|
||||
options?.signal,
|
||||
);
|
||||
|
||||
if (response.type !== MessageTypes.CredentialCreationResponse) {
|
||||
if (response.type !== MessageTypes.CredentialCreationResponse || !response.result) {
|
||||
throw new Error("Something went wrong.");
|
||||
}
|
||||
|
||||
return WebauthnUtils.mapCredentialRegistrationResult(response.result);
|
||||
} catch (error) {
|
||||
if (error && error.fallbackRequested && fallbackSupported) {
|
||||
if (
|
||||
fallbackSupported &&
|
||||
error instanceof Object &&
|
||||
"fallbackRequested" in error &&
|
||||
error.fallbackRequested
|
||||
) {
|
||||
await waitForFocus();
|
||||
return await browserCredentials.create(options);
|
||||
}
|
||||
@@ -127,7 +130,9 @@ import { Messenger } from "./messaging/messenger";
|
||||
* @param options Options for creating new credentials.
|
||||
* @returns Promise that resolves to the new credential object.
|
||||
*/
|
||||
async function getWebAuthnCredential(options?: CredentialRequestOptions): Promise<Credential> {
|
||||
async function getWebAuthnCredential(
|
||||
options?: CredentialRequestOptions,
|
||||
): Promise<Credential | null> {
|
||||
if (!isWebauthnCall(options)) {
|
||||
return await browserCredentials.get(options);
|
||||
}
|
||||
@@ -153,7 +158,7 @@ import { Messenger } from "./messaging/messenger";
|
||||
internalAbortController.signal,
|
||||
);
|
||||
internalAbortController.signal.removeEventListener("abort", abortListener);
|
||||
if (response.type !== MessageTypes.CredentialGetResponse) {
|
||||
if (response.type !== MessageTypes.CredentialGetResponse || !response.result) {
|
||||
throw new Error("Something went wrong.");
|
||||
}
|
||||
|
||||
@@ -176,7 +181,7 @@ import { Messenger } from "./messaging/messenger";
|
||||
abortSignal.removeEventListener("abort", abortListener);
|
||||
internalAbortControllers.forEach((controller) => controller.abort());
|
||||
|
||||
return response;
|
||||
return response ?? null;
|
||||
}
|
||||
|
||||
try {
|
||||
@@ -188,13 +193,18 @@ import { Messenger } from "./messaging/messenger";
|
||||
options?.signal,
|
||||
);
|
||||
|
||||
if (response.type !== MessageTypes.CredentialGetResponse) {
|
||||
if (response.type !== MessageTypes.CredentialGetResponse || !response.result) {
|
||||
throw new Error("Something went wrong.");
|
||||
}
|
||||
|
||||
return WebauthnUtils.mapCredentialAssertResult(response.result);
|
||||
} catch (error) {
|
||||
if (error && error.fallbackRequested && fallbackSupported) {
|
||||
if (
|
||||
fallbackSupported &&
|
||||
error instanceof Object &&
|
||||
"fallbackRequested" in error &&
|
||||
error.fallbackRequested
|
||||
) {
|
||||
await waitForFocus();
|
||||
return await browserCredentials.get(options);
|
||||
}
|
||||
@@ -203,8 +213,10 @@ import { Messenger } from "./messaging/messenger";
|
||||
}
|
||||
}
|
||||
|
||||
function isWebauthnCall(options?: CredentialCreationOptions | CredentialRequestOptions) {
|
||||
return options && "publicKey" in options;
|
||||
function isWebauthnCall(
|
||||
options?: CredentialCreationOptions | CredentialRequestOptions,
|
||||
): options is CredentialCreationOptions | CredentialRequestOptions {
|
||||
return options != null && "publicKey" in options;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -217,7 +229,7 @@ import { Messenger } from "./messaging/messenger";
|
||||
*/
|
||||
async function waitForFocus(fallbackWait = 500, timeout = 5 * 60 * 1000) {
|
||||
try {
|
||||
if (globalContext.top.document.hasFocus()) {
|
||||
if (globalContext.top?.document.hasFocus()) {
|
||||
return;
|
||||
}
|
||||
} catch {
|
||||
@@ -225,9 +237,14 @@ import { Messenger } from "./messaging/messenger";
|
||||
return await new Promise((resolve) => globalContext.setTimeout(resolve, fallbackWait));
|
||||
}
|
||||
|
||||
if (!globalContext.top) {
|
||||
return await new Promise((resolve) => globalContext.setTimeout(resolve, fallbackWait));
|
||||
}
|
||||
|
||||
const topWindow = globalContext.top;
|
||||
const focusPromise = new Promise<void>((resolve) => {
|
||||
focusListenerHandler = () => resolve();
|
||||
globalContext.top.addEventListener("focus", focusListenerHandler);
|
||||
topWindow.addEventListener("focus", focusListenerHandler);
|
||||
});
|
||||
|
||||
const timeoutPromise = new Promise<void>((_, reject) => {
|
||||
@@ -248,7 +265,7 @@ import { Messenger } from "./messaging/messenger";
|
||||
}
|
||||
|
||||
function clearWaitForFocus() {
|
||||
globalContext.top.removeEventListener("focus", focusListenerHandler);
|
||||
globalContext.top?.removeEventListener("focus", focusListenerHandler);
|
||||
if (waitForFocusTimeout) {
|
||||
globalContext.clearTimeout(waitForFocusTimeout);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
// FIXME: Update this file to be type safe and remove this and next line
|
||||
// @ts-strict-ignore
|
||||
import { Message, MessageTypes } from "./message";
|
||||
|
||||
const SENDER = "bitwarden-webauthn";
|
||||
@@ -25,7 +23,9 @@ type Handler = (
|
||||
* handling aborts and exceptions across separate execution contexts.
|
||||
*/
|
||||
export class Messenger {
|
||||
private messageEventListener: (event: MessageEvent<MessageWithMetadata>) => void | null = null;
|
||||
private messageEventListener:
|
||||
| ((event: MessageEvent<MessageWithMetadata>) => void | Promise<void>)
|
||||
| null = null;
|
||||
private onDestroy = new EventTarget();
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user