Repository navigation
Expand file tree
/
Copy pathhandlers_auth.go
More file actions
244 lines (208 loc) · 5.77 KB
/
Copy pathhandlers_auth.go
File metadata and controls
244 lines (208 loc) · 5.77 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package main
import (
"crypto/rand"
"crypto/sha256"
"crypto/subtle"
"encoding/hex"
"encoding/json"
"net/http"
"sync"
"time"
)
// session 存储
var (
sessions = make(map[string]sessionInfo)
sessionMu sync.RWMutex
)
type sessionInfo struct {
ExpiresAt time.Time
}
// generateToken 生成随机 session token
func generateToken() (string, error) {
b := make([]byte, 32)
if _, err := rand.Read(b); err != nil {
return "", err
}
return hex.EncodeToString(b), nil
}
// hashPassword SHA256 哈希(简单但有效)
func hashPassword(pwd string) string {
h := sha256.Sum256([]byte(pwd))
return hex.EncodeToString(h[:])
}
// cleanExpiredSessions 清理过期 session
func cleanExpiredSessions() {
sessionMu.Lock()
defer sessionMu.Unlock()
now := time.Now()
for token, s := range sessions {
if now.After(s.ExpiresAt) {
delete(sessions, token)
}
}
}
// authMiddleware 认证中间件,返回 http.Handler
func authMiddleware(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// 检查 cookie 中的 token
cookie, err := r.Cookie("nexusbox_token")
if err != nil || cookie.Value == "" {
writeJSONError(w, http.StatusUnauthorized, "未登录")
return
}
sessionMu.RLock()
s, ok := sessions[cookie.Value]
sessionMu.RUnlock()
if !ok || time.Now().After(s.ExpiresAt) {
writeJSONError(w, http.StatusUnauthorized, "登录已过期")
return
}
// 每次请求续期 24 小时
sessionMu.Lock()
if entry, exists := sessions[cookie.Value]; exists {
entry.ExpiresAt = time.Now().Add(24 * time.Hour)
sessions[cookie.Value] = entry
}
sessionMu.Unlock()
next(w, r)
}
}
// handleLogin 登录处理
func handleLogin(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
writeJSONError(w, http.StatusMethodNotAllowed, "Method Not Allowed")
return
}
var req struct {
Username string `json:"username"`
Password string `json:"password"`
}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
writeJSONError(w, http.StatusBadRequest, "无效的请求格式")
return
}
subscribeMu.RLock()
cfgUsername := subscribeConfig.Username
cfgPassword := subscribeConfig.Password
subscribeMu.RUnlock()
// 如果未设置账密,拒绝登录
if cfgUsername == "" || cfgPassword == "" {
writeJSONError(w, http.StatusUnauthorized, "未设置登录账户,请先通过配置初始化")
return
}
// 恒定时间比较防止时序攻击
usernameOK := subtle.ConstantTimeCompare([]byte(req.Username), []byte(cfgUsername)) == 1
passwordOK := subtle.ConstantTimeCompare([]byte(req.Password), []byte(cfgPassword)) == 1
if !usernameOK || !passwordOK {
writeJSONError(w, http.StatusUnauthorized, "用户名或密码错误")
return
}
token, err := generateToken()
if err != nil {
writeJSONError(w, http.StatusInternalServerError, "生成 token 失败")
return
}
sessionMu.Lock()
sessions[token] = sessionInfo{ExpiresAt: time.Now().Add(24 * time.Hour)}
sessionMu.Unlock()
// 设置 cookie
http.SetCookie(w, &http.Cookie{
Name: "nexusbox_token",
Value: token,
Path: "/",
HttpOnly: true,
SameSite: http.SameSiteStrictMode,
MaxAge: 86400, // 24 小时
})
respondJSON(w, http.StatusOK, map[string]string{
"status": "ok",
"message": "登录成功",
})
}
// handleLogout 登出
func handleLogout(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
writeJSONError(w, http.StatusMethodNotAllowed, "Method Not Allowed")
return
}
cookie, err := r.Cookie("nexusbox_token")
if err == nil && cookie.Value != "" {
sessionMu.Lock()
delete(sessions, cookie.Value)
sessionMu.Unlock()
}
// 清除 cookie
http.SetCookie(w, &http.Cookie{
Name: "nexusbox_token",
Value: "",
Path: "/",
MaxAge: -1,
})
respondJSON(w, http.StatusOK, map[string]string{
"status": "ok",
"message": "已登出",
})
}
// handleAuthStatus 检查当前登录状态
func handleAuthStatus(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
writeJSONError(w, http.StatusMethodNotAllowed, "Method Not Allowed")
return
}
cookie, err := r.Cookie("nexusbox_token")
if err != nil || cookie.Value == "" {
respondJSON(w, http.StatusOK, map[string]bool{"authenticated": false})
return
}
sessionMu.RLock()
s, ok := sessions[cookie.Value]
sessionMu.RUnlock()
if !ok || time.Now().After(s.ExpiresAt) {
respondJSON(w, http.StatusOK, map[string]bool{"authenticated": false})
return
}
respondJSON(w, http.StatusOK, map[string]bool{"authenticated": true})
}
// handleAuthConfig 获取或更新认证配置(账密)
func handleAuthConfig(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
subscribeMu.RLock()
u := subscribeConfig.Username
subscribeMu.RUnlock()
respondJSON(w, http.StatusOK, map[string]string{
"username": u,
})
case http.MethodPost:
var req struct {
Username string `json:"username"`
Password string `json:"password"`
}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
writeJSONError(w, http.StatusBadRequest, "无效的请求格式")
return
}
if req.Username == "" {
writeJSONError(w, http.StatusBadRequest, "用户名不能为空")
return
}
if req.Password == "" {
writeJSONError(w, http.StatusBadRequest, "密码不能为空")
return
}
subscribeMu.Lock()
subscribeConfig.Username = req.Username
subscribeConfig.Password = req.Password
subscribeMu.Unlock()
if err := saveSubscribeConfig(); err != nil {
writeJSONError(w, http.StatusInternalServerError, "保存配置失败: "+err.Error())
return
}
respondJSON(w, http.StatusOK, map[string]string{
"status": "ok",
"message": "认证配置已保存",
})
default:
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
}
}