-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
71 lines (57 loc) · 1.47 KB
/
user.go
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
60
61
62
63
64
65
66
67
68
69
70
71
package main
import (
"crypto/rand"
"encoding/binary"
"github.com/go-webauthn/webauthn/protocol"
"github.com/go-webauthn/webauthn/webauthn"
)
type User struct {
id uint64
username string
displayName string
credentials []webauthn.Credential
}
func NewUser(username string, displayName string) *User {
user := &User{}
user.id = randomUint64()
user.username = username
user.displayName = displayName
user.credentials = []webauthn.Credential{}
return user
}
func randomUint64() uint64 {
buf := make([]byte, 8)
rand.Read(buf)
return binary.LittleEndian.Uint64(buf)
}
func (u User) WebAuthnID() []byte {
buf := make([]byte, binary.MaxVarintLen64)
binary.PutUvarint(buf, uint64(u.id))
return buf
}
func (u User) WebAuthnName() string {
return u.username
}
func (u User) WebAuthnDisplayName() string {
return u.displayName
}
func (u User) WebAuthnCredentials() []webauthn.Credential {
return u.credentials
}
func (u User) WebAuthnIcon() string {
return ""
}
func (u *User) AddCredential(cred webauthn.Credential) {
u.credentials = append(u.credentials, cred)
}
func (u User) CredentialExcludeList() []protocol.CredentialDescriptor {
credentialExcludeList := []protocol.CredentialDescriptor{}
for _, cred := range u.credentials {
descriptor := protocol.CredentialDescriptor{
Type: protocol.PublicKeyCredentialType,
CredentialID: cred.ID,
}
credentialExcludeList = append(credentialExcludeList, descriptor)
}
return credentialExcludeList
}