Skip to content

Instantly share code, notes, and snippets.

@ytensor42
Last active November 4, 2018 07:52
Show Gist options
  • Save ytensor42/3c0c183525810f22421915b848697ebd to your computer and use it in GitHub Desktop.
Save ytensor42/3c0c183525810f22421915b848697ebd to your computer and use it in GitHub Desktop.
MFA using speakeasy
# https://www.npmjs.com/package/speakeasy
const speakeasy = require('speakeasy');
// Generate Key
var secret = speakeasy.generateSecret();
// secret sample
{ ascii: '6v[qFO^%vfqL%C,Kdv*oCqTGZ0z^p]vU',
hex: '36765b71464f5e257666714c25432c4b64762a6f437154475a307a5e705d7655',
base32: 'GZ3FW4KGJ5PCK5TGOFGCKQZMJNSHMKTPINYVIR22GB5F44C5OZKQ',
otpauth_url: 'otpauth://totp/SecretKey?secret=GZ3FW4KGJ5PCK5TGOFGCKQZMJNSHMKTPINYVIR22GB5F44C5OZKQ' }
// Timebase Token
var token = speakeasy.totp({
secret: secret.base32,
encoding: 'base32'
});
// Token Validation
var tokenValidates = speakeasy.totp.verify({
secret: secret.base32,
encoding: 'base32',
token: <token value>,
window: 2
});
// QR Code generation using qrcode package
// npm install --save qrcode
var QRCode = require('qrcode');
QRCode.toDataURL(secret.otpauth_url, function(err, data_url) {
console.log(data_url);
// Display this data URL to the user in an <img> tag
write('<img src="' + data_url + '">');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment