U2F API Polyfill
The `u2f-api-polyfill` package provides a JavaScript polyfill that implements the high-level FIDO U2F (Universal 2nd Factor) API for web browsers. It specifically targets environments where `window.u2f` might not natively exist, such as older Chrome versions. Maintained as version 0.4.4, its release cadence is irregular, primarily serving to pull in updates from Google's authoritative `u2f-ref-code` repository. This package acts as a direct port of Google's reference implementation, simplifying its consumption via NPM and eliminating the need for manual synchronization. It is not an independent U2F implementation but a faithful reproduction of Google's code designed for browser compatibility and to expose the global `window.u2f` object. While U2F itself has largely been superseded by the more modern WebAuthn standard, this polyfill remains relevant for maintaining compatibility with existing U2F implementations and legacy applications.
Common errors
-
Uncaught ReferenceError: u2f is not defined
cause The `u2f-api-polyfill` script was either not loaded, failed to execute, or did not successfully polyfill `window.u2f` (e.g., due to an existing `window.u2f` object or browser incompatibility).fixEnsure the `u2f-api-polyfill` script is correctly imported or included in your HTML before attempting to access `window.u2f`. Check the browser's console for any loading errors. Verify that the browser environment (e.g., Chrome) is one where the polyfill is expected to activate.
Warnings
- breaking Version 0.4.2 removed the implicit dependency on jQuery. Applications that relied on jQuery being present in the global scope after importing this polyfill may experience errors. Ensure jQuery is explicitly loaded if still required.
- gotcha The FIDO U2F API, and thus this polyfill, is largely superseded by the WebAuthn API, which offers a more modern, flexible, and secure approach to strong authentication. New implementations should prefer WebAuthn.
- gotcha This polyfill is designed to load only if `window.u2f` is undefined, typically in older Chrome environments. If another U2F implementation is already present or if the browser natively supports `window.u2f`, the polyfill might not activate or could potentially conflict.
- gotcha The polyfill primarily targets Chrome and older Edge versions, as indicated by its original scope. Compatibility with other browsers or very recent browser versions that have moved to WebAuthn may vary or be limited.
Install
-
npm install u2f-api-polyfill -
yarn add u2f-api-polyfill -
pnpm add u2f-api-polyfill
Imports
- window.u2f
import { u2f } from 'u2f-api-polyfill';import 'u2f-api-polyfill'; // Then, use the global object: // window.u2f.register(...)
Quickstart
import 'u2f-api-polyfill';
const appId = 'https://your-domain.com'; // Replace with your application's origin
const registerRequests = [{
version: 'U2F_V2',
challenge: 'challenge_string_for_registration',
appId: appId
}];
const signRequests = [{
version: 'U2F_V2',
challenge: 'challenge_string_for_authentication',
appId: appId,
keyHandle: 'base64_encoded_key_handle' // obtained during registration
}];
if (window.u2f) {
console.log('U2F API is available.');
// Example registration call
window.u2f.register(
registerRequests,
[], // Sign requests (optional for registration)
(response) => {
if (response.errorCode) {
console.error('U2F Registration Error:', response);
return;
}
console.log('U2F Registration Success:', response);
// Send response to your server for verification
}
);
// Example sign call (after successful registration)
// window.u2f.sign(
// signRequests,
// (response) => {
// if (response.errorCode) {
// console.error('U2F Sign Error:', response);
// return;
// }
// console.log('U2F Sign Success:', response);
// // Send response to your server for verification
// }
// );
} else {
console.warn('U2F API not available. This polyfill might not have loaded or browser does not support it.');
}