Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions backend/domain/user/service/user_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"os"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -575,8 +576,19 @@ type Session struct {
ExpiresAt time.Time `json:"expires_at"` // 过期时间
}

// 用于签名的密钥(在实际应用中应从配置中读取或使用环境变量)
var hmacSecret = []byte("opencoze-session-hmac-key")
// 用于签名的密钥
var hmacSecret []byte

func getHmacSecret() []byte {
if hmacSecret == nil {
secret := os.Getenv("SESSION_SECRET")
if secret == "" {
secret = "opencoze-session-hmac-key" // 默认的会话密钥
}
hmacSecret = []byte(secret)
}
return hmacSecret
}

// 生成安全的会话密钥
func generateSessionKey(sessionID int64) (string, error) {
Expand All @@ -594,7 +606,7 @@ func generateSessionKey(sessionID int64) (string, error) {
}

// 计算HMAC签名以确保完整性
h := hmac.New(sha256.New, hmacSecret)
h := hmac.New(sha256.New, getHmacSecret())
h.Write(sessionData)
signature := h.Sum(nil)

Expand Down Expand Up @@ -623,7 +635,7 @@ func verifySessionKey(sessionKey string) (*Session, error) {
signature := data[len(data)-32:]

// 验证签名
h := hmac.New(sha256.New, hmacSecret)
h := hmac.New(sha256.New, getHmacSecret())
h.Write(sessionData)
expectedSignature := h.Sum(nil)

Expand Down
1 change: 1 addition & 0 deletions docker/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export LOG_LEVEL="debug"
export MAX_REQUEST_BODY_SIZE=1073741824
export SERVER_HOST="localhost${LISTEN_ADDR}"
export MINIO_PROXY_ENDPOINT=":8889"
export SESSION_SECRET="opencoze-session-hmac-key"

# MySQL
export MYSQL_ROOT_PASSWORD=root
Expand Down