feat: new recovery codes

This commit is contained in:
Hintay 2025-02-09 23:36:28 +09:00
parent 3738bebca7
commit 0d1f56a43e
No known key found for this signature in database
GPG key ID: 120FC7FF121F2F2D
24 changed files with 1882 additions and 713 deletions

View file

@ -1,19 +1,33 @@
package model
import (
"time"
"github.com/go-webauthn/webauthn/webauthn"
"github.com/spf13/cast"
"gorm.io/gorm"
)
type RecoveryCode struct {
Code string `json:"code"`
UsedTime *time.Time `json:"used_time,omitempty" gorm:"type:datetime;default:null"`
}
type RecoveryCodes struct {
Codes []RecoveryCode `json:"codes"`
LastViewed *time.Time `json:"last_viewed,omitempty" gorm:"type:datetime;default:null"`
LastDownloaded *time.Time `json:"last_downloaded,omitempty" gorm:"type:datetime;default:null"`
}
type User struct {
Model
Name string `json:"name" cosy:"add:max=20;update:omitempty,max=20;list:fussy;db_unique"`
Password string `json:"-" cosy:"json:password;add:required,max=20;update:omitempty,max=20"`
Status bool `json:"status" gorm:"default:1"`
OTPSecret []byte `json:"-" gorm:"type:blob"`
EnabledTwoFA bool `json:"enabled_2fa" gorm:"-"`
Name string `json:"name" cosy:"add:max=20;update:omitempty,max=20;list:fussy;db_unique"`
Password string `json:"-" cosy:"json:password;add:required,max=20;update:omitempty,max=20"`
Status bool `json:"status" gorm:"default:1"`
OTPSecret []byte `json:"-" gorm:"type:blob"`
RecoveryCodes RecoveryCodes `json:"-" gorm:"serializer:json"`
EnabledTwoFA bool `json:"enabled_2fa" gorm:"-"`
}
type AuthToken struct {
@ -35,6 +49,14 @@ func (u *User) EnabledOTP() bool {
return len(u.OTPSecret) != 0
}
func (u *User) RecoveryCodeGenerated() bool {
return len(u.RecoveryCodes.Codes) > 0
}
func (u *User) RecoveryCodeViewed() bool {
return u.RecoveryCodes.LastViewed != nil
}
func (u *User) EnabledPasskey() bool {
var passkeys Passkey
db.Where("user_id", u.ID).Limit(1).Find(&passkeys)