-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
73 lines (58 loc) · 1.66 KB
/
store.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
package main
import (
"crypto/rand"
"encoding/base64"
"github.com/go-webauthn/webauthn/webauthn"
)
type InMem struct {
// TODO: it would be nice to have a mutex here
// TODO: use pointers to avoid copying
users map[string]PasskeyUser
sessions map[string]webauthn.SessionData
log Logger
}
func (i *InMem) GenSessionID() (string, error) {
b := make([]byte, 32)
_, err := rand.Read(b)
if err != nil {
return "", err
}
return base64.URLEncoding.EncodeToString(b), nil
}
func NewInMem(log Logger) *InMem {
return &InMem{
users: make(map[string]PasskeyUser),
sessions: make(map[string]webauthn.SessionData),
log: log,
}
}
func (i *InMem) GetSession(token string) (webauthn.SessionData, bool) {
i.log.Printf("[DEBUG] GetSession: %v", i.sessions[token])
val, ok := i.sessions[token]
return val, ok
}
func (i *InMem) SaveSession(token string, data webauthn.SessionData) {
i.log.Printf("[DEBUG] SaveSession: %s - %v", token, data)
i.sessions[token] = data
}
func (i *InMem) DeleteSession(token string) {
i.log.Printf("[DEBUG] DeleteSession: %v", token)
delete(i.sessions, token)
}
func (i *InMem) GetOrCreateUser(userName string) PasskeyUser {
i.log.Printf("[DEBUG] GetOrCreateUser: %v", userName)
if _, ok := i.users[userName]; !ok {
i.log.Printf("[DEBUG] GetOrCreateUser: creating new user: %v", userName)
i.users[userName] = &User{
ID: []byte(userName),
DisplayName: userName,
Name: userName,
}
}
return i.users[userName]
}
func (i *InMem) SaveUser(user PasskeyUser) {
i.log.Printf("[DEBUG] SaveUser: %v", user.WebAuthnName())
i.log.Printf("[DEBUG] SaveUser: %v", user)
i.users[user.WebAuthnName()] = user
}