Search code examples
node.jsqr-codegoogle-authenticator

What data do I have to use to generate a QR code for Google Authenticator?


I want to utilize the Google Authenticator app for login purposes in our application.

I'm using speakeasy to generate the base data for the authentication. It can also spit out a URL to a Google website that generates a QR code which I can scan with Google Authenticator to set up the scheme.

I want to generate the QR code myself, mainly because I want to display it in the console using qrcode-terminal.

What data do I have to encode in the QR code to make it work?


Solution

  • The string you have to encode is:

    otpauth://totp/ApplicationName?secret= + key.base32
    
    • ApplicationName is the name of your application that you want to have displayed in Google Authenticator.

    Your implementation would look something like this:

    var key = speakeasy.generate_key( {length : 20} );
    qrcode.generate( "otpauth://totp/foo?secret=" + key.base32, function( qrcode ) {
      console.log( qrcode );
    } );
    

    There's also official documentation available on the format.