Files
pocket-id-pocket-id/backend/internal/model/user.go
2024-08-23 17:04:19 +02:00

68 lines
1.5 KiB
Go

package model
import (
"github.com/go-webauthn/webauthn/protocol"
"github.com/go-webauthn/webauthn/webauthn"
"time"
)
type User struct {
Base
Username string
Email string
FirstName string
LastName string
IsAdmin bool
Credentials []WebauthnCredential
}
func (u User) WebAuthnID() []byte { return []byte(u.ID) }
func (u User) WebAuthnName() string { return u.Username }
func (u User) WebAuthnDisplayName() string { return u.FirstName + " " + u.LastName }
func (u User) WebAuthnIcon() string { return "" }
func (u User) WebAuthnCredentials() []webauthn.Credential {
credentials := make([]webauthn.Credential, len(u.Credentials))
for i, credential := range u.Credentials {
credentials[i] = webauthn.Credential{
ID: []byte(credential.CredentialID),
AttestationType: credential.AttestationType,
PublicKey: credential.PublicKey,
Transport: credential.Transport,
Flags: webauthn.CredentialFlags{
BackupState: credential.BackupState,
BackupEligible: credential.BackupEligible,
},
}
}
return credentials
}
func (u User) WebAuthnCredentialDescriptors() (descriptors []protocol.CredentialDescriptor) {
credentials := u.WebAuthnCredentials()
descriptors = make([]protocol.CredentialDescriptor, len(credentials))
for i, credential := range credentials {
descriptors[i] = credential.Descriptor()
}
return descriptors
}
type OneTimeAccessToken struct {
Base
Token string
ExpiresAt time.Time
UserID string
User User
}