feat: 2fa via passkey

This commit is contained in:
Jacky 2024-09-16 11:18:14 +08:00
parent bdfbbd0e8f
commit 0a6a7693a1
No known key found for this signature in database
GPG key ID: 215C21B10DF38B4D
21 changed files with 384 additions and 218 deletions

View file

@ -3,17 +3,15 @@ package model
import (
"github.com/go-webauthn/webauthn/webauthn"
"github.com/spf13/cast"
"gorm.io/gorm"
)
type User struct {
Model
Name string `json:"name"`
Password string `json:"-"`
Status bool `json:"status" gorm:"default:1"`
OTPSecret []byte `json:"-" gorm:"type:blob"`
Enabled2FA bool `json:"enabled_2fa" gorm:"-"`
Name string `json:"name"`
Password string `json:"-"`
Status bool `json:"status" gorm:"default:1"`
OTPSecret []byte `json:"-" gorm:"type:blob"`
}
type AuthToken struct {
@ -26,15 +24,20 @@ func (u *User) TableName() string {
return "auths"
}
func (u *User) AfterFind(_ *gorm.DB) error {
u.Enabled2FA = u.EnabledOTP()
return nil
}
func (u *User) EnabledOTP() bool {
return len(u.OTPSecret) != 0
}
func (u *User) EnabledPasskey() bool {
var passkeys Passkey
db.Where("user_id", u.ID).First(&passkeys)
return passkeys.ID != 0
}
func (u *User) Enabled2FA() bool {
return u.EnabledOTP() || u.EnabledPasskey()
}
func (u *User) WebAuthnID() []byte {
return []byte(cast.ToString(u.ID))
}