-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
85 lines (64 loc) · 1.76 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
84
85
package model
import (
"crypto/rand"
"encoding/binary"
"log"
"strconv"
"gorm.io/gorm"
"github.com/go-webauthn/webauthn/protocol"
"github.com/go-webauthn/webauthn/webauthn"
)
// Struct that implements the webauthn user interface to store valid credentials
type User struct {
gorm.Model
ID string `gorm:"primaryKey"`
Username string
IsAdmin bool
Credentials []Credential
}
func (u User) WebAuthnID() []byte {
userID, err := strconv.ParseUint(u.ID, 10, 64)
if err != nil {
log.Fatal("Unable to parse userID", err)
}
webauthnID := make([]byte, 8)
binary.LittleEndian.PutUint64(webauthnID, userID)
return webauthnID
}
func (u User) WebAuthnName() string {
return u.Username
}
func (u User) WebAuthnDisplayName() string {
return u.Username
}
func (u User) WebAuthnIcon() string {
return ""
}
func (u User) WebAuthnCredentials() []webauthn.Credential {
credentials := make([]webauthn.Credential, len(u.Credentials))
for _, credential := range u.Credentials {
credentials = append(credentials, credential.WebauthnCredential)
}
return credentials
}
func (u User) CredentialExclusionList() []protocol.CredentialDescriptor {
credentials := u.WebAuthnCredentials()
exclusionList := make([]protocol.CredentialDescriptor, len(credentials))
for _, credential := range credentials {
descriptor := protocol.CredentialDescriptor{
Type: protocol.PublicKeyCredentialType,
CredentialID: credential.ID,
}
exclusionList = append(exclusionList, descriptor)
}
return exclusionList
}
func GenerateUserID() string {
id := make([]byte, 8)
n, err := rand.Read(id)
if (err != nil) || (n != 8) {
log.Fatal("Unable to generate a random user id", err)
}
userID := binary.LittleEndian.Uint64(id)
return strconv.FormatUint(userID, 10)
}