104 lines
2.8 KiB
Go
104 lines
2.8 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
|
||
|
|
TOTPSecretLength = 32
|
||
|
|
)
|
||
|
|
|
||
|
|
// 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.
|
||
|
|
func VerifyTOTP(secret, token string, now time.Time) bool {
|
||
|
|
secret = normalizeTOTPSecret(secret)
|
||
|
|
token = normalizeTOTPToken(token)
|
||
|
|
if secret == "" || len(token) != TOTPDigits {
|
||
|
|
return false
|
||
|
|
}
|
||
|
|
key, err := base32.StdEncoding.WithPadding(base32.NoPadding).DecodeString(secret)
|
||
|
|
if err != nil || len(key) == 0 {
|
||
|
|
return false
|
||
|
|
}
|
||
|
|
period := now.Unix() / int64(TOTPPeriod/time.Second)
|
||
|
|
for offset := int64(-TOTPWindow); offset <= TOTPWindow; offset++ {
|
||
|
|
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 true
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return 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
|
||
|
|
}
|