This commit is contained in:
Aleksandr Kolbasov 2026-01-18 13:46:24 -05:00 committed by GitHub
commit c991f59ab2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -145,6 +145,29 @@
}
};
/**
* @returns {Promise<void>}
*/
const waitForFocus = function () {
/*
Some browsers (Firefox, Safari) reject requests to original `navigator.credentials.create/get` if the page
is out of focus (when the user selects a passkey in KeePassXC-desktop).
`document.visibilityState` is not suitable: if the page is visible, but the focus is on another application
(or DevTools), the request will be rejected.
*/
return new Promise((resolve) => {
if (document.hasFocus()) {
return resolve();
}
document.addEventListener(
'focus',
() => resolve(),
{ capture: true, passive: true, once: true }
);
});
};
// Throws errors to a correct exceptions
const throwError = function(errorCode, errorMessage) {
if ((!errorCode && !errorMessage) || errorCode === PASSKEYS_REQUEST_CANCELED) {
@ -203,8 +226,10 @@
if (!response.publicKey) {
if (!response.fallback) {
throwError(response?.errorCode, response?.errorMessage);
return null;
}
return response.fallback ? originalCredentials.create(options) : null;
await waitForFocus();
return originalCredentials.create(options);
}
return createPublicKeyCredential(response.publicKey);
@ -228,8 +253,10 @@
if (!response.publicKey) {
if (!response.fallback) {
throwError(response?.errorCode, response?.errorMessage);
return null;
}
return response.fallback ? originalCredentials.get(options) : null;
await waitForFocus();
return originalCredentials.get(options);
}
return createPublicKeyCredential(response.publicKey);