2022-08-16 23:30:23 +08:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
2022-09-19 19:42:06 +08:00
|
|
|
"strconv"
|
|
|
|
|
2022-10-17 16:32:31 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/api/v1/helper"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/repo"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
2022-08-16 23:30:23 +08:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
|
|
|
func SessionAuth() gin.HandlerFunc {
|
|
|
|
return func(c *gin.Context) {
|
|
|
|
if method, exist := c.Get("authMethod"); exist && method == constant.AuthMethodJWT {
|
|
|
|
c.Next()
|
2023-03-29 11:46:14 +08:00
|
|
|
return
|
2022-08-16 23:30:23 +08:00
|
|
|
}
|
2022-09-08 18:47:15 +08:00
|
|
|
sId, err := c.Cookie(constant.SessionName)
|
2022-08-16 23:30:23 +08:00
|
|
|
if err != nil {
|
2022-08-17 11:47:56 +08:00
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrUnauthorized, constant.ErrTypeNotLogin, nil)
|
2022-08-16 23:30:23 +08:00
|
|
|
return
|
|
|
|
}
|
2022-09-19 19:42:06 +08:00
|
|
|
psession, err := global.SESSION.Get(sId)
|
|
|
|
if err != nil {
|
2022-08-17 11:47:56 +08:00
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrUnauthorized, constant.ErrTypeNotLogin, nil)
|
2022-08-16 23:30:23 +08:00
|
|
|
return
|
|
|
|
}
|
2022-09-19 19:42:06 +08:00
|
|
|
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)
|
2022-08-16 23:30:23 +08:00
|
|
|
c.Next()
|
|
|
|
}
|
|
|
|
}
|