-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession.go
59 lines (49 loc) · 1.21 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
58
59
package webauthn
import (
"crypto/rand"
"encoding/base64"
"errors"
"sync"
"time"
)
// DefaultSessionStore provides an in-memory implementation of SessionStore
type DefaultSessionStore struct {
mu sync.RWMutex
sessions map[string]*SessionData
}
func NewDefaultSessionStore() SessionStore {
return &DefaultSessionStore{
sessions: make(map[string]*SessionData),
}
}
func (s *DefaultSessionStore) StoreSession(sessionID string, data *SessionData) error {
s.mu.Lock()
defer s.mu.Unlock()
s.sessions[sessionID] = data
return nil
}
func (s *DefaultSessionStore) GetSession(sessionID string) (*SessionData, error) {
s.mu.RLock()
defer s.mu.RUnlock()
session, exists := s.sessions[sessionID]
if !exists {
return nil, errors.New("session not found")
}
if time.Now().After(session.ExpiresAt) {
delete(s.sessions, sessionID)
return nil, errors.New("session expired")
}
return session, nil
}
func (s *DefaultSessionStore) DeleteSession(sessionID string) error {
s.mu.Lock()
defer s.mu.Unlock()
delete(s.sessions, sessionID)
return nil
}
// generateSessionID creates a random session ID
func generateSessionID() string {
b := make([]byte, 32)
rand.Read(b)
return base64.URLEncoding.EncodeToString(b)
}