# webauthn-lib

> An easy way to use webauthn in your website

Latest version **1.0.0** (published 2022-06-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install webauthn-lib
pnpm add webauthn-lib
yarn add webauthn-lib
bun add webauthn-lib
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2022-06-24 |
| First published | 2022-06-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 22.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | CodyNotFound |
| Maintainers | codynotfound |

## Links

- npm: https://www.npmjs.com/package/webauthn-lib
- Repository: https://github.com/CodyNotFound/webauthn-lib
- Homepage: https://github.com/CodyNotFound/webauthn-lib#readme
- Issues: https://github.com/CodyNotFound/webauthn-lib/issues
- npm.io page: https://npm.io/package/webauthn-lib

## Dependencies (3)

- [cbor](https://npm.io/package/cbor.md) ^8.1.0
- [crypto](https://npm.io/package/crypto.md) ^1.0.1
- [base64url](https://npm.io/package/base64url.md) ^3.0.1

## Recent versions

- 1.0.0 (latest) — 2022-06-24

## README

# webauthn-lib
An easy way to use webauthn in your website

## Usage
1. Install the module
```bash
yarn add webauthn-lib
```

2. Import the module
```
const webauthn = require('webauthn-lib')
```

3. Import `front.js` at the front end
```html
<script src="front.js"></script>
```

### Registration (Backend)
> Generate a makeCredential request
```js
const webauthn = require('webauthn-lib');

let username = 'CodyNotFound';
let displayname = 'Cody';
let id = webauthn.randomBase64URLBuffer();
let request = webauthn.generateServerMakeCredRequest(username, displayname, id);

console.log(request);
```

### Authentication (Backend)
> Generate a makeCredential request
```js
const webauthn = require('webauthn-lib');

// Some code here to get user's authenticators
let username = 'CodyNotFound';
let authenticators = database[username].authenticators;
let request = webauthn.generateServerGetAssertion(authenticators);

console.log(request);
```

### Validation (Backend)
> Verify the response
```js
const webauthn = require('webauthn-lib');

// Some code here to get user's response and authenticators
let response;
let username = 'CodyNotFound';
let authenticators = database[username].authenticators;

// Session should contains the challengeID and username
let session;

let result;
if (response.response.attestationObject !== undefined) {
    // Register
    result = utils.verifyAuthenticatorAttestationResponse(response);
    if (result.verified) {
        // Register success
        // You can do something here
        authenticators.push(response.authrInfo);
    }
} else if (webauthnResp.response.authenticatorData !== undefined) {
    // Authenticate
    result = utils.verifyAuthenticatorAssertionResponse(webauthnResp, authenticators);
    if (result.verified) {
        // Authenticate success
        // You can do something here
        session.login = true;
    }
}
```

# GetResponse (Frontend)
> You should do it after getting the request from the server
```html
<script src="front.js"></script>
<script>
    // When login
    // data is the request from server
    let data;
    let publicKey = preformatGetAssertReq(data);
    navigator.credentials.get({ publicKey }).then((data) => {
        let response = publicKeyCredentialToJSON(data);
        // Send the response to server
    });

    // When Register
    let data;
    let publicKey = preformatMakeCredReq(data);
    navigator.credentials.get({ publicKey }).then((data) => {
        let response = publicKeyCredentialToJSON(data);
        // Send the response to server
    });
</script>
```

> **See more at the demo app**

## Demo App
```bash
git clone https://github.com/CodyNotFound/webauthn-lib
cd webauthn-lib
yarn
cd example
node index.js
```
Then you can see the demo app at [http://localhost:4000](http://localhost:4000)

## Other
This module is edited from [webauthn-demo](https://github.com/fido-alliance/webauthn-demo)

---
_Source: https://npm.io/package/webauthn-lib · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
