2020-02-01 12:54:50 +00:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
2020-04-05 12:37:21 +00:00
|
|
|
"time"
|
|
|
|
|
2020-03-25 01:48:20 +00:00
|
|
|
"github.com/pquerna/otp"
|
2020-02-01 12:54:50 +00:00
|
|
|
"github.com/pquerna/otp/totp"
|
|
|
|
)
|
|
|
|
|
2020-04-20 21:03:38 +00:00
|
|
|
// TOTPVerifier is the interface for verifying TOTPs.
|
2020-02-01 12:54:50 +00:00
|
|
|
type TOTPVerifier interface {
|
2020-03-25 01:48:20 +00:00
|
|
|
Verify(token, secret string) (bool, error)
|
2020-02-01 12:54:50 +00:00
|
|
|
}
|
|
|
|
|
2020-04-20 21:03:38 +00:00
|
|
|
// TOTPVerifierImpl the production implementation for TOTP verification.
|
2020-03-25 01:48:20 +00:00
|
|
|
type TOTPVerifierImpl struct {
|
|
|
|
Period uint
|
|
|
|
Skew uint
|
|
|
|
}
|
2020-02-01 12:54:50 +00:00
|
|
|
|
2020-04-20 21:03:38 +00:00
|
|
|
// Verify verifies TOTPs.
|
2020-03-25 01:48:20 +00: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,
|
|
|
|
}
|
|
|
|
return totp.ValidateCustom(token, secret, time.Now().UTC(), opts)
|
2020-02-01 12:54:50 +00:00
|
|
|
}
|