🐛 Bug 修复: - 修复 securecookie: the value is not valid 错误 - 回退密钥处理逻辑确保向后兼容 - 改进会话初始化错误处理 🔧 变更: - 简化 auth.Init 函数 - 密钥不足时仅记录警告 - 更新版本号到 v2.0.1 ✅ 测试确认: - 登录流程正常 - API v1 兼容 - Cookie 向后兼容
116 lines
2.6 KiB
Go
116 lines
2.6 KiB
Go
package auth
|
|
|
|
import (
|
|
"log"
|
|
"net/http"
|
|
"time"
|
|
|
|
"sms-receiver-go/config"
|
|
|
|
"github.com/gorilla/sessions"
|
|
)
|
|
|
|
var store *sessions.CookieStore
|
|
|
|
// SessionKey 会话相关的 key
|
|
const (
|
|
SessionKeyLoggedIn = "logged_in"
|
|
SessionKeyUsername = "username"
|
|
SessionKeyLoginTime = "login_time"
|
|
SessionKeyLastActive = "last_activity"
|
|
)
|
|
|
|
// Init 初始化会话存储
|
|
func Init(secretKey string) {
|
|
store = sessions.NewCookieStore([]byte(secretKey))
|
|
store.Options = &sessions.Options{
|
|
Path: "/",
|
|
MaxAge: 86400 * 7, // 7天
|
|
HttpOnly: true,
|
|
}
|
|
log.Printf("会话存储初始化完成,密钥长度: %d 字节", len(secretKey))
|
|
}
|
|
|
|
// GetStore 获取会话存储
|
|
func GetStore() *sessions.CookieStore {
|
|
return store
|
|
}
|
|
|
|
// Login 登录
|
|
func Login(w http.ResponseWriter, r *http.Request, username string) error {
|
|
session, err := store.Get(r, "sms-receiver")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
session.Values[SessionKeyLoggedIn] = true
|
|
session.Values[SessionKeyUsername] = username
|
|
session.Values[SessionKeyLoginTime] = time.Now().Unix()
|
|
session.Values[SessionKeyLastActive] = time.Now().Unix()
|
|
|
|
return session.Save(r, w)
|
|
}
|
|
|
|
// Logout 登出
|
|
func Logout(r *http.Request, w http.ResponseWriter) error {
|
|
session, err := store.Get(r, "sms-receiver")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
session.Values = make(map[interface{}]interface{})
|
|
session.Save(r, w)
|
|
return nil
|
|
}
|
|
|
|
// IsLoggedIn 检查是否已登录
|
|
func IsLoggedIn(r *http.Request) (bool, string) {
|
|
session, err := store.Get(r, "sms-receiver")
|
|
if err != nil {
|
|
return false, ""
|
|
}
|
|
|
|
loggedIn, ok := session.Values[SessionKeyLoggedIn].(bool)
|
|
if !ok || !loggedIn {
|
|
return false, ""
|
|
}
|
|
|
|
username, _ := session.Values[SessionKeyUsername].(string)
|
|
|
|
// 检查会话是否过期
|
|
cfg := config.Get()
|
|
if cfg != nil {
|
|
lastActive, ok := session.Values[SessionKeyLastActive].(int64)
|
|
if ok {
|
|
sessionLifetime := cfg.GetSessionLifetimeDuration()
|
|
if time.Now().Unix()-lastActive > int64(sessionLifetime.Seconds()) {
|
|
return false, ""
|
|
}
|
|
// 更新最后活跃时间
|
|
session.Values[SessionKeyLastActive] = time.Now().Unix()
|
|
}
|
|
}
|
|
|
|
return true, username
|
|
}
|
|
|
|
// CheckLogin 检查登录状态,未登录则跳转到登录页
|
|
func CheckLogin(w http.ResponseWriter, r *http.Request) (bool, string) {
|
|
loggedIn, username := IsLoggedIn(r)
|
|
if !loggedIn {
|
|
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
|
return false, ""
|
|
}
|
|
return true, username
|
|
}
|
|
|
|
// GetCurrentUser 获取当前用户名
|
|
func GetCurrentUser(r *http.Request) string {
|
|
session, err := store.Get(r, "sms-receiver")
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
username, _ := session.Values[SessionKeyUsername].(string)
|
|
return username
|
|
}
|