Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OTP Generated by Google Authenticator not validated by the Library's OTP #94

Open
Cprime50 opened this issue Jul 2, 2024 · 2 comments

Comments

@Cprime50
Copy link

Cprime50 commented Jul 2, 2024

I am using this library to generate a QR code that I connect to Google Authenticator. However, the OTP generated by Google Authenticator doesn't get validated by the library.

version of package

github.com/pquerna/otp v1.4.0

go version

go version go1.22.4 linux/amd64

code:

package main

import (
	"fmt"
	"image/png"
	"log"
	"os"

	"github.com/pquerna/otp"
	"github.com/pquerna/otp/totp"
)

func main() {
	key, err := totp.Generate(totp.GenerateOpts{
		Issuer:      "localhost:8000",
		AccountName: "[email protected]",
		SecretSize:  32,
	})
	if err != nil {
		log.Fatalf("Failed to generate OTP: %v", err)
	}

	qrImage, err := key.Image(256, 256)
	if err != nil {
		log.Fatalf("Failed to generate QR code image: %v", err)
	}
	file, err := os.Create("otp_qrcode.png")
	if err != nil {
		log.Fatalf("Failed to create file for QR code: %v", err)
	}
	defer file.Close()
	if err := png.Encode(file, qrImage); err != nil {
		log.Fatalf("Failed to save QR code image: %v", err)
	}
	fmt.Println("QR code generated and saved as otp_qrcode.png")

	fmt.Print("Enter the OTP code from your authenticator app: ")
	var otpCode string
	fmt.Scanln(&otpCode)

	valid := totp.Validate(otpCode, key.Secret())
	if valid {
		fmt.Println("OTP verified successfully!")
	} else {
		fmt.Println("Invalid OTP code.")
	}
}

to reproduce:
Run the code provided above.
Scan the generated QR code using Google Authenticator.
Enter the OTP code displayed in Google Authenticator into the terminal.

@KEINOS
Copy link

KEINOS commented Aug 25, 2024

I can not reproduce in my environment and all the authenticator apps below returns as success, "OTP verified successfully!"

  • Authenticators
    • Android 9 (Kernel 4.9.148)
      • Google Authenticator 6.0 ... OK
      • Microsoft Authenticator 6.2407.5108 ... OK
    • iPhone (iOS 17.6.1)
      • Google Authenticator 4.2.1 ... OK
      • Microsoft Authenticator 6.8.14 ... OK
  • Code executed via macOS, Linux (Alpine, Ubuntu via Docker) @ go1.23.0
  • Package: github.com/pquerna/otp v1.4.0

In my experience, the time zone and/or NTP are not set correctly in such cases.

@the-real-i9
Copy link

the-real-i9 commented Sep 7, 2024

Hi, @Cprime50. I reproduced your code and it worked for me.

Go version (Latest)

go version go1.23.0 linux/amd64

I don't think this issue has anything to do with Go's version.

Package version

github.com/pquerna/otp v1.4.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants