-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession.go
74 lines (62 loc) · 1.61 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import (
"time"
"sync"
"bytes"
)
type TimeMinutes int64
const (
API_TIMEOUT TimeMinutes = 10
ACTIVATION_TIMEOUT TimeMinutes = (24 * 60)
)
type Session struct {
token []byte
expTimeUnix int64
}
type SessionManager struct{
mut sync.RWMutex
userSessions map[string]Session
}
func (sm *SessionManager) Init() () {
sm.mut.Lock()
defer sm.mut.Unlock()
sm.userSessions = make(map[string]Session)
sm.startCleanup()
}
func (sm *SessionManager) startCleanup() () {
go func() {
for true {
now := time.Now().Unix()
sm.mut.RLock()
for user, sess := range sm.userSessions {
if now > sess.expTimeUnix {
sm.mut.RUnlock()
sm.mut.Lock()
delete(sm.userSessions, user)
sm.mut.Unlock()
sm.mut.RLock()
}
}
sm.mut.RUnlock()
// clean up sessions every 15 seconds
time.Sleep(15 * time.Second)
}
}()
}
func (sm *SessionManager) RenewSession(user string, token []byte, timeout TimeMinutes) () {
sm.mut.Lock()
defer sm.mut.Unlock()
tmrLength := time.Now().Unix() + (int64(timeout) * 60)
sess := Session{token, tmrLength}
sm.userSessions[user] = sess
}
func (sm *SessionManager) IsTokenValid(user string, token []byte) (ret bool) {
sm.mut.RLock()
defer sm.mut.RUnlock()
if sess, exists := sm.userSessions[user]; exists == true {
if bytes.Equal(sess.token, token) {
ret = true
}
}
return ret
}