Repository navigation
Expand file tree
/
Copy pathlocal_auth_timeout.go
More file actions
209 lines (180 loc) · 4.6 KB
/
Copy pathlocal_auth_timeout.go
File metadata and controls
209 lines (180 loc) · 4.6 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package kvm
import (
"fmt"
"net/http"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/coder/websocket"
"github.com/gin-gonic/gin"
"github.com/jetkvm/kvm/internal/hidrpc"
)
const (
localAuthMaxIdleMinutes = 24 * 60
localAuthExpirySweepInterval = 10 * time.Second
signalingStatusSessionExpired websocket.StatusCode = 4003
sessionExpiredCode = "session_expired"
)
var (
localAuthClock = time.Now
localAuthEpoch = time.Now()
localAuthLastActivity atomic.Int64
localAuthMu sync.Mutex
expiredLocalAuthToken string
)
type localAuthCookieState int
const (
localAuthCookieInvalid localAuthCookieState = iota
localAuthCookieValid
localAuthCookieExpired
)
func localAuthSinceEpoch() time.Duration {
return localAuthClock().Sub(localAuthEpoch)
}
func recordLocalAuthActivity() {
localAuthLastActivity.Store(int64(localAuthSinceEpoch()) + 1)
}
func localAuthIdleTimeout() time.Duration {
if config.LocalAuthMode != "password" {
return 0
}
return time.Duration(config.LocalAuthIdleMinutes) * time.Minute
}
func localAuthIdleLocked() bool {
timeout := localAuthIdleTimeout()
if timeout <= 0 || config.LocalAuthToken == "" {
return false
}
last := localAuthLastActivity.Load()
if last == 0 {
return true
}
return localAuthSinceEpoch()-time.Duration(last-1) >= timeout
}
func setLocalAuthToken(token string) {
localAuthMu.Lock()
defer localAuthMu.Unlock()
if token != "" {
recordLocalAuthActivity()
}
config.LocalAuthToken = token
}
func expireIdleLocalAuthLocked() bool {
if !localAuthIdleLocked() {
return false
}
expiredLocalAuthToken = config.LocalAuthToken
config.LocalAuthToken = ""
return true
}
func finishLocalAuthExpiry() {
logger.Info().Msg("local session expired after inactivity")
if err := SaveConfig(); err != nil {
logger.Warn().Err(err).Msg("failed to save config after the local session expired")
}
if s := currentSession; s != nil {
s.endForExpiredLogin()
}
}
func expireIdleLocalAuth() bool {
localAuthMu.Lock()
expired := expireIdleLocalAuthLocked()
localAuthMu.Unlock()
if expired {
finishLocalAuthExpiry()
}
return expired
}
func runLocalAuthExpirySweeper() {
ticker := time.NewTicker(localAuthExpirySweepInterval)
defer ticker.Stop()
for range ticker.C {
expireIdleLocalAuth()
}
}
func checkLocalAuthCookie(c *gin.Context) localAuthCookieState {
token, err := c.Cookie("authToken")
if err != nil || token == "" {
return localAuthCookieInvalid
}
localAuthMu.Lock()
if token == expiredLocalAuthToken {
localAuthMu.Unlock()
return localAuthCookieExpired
}
if token != config.LocalAuthToken {
localAuthMu.Unlock()
return localAuthCookieInvalid
}
expired := expireIdleLocalAuthLocked()
localAuthMu.Unlock()
if expired {
finishLocalAuthExpiry()
return localAuthCookieExpired
}
return localAuthCookieValid
}
func localSignalingTokenValid(token string) bool {
localAuthMu.Lock()
defer localAuthMu.Unlock()
if config.LocalAuthMode != "password" {
return true
}
return token != "" && token == config.LocalAuthToken && !localAuthIdleLocked()
}
func sendSessionExpiredThenAbort(c *gin.Context) {
c.SetCookie("authToken", "", -1, "/", "", false, true)
c.JSON(http.StatusUnauthorized, gin.H{"error": "Session expired", "code": sessionExpiredCode})
c.Abort()
}
func rpcCountsAsLocalAuthActivity(method string) bool {
for _, prefix := range []string{"get", "list", "check", "is"} {
if strings.HasPrefix(method, prefix) {
return false
}
}
return method != "ping"
}
func hidMessageCountsAsLocalAuthActivity(t hidrpc.MessageType) bool {
switch t {
case hidrpc.TypeKeyboardReport,
hidrpc.TypeKeypressReport,
hidrpc.TypePointerReport,
hidrpc.TypeMouseReport,
hidrpc.TypeWheelReport,
hidrpc.TypeKeyboardMacroReport,
hidrpc.TypeCancelKeyboardMacroReport:
return true
}
return false
}
func (s *Session) recordLocalAuthActivity() {
if s != nil && s.local {
recordLocalAuthActivity()
}
}
func (s *Session) endForExpiredLogin() {
if !s.local {
return
}
go func() {
if s.signaling != nil {
_ = s.signaling.Close(signalingStatusSessionExpired, "session expired")
}
_ = s.peerConnection.Close()
}()
}
func rpcGetLocalAuthSessionTimeout() int {
return config.LocalAuthIdleMinutes
}
func rpcSetLocalAuthSessionTimeout(timeoutMinutes int) error {
if timeoutMinutes < 0 || timeoutMinutes > localAuthMaxIdleMinutes {
return fmt.Errorf("session timeout must be between 0 and %d minutes", localAuthMaxIdleMinutes)
}
localAuthMu.Lock()
recordLocalAuthActivity()
config.LocalAuthIdleMinutes = timeoutMinutes
localAuthMu.Unlock()
return SaveConfig()
}