-
Notifications
You must be signed in to change notification settings - Fork 224
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 verification failed when time delta is less than period #89
Comments
I adapted your code a little and ran the experiment. I was also able to detect the error, but not consistently. package main
import (
"log"
"time"
"github.com/pquerna/otp"
"github.com/pquerna/otp/totp"
)
func main() {
secret := "FI25AMFLRMYO7OMLI7A2PJOCWMNM6LIR"
now := time.Now()
passcode, err := totp.GenerateCodeCustom(secret, now, totp.ValidateOpts{
Period: 300,
})
if err != nil {
log.Println("error generating otp", err)
return
}
log.Printf("otp generated: %s", passcode)
// Validation at the same time as generation
valid, err := totp.ValidateCustom(passcode, secret, now, totp.ValidateOpts{
Digits: otp.DigitsSix,
Period: 300,
})
log.Printf("validated otp for generation time: %v", valid)
// Validation at a later time within the validity period
validationTime := now.Add(time.Second * 200)
valid, err = totp.ValidateCustom(passcode, secret, validationTime, totp.ValidateOpts{
Digits: otp.DigitsSix,
Period: 300,
})
if err != nil {
log.Println("error validating otp", err)
return
}
log.Printf("validated otp for validation time: %v", valid)
}
In the package main
import (
"log"
"time"
"github.com/pquerna/otp"
"github.com/pquerna/otp/totp"
)
var iterations []bool
func main() {
for i := 0; i < 100; i++ {
err, result := test()
if err != nil {
log.Printf("error in iteration %d: %v", i, err)
}
iterations = append(iterations, result)
}
log.Println(iterations)
}
func test() (error, bool) {
secret := "FI25AMFLRMYO7OMLI7A2PJOCWMNM6LIR"
now := time.Now()
passcode, err := totp.GenerateCodeCustom(secret, now, totp.ValidateOpts{
Period: 300,
})
if err != nil {
log.Println("error generating otp", err)
return err, false
}
// Validation at a later time within the validity period
validationTime := now.Add(time.Second * 200)
valid, err := totp.ValidateCustom(passcode, secret, validationTime, totp.ValidateOpts{
Digits: otp.DigitsSix,
Period: 300,
Skew: 1,
})
if err != nil {
log.Println("error validating otp", err)
return err, false
}
return nil, valid
}
|
Closed
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm not able to validate a TOTP even when time delta is less than period specified, lets say the OTP was generated on x timestamp then validating OTP on x+200 timestamp is failing even when period specified is 300.
The issue is not reproducible at go playground, there the validation is working fine, here's the go playground link : https://go.dev/play/p/xv8TM8pKlq6
This is the go version I'm using at my machine
library version
logs from my machine
logs from go-playground
The text was updated successfully, but these errors were encountered: