1
0
mirror of https://github.com/1Panel-dev/1Panel.git synced 2025-01-31 14:08:06 +08:00

fix: 修改 jwt 超时时间为 1 小时 (#437)

This commit is contained in:
ssongliu 2023-03-29 15:34:13 +08:00 committed by GitHub
parent cbe9c83515
commit 4a1aa84fa8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 4 additions and 19 deletions

View File

@ -118,7 +118,7 @@ func (u *AuthService) generateSession(c *gin.Context, name, authMethod string) (
j := jwt.NewJWT() j := jwt.NewJWT()
claims := j.CreateClaims(jwt.BaseClaims{ claims := j.CreateClaims(jwt.BaseClaims{
Name: name, Name: name,
}, lifeTime) })
token, err := j.CreateToken(claims) token, err := j.CreateToken(claims)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -6,7 +6,7 @@ const (
AuthMethodJWT = "jwt" AuthMethodJWT = "jwt"
JWTHeaderName = "Authorization" JWTHeaderName = "Authorization"
JWTBufferTime = 86400 JWTBufferTime = 3600
JWTIssuer = "1Panel" JWTIssuer = "1Panel"
PasswordExpiredName = "expired" PasswordExpiredName = "expired"

View File

@ -1,15 +1,9 @@
package middleware package middleware
import ( import (
"strconv"
"time"
"github.com/1Panel-dev/1Panel/backend/app/api/v1/helper" "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/constant"
"github.com/1Panel-dev/1Panel/backend/global"
jwtUtils "github.com/1Panel-dev/1Panel/backend/utils/jwt" jwtUtils "github.com/1Panel-dev/1Panel/backend/utils/jwt"
"github.com/golang-jwt/jwt/v4"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
@ -27,15 +21,6 @@ func JwtAuth() gin.HandlerFunc {
helper.ErrorWithDetail(c, constant.CodeErrUnauthorized, constant.ErrTypeInternalServer, err) helper.ErrorWithDetail(c, constant.CodeErrUnauthorized, constant.ErrTypeInternalServer, err)
return 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("claims", claims)
c.Set("authMethod", constant.AuthMethodJWT) c.Set("authMethod", constant.AuthMethodJWT)
c.Next() c.Next()

View File

@ -38,12 +38,12 @@ func NewJWT() *JWT {
} }
} }
func (j *JWT) CreateClaims(baseClaims BaseClaims, ttl int) CustomClaims { func (j *JWT) CreateClaims(baseClaims BaseClaims) CustomClaims {
claims := CustomClaims{ claims := CustomClaims{
BaseClaims: baseClaims, BaseClaims: baseClaims,
BufferTime: constant.JWTBufferTime, BufferTime: constant.JWTBufferTime,
RegisteredClaims: jwt.RegisteredClaims{ RegisteredClaims: jwt.RegisteredClaims{
ExpiresAt: jwt.NewNumericDate(time.Now().Add(time.Second * time.Duration(ttl))), ExpiresAt: jwt.NewNumericDate(time.Now().Add(time.Second * time.Duration(constant.JWTBufferTime))),
Issuer: constant.JWTIssuer, Issuer: constant.JWTIssuer,
}, },
} }