mirror of
https://github.com/bitwarden/browser
synced 2025-12-20 10:13:31 +00:00
[EC-598] feat: polyfill platform authenticator support
This commit is contained in:
@@ -3,9 +3,13 @@ import { WebauthnUtils } from "../../browser/webauthn-utils";
|
|||||||
import { MessageType } from "./messaging/message";
|
import { MessageType } from "./messaging/message";
|
||||||
import { Messenger } from "./messaging/messenger";
|
import { Messenger } from "./messaging/messenger";
|
||||||
|
|
||||||
const browserNativeWebauthnSupport = window.PublicKeyCredential == undefined;
|
const BrowserPublicKeyCredential = window.PublicKeyCredential;
|
||||||
|
|
||||||
|
const browserNativeWebauthnSupport = window.PublicKeyCredential != undefined;
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
|
let browserNativeWebauthnPlatformAuthenticatorSupport = false;
|
||||||
if (!browserNativeWebauthnSupport) {
|
if (!browserNativeWebauthnSupport) {
|
||||||
// Polyfill webauthn in browser without support
|
// Polyfill webauthn support
|
||||||
try {
|
try {
|
||||||
// credentials is read-only if supported, use type-casting to force assignment
|
// credentials is read-only if supported, use type-casting to force assignment
|
||||||
(navigator as any).credentials = {
|
(navigator as any).credentials = {
|
||||||
@@ -16,21 +20,33 @@ if (!browserNativeWebauthnSupport) {
|
|||||||
throw new Error("Webauthn not supported in this browser.");
|
throw new Error("Webauthn not supported in this browser.");
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
// We will be replacing this class with our own implementation when responding
|
window.PublicKeyCredential = class {
|
||||||
window.PublicKeyCredential = class {} as any;
|
static isUserVerifyingPlatformAuthenticatorAvailable() {
|
||||||
|
return Promise.resolve(true);
|
||||||
|
}
|
||||||
|
} as any;
|
||||||
} catch {
|
} catch {
|
||||||
/* empty */
|
/* empty */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
window.PublicKeyCredential.isUserVerifyingPlatformAuthenticatorAvailable = async () => true;
|
|
||||||
|
if (browserNativeWebauthnSupport) {
|
||||||
|
BrowserPublicKeyCredential.isUserVerifyingPlatformAuthenticatorAvailable().then((available) => {
|
||||||
|
browserNativeWebauthnPlatformAuthenticatorSupport = available;
|
||||||
|
|
||||||
|
if (!available) {
|
||||||
|
// Polyfill platform authenticator support
|
||||||
|
window.PublicKeyCredential.isUserVerifyingPlatformAuthenticatorAvailable = () =>
|
||||||
|
Promise.resolve(true);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
const browserCredentials = {
|
const browserCredentials = {
|
||||||
create: navigator.credentials.create.bind(
|
create: navigator.credentials.create.bind(
|
||||||
navigator.credentials
|
navigator.credentials
|
||||||
) as typeof navigator.credentials.create,
|
) as typeof navigator.credentials.create,
|
||||||
get: navigator.credentials.get.bind(navigator.credentials) as typeof navigator.credentials.get,
|
get: navigator.credentials.get.bind(navigator.credentials) as typeof navigator.credentials.get,
|
||||||
isUserVerifyingPlatformAuthenticatorAvailable:
|
|
||||||
window.PublicKeyCredential.isUserVerifyingPlatformAuthenticatorAvailable,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const messenger = Messenger.forDOMCommunication(window);
|
const messenger = Messenger.forDOMCommunication(window);
|
||||||
|
|||||||
Reference in New Issue
Block a user