Probably the only JavaScript webauthn library you'll ever need.
@hexagon/webauthn
webauthn and webauthnurl to string or arraybuffer, and back. Works in Node, Deno or browser.
- Supports regular webauthn, as well as webauthnurl
- Convert to/from string or arraybuffer
- Works in Node.js >=4.0 (both require and import).
- Works in Deno >=1.16.
- Works in browsers as standalone, UMD or ES-module.
- Includes TypeScript typings.
Installation
Node.js
npm install @hexagon/webauthn --save
JavaScript
// ESM Import ...
import webauthn from "@hexagon/webauthn";
// ... or CommonJS Require
const webauthn = require("@hexagon/webauthn");
TypeScript
Note that only default export is available in Node.js TypeScript, as the commonjs module is used internally.
import webauthn from "@hexagon/webauthn";
// ...
Deno
JavaScript
import webauthn from "https://cdn.jsdelivr.net/gh/hexagon/webauthn@1/src/webauthn.js";
// ...
TypeScript
import { webauthn } from "https://cdn.jsdelivr.net/gh/hexagon/webauthn@1/src/webauthn.js";
// ...
Browser
Manual
- Download latest zipball
- Unpack
- Grab
webauthn.min.js
(UMD and standalone) orwebauthn.min.mjs
(ES-module) from the dist/ folder
CDN
To use as a UMD-module (stand alone, RequireJS etc.)
<script src="https://cdn.jsdelivr.net/npm/@hexagon/webauthn@1/dist/webauthn.min.js"></script>
To use as a ES-module
<script type="module">
import webauthn from "https://cdn.jsdelivr.net/npm/@hexagon/webauthn@/dist/webauthn.min.mjs";
// ... see usage section ...
</script>
Documentation
Full documentation available at hexagon.github.io/webauthn.
Examples
Assuming you have imported webauthn as described under 'Installation'.
// Encode string as regular webauthn
const example1enc = webauthn.fromString("Hellö Wörld, how are you doing today?!");
console.log(example1enc);
// > SGVsbMO2IFfDtnJsZCwgaG93IGFyZSB5b3UgZG9pbmcgdG9kYXk/IQ==
// Decode string as regular webauthn
const example1dec = webauthn.toString("SGVsbMO2IFfDtnJsZCwgaG93IGFyZSB5b3UgZG9pbmcgdG9kYXk/IQ==");
console.log(example1dec);
// > Hellö Wörld, how are you doing today?!
// Encode string as webauthnurl (setting the second parameter to true gives webauthnurl)
const example2enc = webauthn.fromString("Hellö Wörld, how are you doing today?!", true);
console.log(example2enc);
// > SGVsbMO2IFfDtnJsZCwgaG93IGFyZSB5b3UgZG9pbmcgdG9kYXk_IQ
// Decode string as webauthnurl (setting the second parameter to true takes webauthnurl)
const example2dec = webauthn.toString("SGVsbMO2IFfDtnJsZCwgaG93IGFyZSB5b3UgZG9pbmcgdG9kYXk_IQ", true);
console.log(example2dec);
// > Hellö Wörld, how are you doing today?!
Full API
The library encodes and decodes webauthn/webauthnurl to and from ArrayBuffers
-
fromArrayBuffer(buffer) - Encodes
ArrayBuffer
into webauthn string -
toArrayBuffer(str) - Decodes webauthn string to
ArrayBuffer
-
fromArrayBuffer(buffer, true) - Encodes
ArrayBuffer
into webauthnurl string -
toArrayBuffer(str, true) - Decodes webauthnurl string to
ArrayBuffer
-
fromString(str) - Encodes
String
into webauthn string -
toString(str) - Decodes webauthn string to
String
-
fromString(buffer, true) - Encodes
String
into webauthnurl string -
toString(str, true) - Decodes webauthnurl string to
String
Contributing
Contributors
The underlying code is loosely based on github.com/niklasvh/webauthn-arraybuffer
License
MIT