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

Der Hochstapler picture Der Hochstapler · Feb 21, 2014 · Viewed 7.2k times · Source

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?

Answer

Der Hochstapler picture Der Hochstapler · Feb 21, 2014

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.