You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When the encoding is done with base32, the TOTP token fails (incorrectly)
Reproduction Steps
importspeakeasyfrom'speakeasy'importqrcodefrom'qrcode'varsecret=speakeasy.generateSecret();constotpAuthUrl=speakeasy.otpauthURL({secret: secret.base_32,label: 'teemo@yordel.org',issuer: 'Yordel Inc',algorithm: 'sha1',digits: 6,});constqrCodeImage=awaitqrcode.toDataURL(otpAuthUrl);// Now scan the qrCodeImage to Google Authenticator and get the OTP and assign to `otp`varotp='123123'// get it from Google AuthenticatorconstotpIsValid=speakeasy.totp.verify({secret: secret.base_32,encoding: 'base32',algorithm: 'sha1',token: otp,window: 1,});console.log(otpIsValid)// returns false
Now, contrast this the ascii encoding, which does work
importspeakeasyfrom'speakeasy'importqrcodefrom'qrcode'varsecret=speakeasy.generateSecret();constotpAuthUrl=speakeasy.otpauthURL({secret: secret.ascii,label: 'teemo@yordel.org',issuer: 'Yordel Inc',algorithm: 'sha1',digits: 6,});constqrCodeImage=awaitqrcode.toDataURL(otpAuthUrl);// Now scan the qrCodeImage to Google Authenticator and get the OTP and assign to `otp`varotp='123123'// get it from Google AuthenticatorconstotpIsValid=speakeasy.totp.verify({secret: secret.ascii,encoding: 'ascii',algorithm: 'sha1',token: otp,window: 1,});console.log(otpIsValid)// returns true
The text was updated successfully, but these errors were encountered:
I found a way to still use speakeasy for my TFA feature using base32 encoding (due to legacy reasons).
Instead of relying on speakeasy to generate the secret and otpAuthUrl, you generate these using a combo of crypto, base32 and url.
importcryptofrom'crypto'importbase32from'hi-base32'importurlfrom'url'constgenerateBase32Secret=()=>{varrandomBytes=crypto.randomBytes(20)constbase32String=base32.encode(randomBytes);returnbase32String}constbase32Secret=generateBase32Secret()constgenerateOtpAuthUrl=(base32Secret,label,issuer,digits)=>{constquery={secret: base32Secret,issuer: issuer,digits: digits,}returnurl.format({protocol: 'otpauth',slashes: true,hostname: 'totp',pathname: label,query: query})}constotpAuthUrl=generateOtpAuthUrl(base32Secret,'teemo@yordel.org','TopLane',6)constqrCode=awaitqrcode.toDataURL(otpAuthUrl);// Now scanning the qrCode and adding this new entry in an authenticator app, you can use speakeasy for verifying it.varotp='466719'// get this from your authenticator appconstotpIsValid=speakeasy.totp.verify({secret: base32Secretencoding: 'base32',token: otp,window: 1,});console.log(otpIsValid)// true
Uh oh!
There was an error while loading. Please reload this page.
When the encoding is done with base32, the TOTP token fails (incorrectly)
Reproduction Steps
Now, contrast this the ascii encoding, which does work
The text was updated successfully, but these errors were encountered: