npm

@twilio/twilio-verify-passkeys-web
TypeScript icon, indicating that this package has built-in type declarations

0.0.2 • Public • Published

Twilio Verify Passkeys Web

Table of content

About

Twilio Passkeys Web SDK enables developers to easily add Passkeys into their existing authentication flows within their own web applications. The Verify Passkeys SDK supports passkeys creation and authentication using the FIDO/WebAuthn industry standard.

Documentation

Verify Passkeys Overview

Requirements

  • Node.js v20.x or higher

Installation

  • Add the library to your project

Using NPM

npm install @twilio/twilio-verify-passkeys-web

Directly from Github

npm install https://github.com/twilio/twilio-verify-passkeys-web.git

Using it from CDN

<script src="https://unpkg.com/@twilio/twilio-verify-passkeys-web@0.0.1/dist/twilio-verify-passkeys.iife.js"></script>

Quickstart

Create registration

Use the TwilioPasskeys instance to create a registration by calling the create(String) function.

The create method receive a param that represent a challenge payload, check how to create challenge payload.

You can also call create(CreatePasskeysRequest), where the CreatePasskeysRequest is wrapper object of a creation challenge payload schema.

const createPasskeysResult = twilioPasskeys.create(challengePayload)

if(createPasskeysResult.Error !== null) {
    // verify the createPasskeyResult.createPasskeyResponse against your backend and finish sign up
} else {
    // handle error
}

Authenticate a user

Use the TwilioPasskeys instance to authenticate a user by calling the authenticate(String) function.

The authenticate method receive a param that represent an authentication request, it follows the schema of an authentication challenge payload.

You can also call authenticate(AuthenticatePasskeysRequest), where the AuthenticatePasskeysRequest is a wrapper object of an authentication challenge payload.

const authenticatePasskeyResult = twilioPasskeys.authenticate(authenticationRequest)

if(authenticatePasskeyResult.Error !== null) {
    // verify the authenticatePasskeyResult.authenticatePasskeyResponse against your backend
} else {
    // handle error
}

Create Challenge Payload

The challenge payload for creating a registration is a String obtained by requesting your backend a challenge for registering a user, it uses the JSON schema:

{"rp":{"id":"your_backend","name":"PasskeySample"},"user":{"id":"WUV...5Ng","name":"1234567890","displayName":"1234567890"},"challenge":"WUY...jZQ","pubKeyCredParams":[{"type":"public-key","alg":-7}],"timeout":600000,"excludeCredentials":[],"authenticatorSelection":{"authenticatorAttachment":"platform","requireResidentKey":false,"residentKey":"preferred","userVerification":"preferred"},"attestation":"none"}

Authenticate Challenge Payload

The challenge payload for authenticating a user is a JSON with the schema:

{"publicKey":{"challenge":"WUM...2Mw","timeout":300000,"rpId":"your_backend","allowCredentials":[],"userVerification":"preferred"}}

Building and Running Sample App

Requirements

  • ngrok

Steps

  1. Clone this repository.
  2. Install the dependencies using
npm install
  1. Make sure you have setup your backend
  2. Go to the config.js file inside the sample-app folder and replace the backend url with your backend url
  3. Run the following command to build and run a local server with the sample app
npm run demo
  1. The previous step will open a browser tap with the localhost server running at port 8080, run ngrok for that port:
ngrok http http://localhost:8080
  1. Open the sample app in your browser using the url that ngrok provides: https://example.ngrok.app/sample-app/

Backend side configurations for sample app

  1. Setup a backend throught the function template of passkeys-backend

  2. Make sure you already added support for digital asset links, this should be inside a file called assetlinks.json in your backend, check whether an entry with the following structure:

{
    "relation":[
        "delegate_permission/common.get_login_creds",
        "delegate_permission/common.handle_all_urls"
    ],
    "target":{
        "namespace": "web",
        "site": "https://example.ngrok.app"
    }
},
  1. Add the ngrok url to the enviroment variables in the field RP_DOMAIN if you are using just this sample app.
RP_DOMAIN=example.ngrok.app
  1. Add the ngrok url to the enviroment variable called ORIGINS, using a full url format.
ORIGINS=https://example.ngrok.app

Project Structure

  • src: Contains all the main code, including business logic, data models, and utility functions.
  • sample-app: This folder contains a simple demo application code that use html and vanilla javascript.

Code Structure

Main code

The src folder constains all the core code. This includes:

  • Data models (src/models)
  • Buisness logic (src/TwilioPasskeys.js)
  • Utility functions (src/utils)

Sample App

The sample-app folder contains a simple demo app code. This includes:

  • Vanilla javascript code that works as code snippets for integrating with the Twilio Verify Passkeys SDK

  • HTML code that works as UI implementation

Running Tests

Running all tests

npm run test

Running test coverage

npm run coverage

Package Sidebar

Install

npm i @twilio/twilio-verify-passkeys-web

Weekly Downloads

4

Version

0.0.2

License

Apache-2.0

Unpacked Size

96.2 kB

Total Files

11

Last publish

Collaborators

  • twilio-kyc
  • twilio-authy-and-sdks