From 0e528f35f0c8440414ba8571f84155c8d3d7e09d Mon Sep 17 00:00:00 2001 From: Andreas Coroiu Date: Fri, 28 Apr 2023 10:09:14 +0200 Subject: [PATCH] [EC-598] feat: add webauthn polyfill --- apps/browser/src/fido2/content/page-script.ts | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/apps/browser/src/fido2/content/page-script.ts b/apps/browser/src/fido2/content/page-script.ts index a75b871246..2fed128666 100644 --- a/apps/browser/src/fido2/content/page-script.ts +++ b/apps/browser/src/fido2/content/page-script.ts @@ -3,11 +3,34 @@ import { WebauthnUtils } from "../../browser/webauthn-utils"; import { MessageType } from "./messaging/message"; import { Messenger } from "./messaging/messenger"; +const browserNativeWebauthnSupport = window.PublicKeyCredential == undefined; +if (!browserNativeWebauthnSupport) { + // Polyfill webauthn in browser without support + try { + // credentials is read-only if supported, use type-casting to force assignment + (navigator as any).credentials = { + async create() { + throw new Error("Webauthn not supported in this browser."); + }, + async get() { + throw new Error("Webauthn not supported in this browser."); + }, + }; + // We will be replacing this class with our own implementation when responding + window.PublicKeyCredential = class {} as any; + } catch { + /* empty */ + } +} +window.PublicKeyCredential.isUserVerifyingPlatformAuthenticatorAvailable = async () => true; + const browserCredentials = { create: navigator.credentials.create.bind( navigator.credentials ) as typeof navigator.credentials.create, get: navigator.credentials.get.bind(navigator.credentials) as typeof navigator.credentials.get, + isUserVerifyingPlatformAuthenticatorAvailable: + window.PublicKeyCredential.isUserVerifyingPlatformAuthenticatorAvailable, }; const messenger = Messenger.forDOMCommunication(window);