feat(auth): implement TOTP setup and management
Lint / Lint (Node/Svelte) (push) Successful in 24s
Lint / Lint (Go) (push) Successful in 28s
Lint / Lint (Python) (push) Failing after 14s
Tests / Test (Node/Svelte) (push) Successful in 18s
Tests / Test (Go) (push) Successful in 25s
Tests / Test (Python) (push) Successful in 12s
Lint / Lint (Node/Svelte) (push) Successful in 24s
Lint / Lint (Go) (push) Successful in 28s
Lint / Lint (Python) (push) Failing after 14s
Tests / Test (Node/Svelte) (push) Successful in 18s
Tests / Test (Go) (push) Successful in 25s
Tests / Test (Python) (push) Successful in 12s
- Added TOTP support for two-factor authentication in the auth module. - Introduced new types and interfaces for TOTP setup and token actions in the web UI. - Created backend logic for generating and verifying TOTP secrets and tokens. - Implemented user management features including role assignments and user CRUD operations. - Enhanced UI components for managing users and TOTP settings. - Added styles for optional labels and authentication reset links. - Implemented reset code generation and validation for admin accounts.
This commit is contained in:
1 parent
2f57554154
commit
da97fd4fb1
30 files changed
+2145
-27
No files matched your search
@@ -0,0 +1,104 @@
|
||||
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
|
||||
}
|
||||
Reference in new issue
Block a user