DEV Community

Zaw Htut Win
Zaw Htut Win

Posted on

Key not recognized in rotp rails gem when scanning with Google Authenticator

Yesterday I was updating my rails project with "bundle update" and rotp gem goes haywired. The previous otp_secret of User(Devise Model) is something like this

{\"p\":\"7+cOhwxYt9UJxLgaMBnY0K1vJI0LS2vT\",\"h\":{\"iv\":..."

and after updating to rotp 6.3.0, the Google Authenticator didn't recognized the generated QR code anymore.

It is because in the newer version the otp_secret must only be the string, no json or whatsoever. Something like this

"7+cOhwxYt9UJxLgaMBnY0K1vJI0LS2vT"

The above string can be generated using class method called

User.generate_otp_secret
Enter fullscreen mode Exit fullscreen mode

Following is the way to return svg to show in the view

    def qrcode
        issuer = "My Application Name"

        # Generate a TOTP provisioning URI with the issuer
        totp = ROTP::TOTP.new(current_user.otp_secret, issuer: issuer)
        provisioning_uri = totp.provisioning_uri(current_user.email)
        qrcode = RQRCode::QRCode.new(provisioning_uri)

        @svg = qrcode.as_svg(
          offset: 0,
          color: '000',
          shape_rendering: 'crispEdges',
          module_size: 3,
          use_path: true,
          file: nil
        )   

    end

Enter fullscreen mode Exit fullscreen mode

Then you can show the svg in the view easily

    <div class="text-center">
      <p>Scan the Below QR Code with Google Authenticator or Microsoft Authenticator.</p></br>
      <%= raw @svg %>
  </div>
Enter fullscreen mode Exit fullscreen mode

Top comments (0)