npm install @rexfng/tfajavascript
const Tfa = require('@rexfng/tfa').init;
let TfaOptions = {
label: String, // username
//values below are optional
issuer: String, // issuer of tfa token (if null, it looks up value from APP_NAME environment variable)
algorithm: String, // example "SHA1", "SHA256" and "SHA512" (default to "SHA256") Also Supports. Do not use SHA1 as it is outdated and already broken into
digits: Integer, // 6 or 8 (default to 6)
period: Integer, // seconds to invalidate the code (default to 30)
secret: String // secret between user and server (note that user will be able to see this value when the code is produced) example: "NB2W45DFOIZB"
}
let tfa = new Tfa(TfaOptions);`Tfa().generate()
`javascript
let code = tfa.generate({
qrSize: "150" // value in pixel (default to "150", this option is optional)
})let token = code.token // 123412
let uri = code.uri // Google Authenticator key URI
let qr = code.qr // url of qrcode image
`Tfa().validate()
`javascript
let validation = tfa.validate({
token: Integer // 123412
})console.log(validation) // return true or false in Boolean
`Tfa Express Routers
| Base Endpoint | Method Example | HTTP Action |
|---------------|-------------|-------------|
| /api/getcode | app.use('/', Tfa.routes.api.getcode) | POST |
| /api/verifycode | app.use('/', Tfa.routes.email.verifycode) | POST |
| /sms/getcode | app.use('/', Tfa.routes.sms.getcode) | POST |
| /sms/verifycode | app.use('/', Tfa.routes.sms.verifycode) | POST |
| /email/getcode | app.use('/', Tfa.routes.email.getcode) | POST |
| /verification | app.use('/', Tfa.routes.email.verifycode) | GET |
$3
`javascript
{
issuer: String // default to process.env.APP_NAME,
label: String, // unique token identifier in alphabetical characters, no numbers allowed
period: Inteer, // period to expire the verification, default to 30,
digits: Integer //default to 6
}
`$3
`javascript
{
issuer: String // default to process.env.APP_NAME,
label: String, // unique token identifier in alphabetical characters, no numbers allowed
period: Inteer, period to expire the verification, default to 30,
digits: Integer //digits of the verification code, choose between 4-10 default to 6
code: String // verification code identified by "label"
}
`$3
`javascript
{
"phone_number": String, //"6047229494"
"country_code": String, //"1"
"code_length": Integer //4-10 default to 6
}
` $3
`javascript
{
"phone_number": String, //"6047229494"
"country_code": String, //"1"
"verification_code": String //"2421"
}
`$3
`javascript
{
from: "John", // sender address
to: "Paul", // list of receivers
subject: "Welcome Message", // Subject line
tpl: "Welcome to our service. Please verify with the following code {{code}}", // plain text or html
tpl: "Welcome to our service. Please verify with the following url: {{&url}}
", the template use for rendering the email body. {{&url}} or {{code}} will be replaced by verification_code or verification_url
label: "somerandomstring", //unique verifycode identifier, strings only
period: Integer, // 900
redirect_success: "https://your-app.com/redirect_success", || null, if empty, {{code}} will be used, if provided, {{&url}} will be used.
redirect_fail: "https://your-app.com/fail" || null if empty, {{code}} will be used, if provided, {{&url}} will be used.
}
``