-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
57 lines (44 loc) · 1.05 KB
/
session.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
package main
import (
"crypto/rand"
"encoding/hex"
"fmt"
"sync"
"github.com/go-webauthn/webauthn/webauthn"
)
type sessiondb struct {
sessions map[string]*webauthn.SessionData
mu sync.RWMutex
}
var sessionDb *sessiondb = &sessiondb{
sessions: make(map[string]*webauthn.SessionData),
}
func (db *sessiondb) GetSession(sessionID string) (*webauthn.SessionData, error) {
db.mu.Lock()
defer db.mu.Unlock()
session, ok := db.sessions[sessionID]
if !ok {
return nil, fmt.Errorf("error getting session '%s': does not exist", sessionID)
}
return session, nil
}
func (db *sessiondb) DeleteSession(sessionID string) {
db.mu.Lock()
defer db.mu.Unlock()
delete(db.sessions, sessionID)
}
func (db *sessiondb) StartSession(data *webauthn.SessionData) string {
db.mu.Lock()
defer db.mu.Unlock()
id, _ := random(32)
db.sessions[id] = data
return id
}
func random(length int) (string, error) {
randomData := make([]byte, length)
_, err := rand.Read(randomData)
if err != nil {
return "", err
}
return hex.EncodeToString(randomData), nil
}