func main() { key, err := totp.Generate(totp.GenerateOpts{ Issuer: "Example.com", AccountName: "*****@*****.**", }) if err != nil { panic(err) } // Convert TOTP key into a PNG var buf bytes.Buffer img, err := key.Image(200, 200) if err != nil { panic(err) } png.Encode(&buf, img) // display the QR code to the user. display(key, buf.Bytes()) // Now Validate that the user's successfully added the passcode. fmt.Println("Validating TOTP...") passcode := promptForPasscode() valid := totp.Validate(passcode, key.Secret()) if valid { println("Valid passcode!") os.Exit(0) } else { println("Invalid passocde!") os.Exit(1) } }
func (o *OTP) Validate(password string) error { key, err := otp.NewKeyFromURL(o.URI) if err != nil { return err } if !totp.Validate(password, key.Secret()) { return ErrAccessDenied } return nil }