mirror of
https://gitee.com/fit2cloud-feizhiyun/1Panel.git
synced 2024-12-04 12:59:52 +08:00
45 lines
1.2 KiB
Go
45 lines
1.2 KiB
Go
package middleware
|
|
|
|
import (
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/1Panel-dev/1Panel/app/api/v1/helper"
|
|
"github.com/1Panel-dev/1Panel/app/repo"
|
|
"github.com/1Panel-dev/1Panel/constant"
|
|
"github.com/1Panel-dev/1Panel/global"
|
|
jwtUtils "github.com/1Panel-dev/1Panel/utils/jwt"
|
|
"github.com/golang-jwt/jwt/v4"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func JwtAuth() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
c.Set("authMethod", "")
|
|
token := c.Request.Header.Get(constant.JWTHeaderName)
|
|
if token == "" {
|
|
c.Next()
|
|
return
|
|
}
|
|
j := jwtUtils.NewJWT()
|
|
claims, err := j.ParseToken(token)
|
|
if err != nil {
|
|
helper.ErrorWithDetail(c, constant.CodeErrUnauthorized, constant.ErrTypeInternalServer, err)
|
|
return
|
|
}
|
|
if claims.ExpiresAt.Unix()-time.Now().Unix() < claims.BufferTime {
|
|
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)
|
|
claims.ExpiresAt = jwt.NewNumericDate(time.Now().Add(time.Second * time.Duration(lifeTime)))
|
|
}
|
|
c.Set("claims", claims)
|
|
c.Set("authMethod", constant.AuthMethodJWT)
|
|
c.Next()
|
|
}
|
|
}
|