@twilio/twilio-verify-passkeys-web
v0.0.2
Published
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.
Downloads
5
Readme
Twilio Verify Passkeys Web
Table of content
- About
- Documentation
- Requirements
- Installation
- Quickstart
- Building and Running Sample App
- Project Structure
- Code Structure
- Running Tests
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
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/[email protected]/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
- Clone this repository.
- Install the dependencies using
npm install
- Make sure you have setup your backend
- Go to the
config.js
file inside thesample-app
folder and replace the backend url with your backend url - Run the following command to build and run a local server with the sample app
npm run demo
- 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
- 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
Setup a backend throught the function template of passkeys-backend
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"
}
},
- 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
- 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