U2F API Polyfill

0.4.4 · maintenance · verified Tue Apr 21

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

Warnings

Install

Imports

Quickstart

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.

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.');
}

view raw JSON →