🔏 A small WebAuthn API wrapper that translates to/from pure JSON using base64url.
Перейти к файлу
Brian Kungl 3d7767b575
Merge pull request #83 from github/dependabot/npm_and_yarn/ip-1.1.9
Bump ip from 1.1.8 to 1.1.9
2024-05-03 09:27:43 -04:00
.github/workflows Update to use latest npm CLI release 2023-02-15 17:20:42 +00:00
.vscode Avoid formatting `REAMDE.md` with an undesired newline. 2022-07-18 11:44:46 -07:00
browser-ponyfill Split the `browser-ponyfill` into regular and extended like the main build. 2022-06-30 02:45:43 -07:00
extended Add `/extended` for tools that don't understand `export` (in `package.json`). 2022-01-11 15:29:20 -08:00
script Split the `browser-ponyfill` into regular and extended like the main build. 2022-06-30 02:45:43 -07:00
src Add a `conditional-mediation-prompt-on-load` URL param to the demo. 2023-03-06 15:00:33 -08:00
.gitignore Use temp dir for dev server build dir. 2022-01-11 14:14:23 -08:00
CODE_OF_CONDUCT.md Initial implementation. 2019-08-02 03:04:45 -04:00
LICENSE.md Initial implementation. 2019-08-02 03:04:45 -04:00
Makefile Remove vestigial `make format-bin`. 2022-01-19 02:48:04 -08:00
README.md Update link for JSON deserialization methods in the spec. 2023-03-27 11:49:28 -07:00
compat.md Add more compat info. 2023-03-21 13:12:35 -07:00
package-lock.json Bump ip from 1.1.8 to 1.1.9 2024-02-22 04:38:47 +00:00
package.json Update TypeScript. 2023-03-20 19:05:53 -07:00
rome.json Update to `rome` stable. 2022-11-14 17:47:45 -08:00
tsconfig-types.json Fix up types. 2022-01-11 13:44:22 -08:00
tsconfig.json Update `tsconfig.json` to `es2019`, to match the the target we use for ESM builds with `esbuild`. 2022-11-14 17:47:45 -08:00
web-test-runner.config.js Update tests. 2023-01-30 20:38:56 -08:00

README.md

@github/webauthn-json

@github/webauthn-json is a client-side Javascript library that serves as convenience wrapper for the the WebAuthn API by encoding binary data using base64url (also known as "websafe" or "urlsafe" base64).

The WebAuthn API itself takes input and output values that look almost like JSON, except that binary data is represented as ArrayBuffers. Using webauthn-json allows the data to be sent from/to the server as normal JSON without any custom client-side processing. This will be possible directly in the browser some day, but we're here for you until then.

Usage

  1. Replace calls:
  • navigator.credentials.create(...) with create(parseCreationOptionsFromJSON(...)).
  • navigator.credentials.get(...) with get(parseRequestOptionsFromJSON(...)).
  1. Encode/decode binary values on the server as base64url.

Example

Install using:

npm install --save @github/webauthn-json

Then:

import {
  create,
  parseCreationOptionsFromJSON,
} from "@github/webauthn-json/browser-ponyfill";

const request = fetch("...");

async function createCredential() {
  const json = await (await request).json();
  const options = parseCreationOptionsFromJSON(json);
  const response = await create(options);
  fetch("...", {
    method: "POST",
    body: JSON.stringify(response),
  });
}

See here for fully working client-side demo code.

API (browser ponyfill)

We now recommend using a ponyfill for the new JSON-based APIs in the WebAuthn spec:

// @github/webauthn-json/browser-ponyfill

function supported(): boolean;

function parseCreationOptionsFromJSON(json: JSON): CredentialCreationOptions;
function parseRequestOptionsFromJSON(json: JSON): CredentialRequestOptions;

// You can call `.toJSON()` on the result or pass directly to `JSON.stringify()`.
function create(options: CredentialCreationOptions): Promise<PublicKeyCredential>;
// You can call `.toJSON()` on the result or pass directly to `JSON.stringify()`.
function get(options: CredentialRequestOptions): Promise<PublicKeyCredential>;

API (main library)

This was the original simplified API, which remains supported.

// @github/webauthn-json

function create(requestJSON: JSON): Promise<JSON>;
function get(requestJSON: JSON): Promise<JSON>;
function supported(): boolean;

Schema

There are are several ways to encode JSON with binary fields. @github/webauthn-json focuses on one simple approach: converting the known structure using a simple (custom) schema format. @github/webauthn-json uses a few tricks for a compact schema encoding: the main build is about ≈1KB when minified and gzipped (although we publish unminified builds).

Right now, we only convert fields explicitly known to be used by the WebAuthn API. This means that you'll have to update to a newer version of this library if you want to use new fields in the future.

To print the current schema, run:

npx @github/webauthn-json schema

Extensions

Modern browsers generally only support — and most sites only need to use — a small number of extensions. To save code size, @github/webauthn-json only includes the following extensions by default:

In addition, we handle the following info (that is not technically part of extensions):

If you need to convert additional input or output extensions, use either of the following:

  • createExtended() and getExtended() from @github/webauthn-json/extended.
  • parseExtendedCreationOptionsFromJSON() and parseExtendedRequestOptionsFromJSON() from @github/webauthn-json/browser-ponyfill/extended.

Contributions

The scope of @github/webauthn-json is fairly small — it's essentially feature-complete. However, we're happy to accept issues or pull requests that address the core goal of the project!


  1. This comes from getTransports() on the AuthenticatorAttestationResponse. Note that we don't include its three sibling functions (getAuthenticatorData(), getPublicKey(), and getPublicKeyAlgorithm()), since they duplicates information that is available in other parts of the response. In particular, the authenticator data is available inside the signed attestation object. ↩︎