summaryrefslogtreecommitdiff
path: root/app/models/user.go
blob: 8145aedc1b92a2f8afaba9cc394e12956f34c976 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package models

import (
	"context"
	"time"

	"github.com/go-webauthn/webauthn/webauthn"
	"golang.org/x/exp/slices"
)

type User struct {
	Username         string `bson:"_id"`
	DisplayName      string
	AllowedHosts     []string
	Fido2Credentials []webauthn.Credential
	Deleted          *time.Time
}

var _ webauthn.User = (*User)(nil)

func (u *User) RecordId() string {
	return u.Username
}

func (u *User) MarkDeleted(t time.Time) {
	u.Deleted = &t
}

func (u *User) WebAuthnID() []byte {
	return []byte(u.Username)
}

func (u *User) WebAuthnName() string {
	return u.Username
}

func (u *User) WebAuthnDisplayName() string {
	return u.DisplayName
}

func (u *User) WebAuthnCredentials() []webauthn.Credential {
	return u.Fido2Credentials
}

func (u *User) WebAuthnIcon() string {
	return ""
}

func (u *User) AuthorizedForHost(host string) bool {
	return slices.Contains(u.AllowedHosts, host)
}

type UserStore interface {
	List(ctx context.Context) ([]*User, error)
	ListAll(ctx context.Context) ([]*User, error)
	Get(ctx context.Context, name string) (*User, error)
	Upsert(ctx context.Context, m *User) error
	Delete(ctx context.Context, m *User) error
}