Speakeasy
Speakeasy is a one-time passcode generator, ideal for use in two-factor authentication, that supports Google Authenticator and other two-factor devices.
It is well-tested and includes robust support for custom token lengths, authentication windows, hash algorithms like SHA256 and SHA512, and other features, and includes helpers like a secret key generator.
Speakeasy implements one-time passcode generators as standardized by the Initiative for Open Authentication (OATH). The HMAC-Based One-Time Password (HOTP) algorithm defined in RFC 4226 and the Time-Based One-time Password (TOTP) algorithm defined in RFC 6238 are supported. This project incorporates code from passcode, originally a fork of Speakeasy, and notp.
Two-Factor Usage
Let's say you have a user who wants to enable two-factor authentication, and you intend to do two-factor authentication using an app like Google Authenticator, Duo Security, Authy, etc. This is a three-step process:
- Generate a secret
- Show a QR code for the user to scan in
- Authenticate the token for the first time
Some Docs Links :
let's start with vs-code :
1. open vs code
2. create file: index.js & verify.js
3. open terminal end and enter the command for install speakeasy & node-qrcode packages
npm i speakeasy qrcode
4. into: index.js file, first require the speakeasy and then qrcode.
const speakeasy = require("speakeasy");
const qrcode = require("qrcode");
5. Generate secret key :
const secret = speakeasy.generate secret({
generate secret () is a method for generating a secret for a particular name. this name is shown in Google auth. app.
if you run this code and console.log(secret); then you show this :
there are 4 encoding types in this package and their value is different each time. these types are used as a secret.
ASCII, hex , base32, and base64
then we generate a QR code URL :
into index.js we type :
qrcode.toDataURL(secret.otpauth_url, (err, data) => {
console.log(data);
});
node-qrcode pkg is generated qrcode/2d-barcode, we print data, that is image link show in the below image :
this data url we paste in another file index.html and create into image tage src = "this data URL" & open the live file then we show a QR code. like this :
this qrcode is google auth. qr which use for scane and its genrate 6 digit tokan . so scan it and watch, the name: is first2FA, and the token is generated.
then move into the verify.js file or we can use the index.js file, but we use a different file for the verification process is done or note.
into verify.js :
const speakeasy = require("speakeasy");
let verification = speakeasy. top.verify({
secret: 'kkX[&C<h<iR](.3Xgzhu)1IyqgT)IE4T', // encoding type = ASCII and it's value ( QRcode )
encoding: "ASCII",
token: "714553", // in google auth. app token { 6 digits }
});
console.log(verification);
run it and check if it is true or false?
so that is done.
revise :
index.js:
const speakeasy = require("speakeasy");
const qrcode = require("qrcode");
const secret = speakeasy.generate secret({
name: "First2FA", // name shown in g. app
});
console.log(secret);
qrcode.toDataURL(secret.otpauth_url, (err, data) => {
console.log(data);
});
verify.js :
const speakeasy = require("speakeasy");
let verification = speakeasy. totp.verify({
secret: ":OZeaK9a5zusH#){uM]D3Otb#:x/z8kW",
encoding: "ASCII",
token: "504006",
});
console.log(verification);