Skip to content

Commit

Permalink
Better polyfill
Browse files Browse the repository at this point in the history
  • Loading branch information
agektmr committed Nov 6, 2024
1 parent 755f03a commit d8410a0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
30 changes: 19 additions & 11 deletions public/webauthn-polyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,21 @@ export class base64url {
}

if (window.PublicKeyCredential) {
const uap = new UAParser();
const browser = uap.getBrowser();
if (!browser?.version) {
throw new Error('Browser major version not found.');
}
const browserName = browser.name;
const browserVer = parseFloat(browser.version.replace(/^([0-9]+\.[0-9]+).*$/, '$1'));

const engine = uap.getEngine();
if (!engine?.version) {
throw new Error('Engine version not found.');
}
const engineName = engine.name;
const engineVer = parseFloat(engine.version.replace(/^([0-9]+\.[0-9]+)\.*$/, '$1'));

if (!window.PublicKeyCredential?.parseCreationOptionsFromJSON) {
PublicKeyCredential.parseCreationOptionsFromJSON = (options) => {
const user = {
Expand Down Expand Up @@ -103,7 +118,9 @@ if (window.PublicKeyCredential) {
}
}

if (!PublicKeyCredential.getClientCapabilities) {
if (!PublicKeyCredential.getClientCapabilities ||
// If this is Safari 17.4+, there's a spec glitch.
(browserName === 'Safari' && browserVer >= 17.4)) {
PublicKeyCredential.getClientCapabilities = async () => {
let conditionalCreate = false;
let conditionalGet = false;
Expand Down Expand Up @@ -134,20 +151,11 @@ if (window.PublicKeyCredential) {
signalUnknownCredential = true;
}

const uap = new UAParser();
const browser = uap.getBrowser();
const browserName = browser.name;
const browserVer = parseInt(browser.major);
const engine = uap.getEngine();
const engineName = engine.name;
const engineVer = parseInt(engine.version.replace(/^([0-9]+)\.*$/, '$1'));

// `conditionalCreate` is `true` on Safari 15+
if (browserName === 'Safari' && browserVer >= 15) {
if (browserName === 'Safari' && browserVer >= 18) {
conditionalCreate = true;
}
// `hybridTransport` is `true` on Firefox 119+, Chromium 108+ and Safari 16+
// TODO: These version numbers may not be precise.
if ((engineName === 'Blink' && engineVer >= 108) ||
(browserName === 'Firefox' && browserVer >= 119) ||
(browserName === 'Safari' && browserVer >= 16)) {
Expand Down
8 changes: 4 additions & 4 deletions views/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ <h2>Enter a username</h2>
});

// Feature detection: check if WebAuthn and conditional UI are supported.
if (window.PublicKeyCredential &&
PublicKeyCredential.isConditionalMediationAvailable) {
if (window.PublicKeyCredential) {
try {
const cma= await PublicKeyCredential.isConditionalMediationAvailable();
if (cma) {
const capabilities = await PublicKeyCredential.getClientCapabilities();
if (capabilities.passkeyPlatformAuthenticator &&
capabilities.conditionalGet) {
// If a conditional UI is supported, invoke the conditional `authenticate()` immediately.
const user = await authenticate(true);
if (user) {
Expand Down

0 comments on commit d8410a0

Please sign in to comment.