Skip to content

Commit 6841b0b

Browse files
committed
feat: use env variable replace hardcoded hmacSecret
1 parent a0d3bcf commit 6841b0b

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

backend/domain/user/service/user_impl.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"encoding/base64"
2626
"encoding/json"
2727
"fmt"
28+
"os"
2829
"strconv"
2930
"strings"
3031
"time"
@@ -575,8 +576,16 @@ type Session struct {
575576
ExpiresAt time.Time `json:"expires_at"` // 过期时间
576577
}
577578

578-
// 用于签名的密钥(在实际应用中应从配置中读取或使用环境变量)
579-
var hmacSecret = []byte("opencoze-session-hmac-key")
579+
func initHmacSecret() []byte {
580+
SESSION_SECRET := os.Getenv("SESSION_SECRET")
581+
if len(SESSION_SECRET) == 0 {
582+
SESSION_SECRET = "opencoze-session-hmac-key" // 默认的会话密钥
583+
}
584+
return []byte(SESSION_SECRET)
585+
}
586+
587+
// 用于签名的密钥
588+
var hmacSecret = initHmacSecret()
580589

581590
// 生成安全的会话密钥
582591
func generateSessionKey(sessionID int64) (string, error) {

docker/.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export LOG_LEVEL="debug"
44
export MAX_REQUEST_BODY_SIZE=1073741824
55
export SERVER_HOST="localhost${LISTEN_ADDR}"
66
export MINIO_PROXY_ENDPOINT=":8889"
7+
export SESSION_SECRET="opencoze-session-hmac-key"
78

89
# MySQL
910
export MYSQL_ROOT_PASSWORD=root

0 commit comments

Comments
 (0)