fix: v2.0.1 - 修复登录会话创建失败问题

🐛 Bug 修复:
- 修复 securecookie: the value is not valid 错误
- 回退密钥处理逻辑确保向后兼容
- 改进会话初始化错误处理

🔧 变更:
- 简化 auth.Init 函数
- 密钥不足时仅记录警告
- 更新版本号到 v2.0.1

 测试确认:
- 登录流程正常
- API v1 兼容
- Cookie 向后兼容
This commit is contained in:
OpenClaw Agent
2026-02-08 23:09:41 +08:00
parent 93e3911f43
commit c01b3e82cf
9 changed files with 84 additions and 72 deletions

View File

@@ -1,8 +1,6 @@
package auth
import (
"encoding/hex"
"fmt"
"log"
"net/http"
"time"
@@ -23,39 +21,14 @@ const (
)
// Init 初始化会话存储
func Init(secretKey string) error {
if secretKey == "" {
return fmt.Errorf("安全密钥不能为空")
}
// 支持 hex 格式的密钥
key := []byte(secretKey)
if len(key) > 64 && len(secretKey) >= 64 { // 可能是 hex 格式 32 字节
if decodedKey, err := hex.DecodeString(secretKey); err == nil {
key = decodedKey
log.Printf("使用 hex 解码密钥")
} else {
log.Printf("hex 解码失败,使用原始密钥: %v", err)
}
}
// 检查密钥长度至少16字节
if len(key) < 16 {
return fmt.Errorf("安全密钥长度不足至少需要16字节当前: %d 字节)", len(key))
}
store = sessions.NewCookieStore(key)
func Init(secretKey string) {
store = sessions.NewCookieStore([]byte(secretKey))
store.Options = &sessions.Options{
Path: "/",
MaxAge: 86400 * 7, // 7天
HttpOnly: true,
// 不设置 SameSite让浏览器使用默认值Lax在同站上下文中工作正常
// SameSite: http.SameSiteNoneMode,
// Secure: true,
}
log.Printf("会话存储初始化完成,密钥长度: %d 字节", len(key))
return nil
log.Printf("会话存储初始化完成,密钥长度: %d 字节", len(secretKey))
}
// GetStore 获取会话存储