Created
June 9, 2018 02:21
-
-
Save miguelmota/8eda8e256c749e0243c8421edfdcb125 to your computer and use it in GitHub Desktop.
Node.js speakeasy MFA example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import speakeasy from 'speakeasy' | |
export function generateSecret() { | |
return speakeasy.generateSecret({ | |
length: 32, | |
symbols: false | |
}).base32 | |
} | |
export function generateAuthUrl({username, secret}) { | |
return speakeasy.otpauthURL({ secret: secret, label: username, issuer: 'CoinCircle', algorithm: 'sha1', encoding: 'base32' }); | |
} | |
export function verify(mfaSecret, mfaCode) { | |
mfaCode = (mfaCode||'').replace(/[^\d]+/gi, '') | |
return speakeasy.totp.verify({ | |
secret: mfaSecret, | |
encoding: 'base32', | |
token: mfaCode, | |
window: 2, | |
digits: 6, | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment