-
Notifications
You must be signed in to change notification settings - Fork 49
/
session.go
58 lines (48 loc) · 1.12 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
package keyshareserver
import (
"sync"
"time"
irma "github.com/privacybydesign/irmago"
)
type session struct {
KeyID irma.PublicKeyIdentifier // last used key, used in signing the issuance message
CommitID uint64
expiry time.Time
}
type sessionStore interface {
add(username string, session *session)
get(username string) *session
flush()
}
type memorySessionStore struct {
sync.Mutex
sessions map[string]*session
sessionLifetime time.Duration
}
func newMemorySessionStore(sessionLifetime time.Duration) sessionStore {
return &memorySessionStore{
sessionLifetime: sessionLifetime,
sessions: map[string]*session{},
}
}
func (s *memorySessionStore) add(username string, session *session) {
s.Lock()
defer s.Unlock()
session.expiry = time.Now().Add(s.sessionLifetime)
s.sessions[username] = session
}
func (s *memorySessionStore) get(username string) *session {
s.Lock()
defer s.Unlock()
return s.sessions[username]
}
func (s *memorySessionStore) flush() {
now := time.Now()
s.Lock()
defer s.Unlock()
for k, v := range s.sessions {
if now.After(v.expiry) {
delete(s.sessions, k)
}
}
}