2024-07-19 19:04:11 +08:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
2024-07-25 18:13:57 +08:00
|
|
|
"strings"
|
2024-07-19 19:04:11 +08:00
|
|
|
|
2024-08-07 13:52:34 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/core/app/api/v2/helper"
|
2024-07-19 19:04:11 +08:00
|
|
|
"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 18:13:57 +08:00
|
|
|
if strings.HasPrefix(c.Request.URL.Path, "/api/v2/core/auth") {
|
|
|
|
c.Next()
|
|
|
|
return
|
|
|
|
}
|
2024-07-19 19:04:11 +08:00
|
|
|
if method, exist := c.Get("authMethod"); exist && method == constant.AuthMethodJWT {
|
|
|
|
c.Next()
|
|
|
|
return
|
|
|
|
}
|
2024-11-26 16:00:29 +08:00
|
|
|
psession, err := global.SESSION.Get(c)
|
2024-07-19 19:04:11 +08:00
|
|
|
if err != nil {
|
2024-12-20 16:08:34 +08:00
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrUnauthorized, constant.ErrTypeNotLogin, err)
|
2024-07-19 19:04:11 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
settingRepo := repo.NewISettingRepo()
|
2024-12-10 13:57:38 +08:00
|
|
|
setting, err := settingRepo.Get(repo.WithByKey("SessionTimeout"))
|
2024-07-19 19:04:11 +08:00
|
|
|
if err != nil {
|
|
|
|
global.LOG.Errorf("create operation record failed, err: %v", err)
|
2024-11-26 16:00:29 +08:00
|
|
|
return
|
2024-07-19 19:04:11 +08:00
|
|
|
}
|
|
|
|
lifeTime, _ := strconv.Atoi(setting.Value)
|
2024-12-10 13:57:38 +08:00
|
|
|
httpsSetting, err := settingRepo.Get(repo.WithByKey("SSL"))
|
2024-11-26 16:00:29 +08:00
|
|
|
if err != nil {
|
|
|
|
global.LOG.Errorf("create operation record failed, err: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2024-12-19 14:30:20 +08:00
|
|
|
_ = global.SESSION.Set(c, psession, httpsSetting.Value == constant.StatusEnable, lifeTime)
|
2024-07-19 19:04:11 +08:00
|
|
|
c.Next()
|
|
|
|
}
|
|
|
|
}
|