{"id":16255,"library":"u2f-api-polyfill","title":"U2F API Polyfill","description":"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.","status":"maintenance","version":"0.4.4","language":"javascript","source_language":"en","source_url":"https://github.com/mastahyeti/u2f-api","tags":["javascript","fido","u2f"],"install":[{"cmd":"npm install u2f-api-polyfill","lang":"bash","label":"npm"},{"cmd":"yarn add u2f-api-polyfill","lang":"bash","label":"yarn"},{"cmd":"pnpm add u2f-api-polyfill","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This package is a polyfill that modifies the global `window.u2f` object upon import or script execution. There are no named or default exports from the package itself. For CommonJS, use `require('u2f-api-polyfill');` for the same side-effect.","wrong":"import { u2f } from 'u2f-api-polyfill';","symbol":"window.u2f","correct":"import 'u2f-api-polyfill';\n// Then, use the global object:\n// window.u2f.register(...)"}],"quickstart":{"code":"import 'u2f-api-polyfill';\n\nconst appId = 'https://your-domain.com'; // Replace with your application's origin\nconst registerRequests = [{\n  version: 'U2F_V2',\n  challenge: 'challenge_string_for_registration',\n  appId: appId\n}];\nconst signRequests = [{\n  version: 'U2F_V2',\n  challenge: 'challenge_string_for_authentication',\n  appId: appId,\n  keyHandle: 'base64_encoded_key_handle' // obtained during registration\n}];\n\nif (window.u2f) {\n  console.log('U2F API is available.');\n\n  // Example registration call\n  window.u2f.register(\n    registerRequests,\n    [], // Sign requests (optional for registration)\n    (response) => {\n      if (response.errorCode) {\n        console.error('U2F Registration Error:', response);\n        return;\n      }\n      console.log('U2F Registration Success:', response);\n      // Send response to your server for verification\n    }\n  );\n\n  // Example sign call (after successful registration)\n  // window.u2f.sign(\n  //   signRequests,\n  //   (response) => {\n  //     if (response.errorCode) {\n  //       console.error('U2F Sign Error:', response);\n  //       return;\n  //     }\n  //     console.log('U2F Sign Success:', response);\n  //     // Send response to your server for verification\n  //   }\n  // );\n} else {\n  console.warn('U2F API not available. This polyfill might not have loaded or browser does not support it.');\n}","lang":"javascript","description":"Demonstrates how to include the `u2f-api-polyfill` and then interact with the global `window.u2f` object for FIDO U2F registration. This code would typically run in a browser environment."},"warnings":[{"fix":"If your application implicitly relied on jQuery being loaded by `u2f-api-polyfill`, ensure you include jQuery as a separate dependency and load it before `u2f-api-polyfill` if needed.","message":"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.","severity":"breaking","affected_versions":">=0.4.2"},{"fix":"For new applications, consider migrating to or implementing WebAuthn for FIDO authentication. This polyfill is primarily for maintaining compatibility with legacy U2F deployments.","message":"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.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Ensure that `u2f-api-polyfill` is loaded in an environment where `window.u2f` is genuinely absent and that no other U2F polyfills or scripts are conflicting. Test thoroughly across target browsers.","message":"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.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Verify compatibility with your specific target browser versions. For broader or modern browser support, rely on WebAuthn or a polyfill explicitly designed for it.","message":"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.","severity":"gotcha","affected_versions":">=0.1.0"}],"env_vars":null,"last_verified":"2026-04-21T00:00:00.000Z","next_check":"2026-07-20T00:00:00.000Z","problems":[{"fix":"Ensure 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.","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).","error":"Uncaught ReferenceError: u2f is not defined"}],"ecosystem":"npm"}