panel/models/models.go

81 lines
1.8 KiB
Go
Raw Normal View History

2024-11-03 21:33:08 +01:00
package models
import (
"github.com/go-webauthn/webauthn/protocol"
"github.com/go-webauthn/webauthn/webauthn"
"github.com/uptrace/bun"
)
type User struct {
bun.BaseModel `bun:"table:users"`
ID string `bun:",pk"`
Username string
Email string
Hash string
2024-12-20 23:29:45 +01:00
FirstName string `bun:",nullzero"`
2024-11-03 21:33:08 +01:00
LastName string `bun:",nullzero"`
DisplayName string `bun:",nullzero"`
LoginMethods []*UserLoginMethod `bun:"rel:has-many,join:id=user_id"`
Logs []*UserLog `bun:"rel:has-many,join:id=user_id"`
}
type UserLoginMethod struct {
bun.BaseModel `bun:"table:users_login_methods"`
ID int64 `bun:",pk,autoincrement"`
UserID string
Type string
Name string
WebAuthn *webauthn.Credential `bun:",nullzero,type:jsonb"`
}
func (u User) WebAuthnID() []byte {
return []byte(u.ID)
}
func (u User) WebAuthnName() string {
return u.Username
}
func (u User) WebAuthnDisplayName() string {
if len(u.DisplayName) != 0 {
return u.DisplayName
} else {
return u.FirstName + " " + u.LastName
}
}
func (u User) WebAuthnCredentials() []webauthn.Credential {
var credentials []webauthn.Credential
for _, l := range u.LoginMethods {
if l.WebAuthn != nil {
credentials = append(credentials, *l.WebAuthn)
}
}
return credentials
}
func (u User) WebAuthnCredentialExcludeList() []protocol.CredentialDescriptor {
var excludeList []protocol.CredentialDescriptor
webauthnCreds := u.WebAuthnCredentials()
for _, cred := range webauthnCreds {
descriptor := protocol.CredentialDescriptor{
Type: protocol.PublicKeyCredentialType,
CredentialID: cred.ID,
}
excludeList = append(excludeList, descriptor)
}
return excludeList
}
type UserLog struct {
bun.BaseModel `bun:"table:user_logs"`
ID int64 `bun:",pk,autoincrement"`
UserID int64 `bun:",pk"`
}