mirror of
https://github.com/TeaOSLab/EdgeAdmin.git
synced 2025-12-10 00:00:26 +08:00
提升Cookie安全性
This commit is contained in:
60
internal/web/actions/default/index/loginutils/utils.go
Normal file
60
internal/web/actions/default/index/loginutils/utils.go
Normal file
@@ -0,0 +1,60 @@
|
||||
// Copyright 2023 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
|
||||
package loginutils
|
||||
|
||||
import (
|
||||
teaconst "github.com/TeaOSLab/EdgeAdmin/internal/const"
|
||||
"github.com/iwind/TeaGo/actions"
|
||||
stringutil "github.com/iwind/TeaGo/utils/string"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// CalculateClientFingerprint 计算客户端指纹
|
||||
func CalculateClientFingerprint(action *actions.ActionObject) string {
|
||||
return stringutil.Md5(action.RequestRemoteIP() + "@" + action.Request.UserAgent())
|
||||
}
|
||||
|
||||
func SetCookie(action *actions.ActionObject, remember bool) {
|
||||
if remember {
|
||||
var cookie = &http.Cookie{
|
||||
Name: teaconst.CookieSID,
|
||||
Value: action.Session().Sid,
|
||||
Path: "/",
|
||||
MaxAge: 14 * 86400,
|
||||
HttpOnly: true,
|
||||
}
|
||||
if action.Request.TLS != nil {
|
||||
cookie.SameSite = http.SameSiteStrictMode
|
||||
cookie.Secure = true
|
||||
}
|
||||
action.AddCookie(cookie)
|
||||
} else {
|
||||
var cookie = &http.Cookie{
|
||||
Name: teaconst.CookieSID,
|
||||
Value: action.Session().Sid,
|
||||
Path: "/",
|
||||
MaxAge: 0,
|
||||
HttpOnly: true,
|
||||
}
|
||||
if action.Request.TLS != nil {
|
||||
cookie.SameSite = http.SameSiteStrictMode
|
||||
cookie.Secure = true
|
||||
}
|
||||
action.AddCookie(cookie)
|
||||
}
|
||||
}
|
||||
|
||||
func UnsetCookie(action *actions.ActionObject) {
|
||||
cookie := &http.Cookie{
|
||||
Name: teaconst.CookieSID,
|
||||
Value: action.Session().Sid,
|
||||
Path: "/",
|
||||
MaxAge: -1,
|
||||
HttpOnly: true,
|
||||
}
|
||||
if action.Request.TLS != nil {
|
||||
cookie.SameSite = http.SameSiteStrictMode
|
||||
cookie.Secure = true
|
||||
}
|
||||
action.AddCookie(cookie)
|
||||
}
|
||||
Reference in New Issue
Block a user