1Panel/core/middleware/session.go

44 lines
1.2 KiB
Go
Raw Normal View History

2024-07-19 11:04:11 +00:00
package middleware
import (
"strconv"
2024-07-25 10:13:57 +00:00
"strings"
2024-07-19 11:04:11 +00:00
"github.com/1Panel-dev/1Panel/core/app/api/v1/helper"
"github.com/1Panel-dev/1Panel/core/app/repo"
"github.com/1Panel-dev/1Panel/core/constant"
"github.com/1Panel-dev/1Panel/core/global"
"github.com/gin-gonic/gin"
)
func SessionAuth() gin.HandlerFunc {
return func(c *gin.Context) {
2024-07-25 10:13:57 +00:00
if strings.HasPrefix(c.Request.URL.Path, "/api/v2/core/auth") {
c.Next()
return
}
2024-07-19 11:04:11 +00:00
if method, exist := c.Get("authMethod"); exist && method == constant.AuthMethodJWT {
c.Next()
return
}
sId, err := c.Cookie(constant.SessionName)
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrUnauthorized, constant.ErrTypeNotLogin, nil)
return
}
psession, err := global.SESSION.Get(sId)
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrUnauthorized, constant.ErrTypeNotLogin, nil)
return
}
settingRepo := repo.NewISettingRepo()
setting, err := settingRepo.Get(settingRepo.WithByKey("SessionTimeout"))
if err != nil {
global.LOG.Errorf("create operation record failed, err: %v", err)
}
lifeTime, _ := strconv.Atoi(setting.Value)
_ = global.SESSION.Set(sId, psession, lifeTime)
c.Next()
}
}