docs: record auth hash alignment and validation results
This commit is contained in:
1 parent
22887fef03
commit
f7819904b9
51 files changed
+1792
-542
No files matched your search
+10
-7
@@ -52,19 +52,22 @@ func TOTPProvisioningURI(issuer, account, secret string) string {
|
||||
return "otpauth://totp/" + url.PathEscape(label) + "?" + query.Encode()
|
||||
}
|
||||
|
||||
// VerifyTOTP checks a six-digit token with a one-period clock-skew window.
|
||||
func VerifyTOTP(secret, token string, now time.Time) bool {
|
||||
// VerifyTOTP checks a six-digit token with a one-period clock-skew window and
|
||||
// returns the matching time step. A caller must atomically record that step
|
||||
// before accepting the code so it cannot be used twice.
|
||||
func VerifyTOTP(secret, token string, now time.Time) (int64, bool) {
|
||||
secret = normalizeTOTPSecret(secret)
|
||||
token = normalizeTOTPToken(token)
|
||||
if secret == "" || len(token) != TOTPDigits {
|
||||
return false
|
||||
return -1, false
|
||||
}
|
||||
key, err := base32.StdEncoding.WithPadding(base32.NoPadding).DecodeString(secret)
|
||||
if err != nil || len(key) == 0 {
|
||||
return false
|
||||
return -1, false
|
||||
}
|
||||
period := now.Unix() / int64(TOTPPeriod/time.Second)
|
||||
for offset := int64(-TOTPWindow); offset <= TOTPWindow; offset++ {
|
||||
// Prefer the current step if a six-digit collision occurs in the window.
|
||||
for _, offset := range [...]int64{0, -1, 1} {
|
||||
if period+offset < 0 {
|
||||
continue
|
||||
}
|
||||
@@ -80,10 +83,10 @@ func VerifyTOTP(secret, token string, now time.Time) bool {
|
||||
uint32(digest[index+3])
|
||||
candidate := fmt.Sprintf("%06d", value%1000000)
|
||||
if subtle.ConstantTimeCompare([]byte(candidate), []byte(token)) == 1 {
|
||||
return true
|
||||
return period + offset, true
|
||||
}
|
||||
}
|
||||
return false
|
||||
return -1, false
|
||||
}
|
||||
|
||||
func normalizeTOTPSecret(value string) string {
|
||||
|
||||
Reference in new issue
Block a user