forked from hbolimovsky/webauthn-example
-
Notifications
You must be signed in to change notification settings - Fork 1
/
user.go
83 lines (67 loc) · 1.91 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
72
73
74
75
76
77
78
79
80
81
82
83
package main
import (
"crypto/rand"
"encoding/binary"
"github.com/duo-labs/webauthn/protocol"
"github.com/duo-labs/webauthn/webauthn"
)
// User represents the user model
type User struct {
id uint64
name string
displayName string
credentials []webauthn.Credential
}
// NewUser creates and returns a new User
func NewUser(name string, displayName string) *User {
user := &User{}
user.id = randomUint64()
user.name = name
user.displayName = displayName
// user.credentials = []webauthn.Credential{}
return user
}
func randomUint64() uint64 {
buf := make([]byte, 8)
rand.Read(buf)
return binary.LittleEndian.Uint64(buf)
}
// WebAuthnID returns the user's ID
func (u User) WebAuthnID() []byte {
buf := make([]byte, binary.MaxVarintLen64)
binary.PutUvarint(buf, uint64(u.id))
return buf
}
// WebAuthnName returns the user's username
func (u User) WebAuthnName() string {
return u.name
}
// WebAuthnDisplayName returns the user's display name
func (u User) WebAuthnDisplayName() string {
return u.displayName
}
// WebAuthnIcon is not (yet) implemented
func (u User) WebAuthnIcon() string {
return ""
}
// AddCredential associates the credential to the user
func (u *User) AddCredential(cred webauthn.Credential) {
u.credentials = append(u.credentials, cred)
}
// WebAuthnCredentials returns credentials owned by the user
func (u User) WebAuthnCredentials() []webauthn.Credential {
return u.credentials
}
// CredentialExcludeList returns a CredentialDescriptor array filled
// with all the user's credentials
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
}