- Remove legacy project restriction schema and helpers - Install Python test extras in CI - Update the quality report status
107 lines
3.0 KiB
Go
107 lines
3.0 KiB
Go
package auth
|
|
|
|
import (
|
|
"crypto/hmac"
|
|
"crypto/rand"
|
|
"crypto/sha1"
|
|
"crypto/subtle"
|
|
"encoding/base32"
|
|
"encoding/binary"
|
|
"fmt"
|
|
"net/url"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// TOTP follows RFC 6238's default interoperable profile.
|
|
const (
|
|
TOTPPeriod = 30 * time.Second
|
|
TOTPDigits = 6
|
|
TOTPSecretBytes = 20
|
|
TOTPWindow = 1
|
|
)
|
|
|
|
// GenerateTOTPSecret returns a base32 secret suitable for authenticator apps.
|
|
func GenerateTOTPSecret() (string, error) {
|
|
raw := make([]byte, TOTPSecretBytes)
|
|
if _, err := rand.Read(raw); err != nil {
|
|
return "", fmt.Errorf("generate TOTP secret: %w", err)
|
|
}
|
|
return base32.StdEncoding.WithPadding(base32.NoPadding).EncodeToString(raw), nil
|
|
}
|
|
|
|
// TOTPProvisioningURI builds the otpauth URI accepted by common authenticator apps.
|
|
func TOTPProvisioningURI(issuer, account, secret string) string {
|
|
issuer = strings.TrimSpace(issuer)
|
|
account = strings.TrimSpace(account)
|
|
secret = normalizeTOTPSecret(secret)
|
|
label := account
|
|
if issuer != "" {
|
|
label = issuer + ":" + account
|
|
}
|
|
query := url.Values{}
|
|
query.Set("secret", secret)
|
|
if issuer != "" {
|
|
query.Set("issuer", issuer)
|
|
}
|
|
query.Set("algorithm", "SHA1")
|
|
query.Set("digits", strconv.Itoa(TOTPDigits))
|
|
query.Set("period", strconv.Itoa(int(TOTPPeriod/time.Second)))
|
|
return "otpauth://totp/" + url.PathEscape(label) + "?" + query.Encode()
|
|
}
|
|
|
|
// 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 -1, false
|
|
}
|
|
key, err := base32.StdEncoding.WithPadding(base32.NoPadding).DecodeString(secret)
|
|
if err != nil || len(key) == 0 {
|
|
return -1, false
|
|
}
|
|
period := now.Unix() / int64(TOTPPeriod/time.Second)
|
|
// 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
|
|
}
|
|
var counter [8]byte
|
|
binary.BigEndian.PutUint64(counter[:], uint64(period+offset))
|
|
hash := hmac.New(sha1.New, key)
|
|
_, _ = hash.Write(counter[:])
|
|
digest := hash.Sum(nil)
|
|
index := digest[len(digest)-1] & 0x0f
|
|
value := (uint32(digest[index])&0x7f)<<24 |
|
|
uint32(digest[index+1])<<16 |
|
|
uint32(digest[index+2])<<8 |
|
|
uint32(digest[index+3])
|
|
candidate := fmt.Sprintf("%06d", value%1000000)
|
|
if subtle.ConstantTimeCompare([]byte(candidate), []byte(token)) == 1 {
|
|
return period + offset, true
|
|
}
|
|
}
|
|
return -1, false
|
|
}
|
|
|
|
func normalizeTOTPSecret(value string) string {
|
|
return strings.TrimRight(strings.ReplaceAll(strings.ToUpper(strings.TrimSpace(value)), " ", ""), "=")
|
|
}
|
|
|
|
func normalizeTOTPToken(value string) string {
|
|
value = strings.TrimSpace(value)
|
|
if len(value) > TOTPDigits {
|
|
return ""
|
|
}
|
|
for _, character := range value {
|
|
if character < '0' || character > '9' {
|
|
return ""
|
|
}
|
|
}
|
|
return value
|
|
}
|