2020-02-01 19:54:50 +07:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
2020-04-05 19:37:21 +07:00
|
|
|
"time"
|
|
|
|
|
2020-03-25 08:48:20 +07:00
|
|
|
"github.com/pquerna/otp"
|
2020-02-01 19:54:50 +07:00
|
|
|
"github.com/pquerna/otp/totp"
|
|
|
|
)
|
|
|
|
|
2020-04-21 04:03:38 +07:00
|
|
|
// TOTPVerifier is the interface for verifying TOTPs.
|
2020-02-01 19:54:50 +07:00
|
|
|
type TOTPVerifier interface {
|
2020-03-25 08:48:20 +07:00
|
|
|
Verify(token, secret string) (bool, error)
|
2020-02-01 19:54:50 +07:00
|
|
|
}
|
|
|
|
|
2020-04-21 04:03:38 +07:00
|
|
|
// TOTPVerifierImpl the production implementation for TOTP verification.
|
2020-03-25 08:48:20 +07:00
|
|
|
type TOTPVerifierImpl struct {
|
|
|
|
Period uint
|
|
|
|
Skew uint
|
|
|
|
}
|
2020-02-01 19:54:50 +07:00
|
|
|
|
2020-04-21 04:03:38 +07:00
|
|
|
// Verify verifies TOTPs.
|
2020-03-25 08:48:20 +07:00
|
|
|
func (tv *TOTPVerifierImpl) Verify(token, secret string) (bool, error) {
|
|
|
|
opts := totp.ValidateOpts{
|
|
|
|
Period: tv.Period,
|
|
|
|
Skew: tv.Skew,
|
|
|
|
Digits: otp.DigitsSix,
|
|
|
|
Algorithm: otp.AlgorithmSHA1,
|
|
|
|
}
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2020-03-25 08:48:20 +07:00
|
|
|
return totp.ValidateCustom(token, secret, time.Now().UTC(), opts)
|
2020-02-01 19:54:50 +07:00
|
|
|
}
|