mirror of
https://github.com/0xJacky/nginx-ui.git
synced 2025-05-11 02:15:48 +02:00
feat: login via passkey
This commit is contained in:
parent
44c3180df7
commit
bdfbbd0e8f
37 changed files with 1529 additions and 643 deletions
|
@ -1,8 +1,12 @@
|
|||
package model
|
||||
|
||||
import "gorm.io/gorm"
|
||||
import (
|
||||
"github.com/go-webauthn/webauthn/webauthn"
|
||||
"github.com/spf13/cast"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Auth struct {
|
||||
type User struct {
|
||||
Model
|
||||
|
||||
Name string `json:"name"`
|
||||
|
@ -18,11 +22,36 @@ type AuthToken struct {
|
|||
ExpiredAt int64 `json:"expired_at" gorm:"default:0"`
|
||||
}
|
||||
|
||||
func (u *Auth) AfterFind(tx *gorm.DB) error {
|
||||
u.Enabled2FA = u.EnabledOTP()
|
||||
return nil
|
||||
func (u *User) TableName() string {
|
||||
return "auths"
|
||||
}
|
||||
|
||||
func (u *Auth) EnabledOTP() bool {
|
||||
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) WebAuthnID() []byte {
|
||||
return []byte(cast.ToString(u.ID))
|
||||
}
|
||||
|
||||
func (u *User) WebAuthnName() string {
|
||||
return u.Name
|
||||
}
|
||||
|
||||
func (u *User) WebAuthnDisplayName() string {
|
||||
return u.Name
|
||||
}
|
||||
|
||||
func (u *User) WebAuthnCredentials() (credentials []webauthn.Credential) {
|
||||
var passkeys []Passkey
|
||||
db.Where("user_id", u.ID).Find(&passkeys)
|
||||
for _, passkey := range passkeys {
|
||||
credentials = append(credentials, *passkey.Credential)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
|
|
@ -25,7 +25,7 @@ type Model struct {
|
|||
func GenerateAllModel() []any {
|
||||
return []any{
|
||||
ConfigBackup{},
|
||||
Auth{},
|
||||
User{},
|
||||
AuthToken{},
|
||||
Cert{},
|
||||
ChatGPTLog{},
|
||||
|
@ -37,6 +37,7 @@ func GenerateAllModel() []any {
|
|||
AcmeUser{},
|
||||
BanIP{},
|
||||
Config{},
|
||||
Passkey{},
|
||||
}
|
||||
}
|
||||
|
||||
|
|
13
model/passkey.go
Normal file
13
model/passkey.go
Normal file
|
@ -0,0 +1,13 @@
|
|||
package model
|
||||
|
||||
import "github.com/go-webauthn/webauthn/webauthn"
|
||||
|
||||
type Passkey struct {
|
||||
Model
|
||||
|
||||
Name string `json:"name"`
|
||||
UserID int `json:"user_id"`
|
||||
RawID string `json:"raw_id"`
|
||||
Credential *webauthn.Credential `json:"-" gorm:"serializer:json"`
|
||||
LastUsedAt int64 `json:"last_used_at" gorm:"default:0"`
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue