/
webauthn_user.go
71 lines (58 loc) · 1.82 KB
/
webauthn_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 models
import (
"github.com/duo-labs/webauthn/protocol"
"github.com/duo-labs/webauthn/webauthn"
"github.com/gofrs/uuid"
)
// WebAuthNUser represents the user model for the webauthn package
type WebAuthNUser struct {
id uuid.UUID
name string
displayName string
credentials []webauthn.Credential
}
// NewWebAuthNUser creates and returns a new WebAuthNUser
func NewWebAuthNUser(id uuid.UUID, name string, displayName string) *WebAuthNUser {
user := WebAuthNUser{}
user.id = id
user.name = name
user.displayName = displayName
return &user
}
// WebAuthnID returns the user's ID
func (u WebAuthNUser) WebAuthnID() []byte {
return u.id.Bytes()
}
// WebAuthnName returns the user's username
func (u WebAuthNUser) WebAuthnName() string {
return u.name
}
// WebAuthnDisplayName returns the user's display name
func (u WebAuthNUser) WebAuthnDisplayName() string {
return u.displayName
}
// WebAuthnIcon is not (yet) implemented
func (u WebAuthNUser) WebAuthnIcon() string {
return ""
}
// AddCredential associates the credential to the user
func (u *WebAuthNUser) AddCredential(cred webauthn.Credential) {
u.credentials = append(u.credentials, cred)
}
// WebAuthnCredentials returns credentials owned by the user
func (u WebAuthNUser) WebAuthnCredentials() []webauthn.Credential {
return u.credentials
}
// CredentialExcludeList returns a CredentialDescriptor array filled
// with all the user's credentials
func (u WebAuthNUser) 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
}