-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
134 lines (120 loc) · 2.77 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"log"
"net/http"
"sync"
"time"
"github.com/google/uuid"
)
const SessionCookieName = "Session"
const SessionMaxDiration = 15 * time.Minute
var counterLock = &sync.Mutex{}
const SessionsCleanupPeriod uint32 = 10
var SessionsCleanupPeriodCounter uint32 = 0
type SessionData struct {
Data map[string]interface{}
LastOperation time.Time
}
type Sessions struct {
sessions map[string]*SessionData
}
var sessions *Sessions
var lock = &sync.Mutex{}
func Session() *Sessions {
if sessions == nil {
lock.Lock()
defer lock.Unlock()
if sessions == nil {
sessions = &Sessions{
sessions: make(map[string]*SessionData),
}
}
}
return sessions
}
func (s *Sessions) Get(r *http.Request) (string, *SessionData) {
counterLock.Lock()
SessionsCleanupPeriodCounter++
if SessionsCleanupPeriodCounter > SessionsCleanupPeriod {
s.Expire()
SessionsCleanupPeriodCounter = 0
}
counterLock.Unlock()
var cookie *http.Cookie
for _, c := range r.Cookies() {
if c.Name == SessionCookieName {
cookie = c
break
}
}
if cookie == nil {
return "", nil
}
sessionID := cookie.Value
session := s.sessions[sessionID]
if session == nil {
log.Printf("Session not found: %s", sessionID)
return "", nil
}
threshold := time.Now().Add(-SessionMaxDiration)
if session.LastOperation.Before(threshold) {
lock.Lock()
delete(s.sessions, sessionID)
lock.Unlock()
return "", nil
}
session.LastOperation = time.Now()
return sessionID, session
}
func (s *Sessions) Start(w http.ResponseWriter) {
uuid := uuid.New().String()
newSessionData := &SessionData{
Data: make(map[string]interface{}),
LastOperation: time.Now(),
}
lock.Lock()
s.sessions[uuid] = newSessionData
lock.Unlock()
cookie := http.Cookie{
Name: SessionCookieName,
Value: uuid,
Path: "/",
Secure: true,
HttpOnly: true,
SameSite: http.SameSiteStrictMode,
MaxAge: 60 * 60 * 24,
// Expires: time.Now().Add(SessionMaxDiration), this limits session duration. Could help if each
// https://www.sohamkamani.com/golang/session-cookie-authentication/ - recreating session IDs each request
}
http.SetCookie(w, &cookie)
}
func (s *Sessions) End(w http.ResponseWriter, r *http.Request) {
id, session := s.Get(r)
if session == nil {
return
}
cookie := http.Cookie{
Name: SessionCookieName,
Value: id,
Path: "/webui/pages",
Secure: true,
HttpOnly: true,
SameSite: http.SameSiteStrictMode,
MaxAge: -1,
}
http.SetCookie(w, &cookie)
lock.Lock()
delete(s.sessions, id)
lock.Unlock()
}
func (s *Sessions) Expire() {
threshold := time.Now().Add(-SessionMaxDiration)
lock.Lock()
defer lock.Unlock()
for id, session := range s.sessions {
if session.LastOperation.After(threshold) {
continue
}
delete(s.sessions, id)
}
}