feat: 将 Sense 登录有效期调整为30天 (#104)
This commit is contained in:
@@ -8,16 +8,11 @@ import (
|
||||
jwt "github.com/go-admin-team/go-admin-core/sdk/pkg/jwtauth"
|
||||
)
|
||||
|
||||
const defaultLoginValidity = 30 * 24 * time.Hour
|
||||
|
||||
// AuthInit jwt验证new
|
||||
func AuthInit() (*jwt.GinJWTMiddleware, error) {
|
||||
timeout := time.Hour
|
||||
if config.ApplicationConfig.Mode == "dev" {
|
||||
timeout = time.Duration(876010) * time.Hour
|
||||
} else {
|
||||
if config.JwtConfig.Timeout != 0 {
|
||||
timeout = time.Duration(config.JwtConfig.Timeout) * time.Second
|
||||
}
|
||||
}
|
||||
timeout := resolveJWTTimeout(config.ApplicationConfig.Mode, config.JwtConfig.Timeout)
|
||||
return jwt.New(&jwt.GinJWTMiddleware{
|
||||
Realm: "Sense",
|
||||
Key: []byte(config.JwtConfig.Secret),
|
||||
@@ -34,3 +29,13 @@ func AuthInit() (*jwt.GinJWTMiddleware, error) {
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func resolveJWTTimeout(mode string, configuredSeconds int64) time.Duration {
|
||||
if mode == "dev" {
|
||||
return time.Duration(876010) * time.Hour
|
||||
}
|
||||
if configuredSeconds > 0 {
|
||||
return time.Duration(configuredSeconds) * time.Second
|
||||
}
|
||||
return defaultLoginValidity
|
||||
}
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestResolveJWTTimeoutDefaultsToThirtyDays(t *testing.T) {
|
||||
for _, mode := range []string{"prod", "test", "demo"} {
|
||||
if got := resolveJWTTimeout(mode, 0); got != 30*24*time.Hour {
|
||||
t.Errorf("mode %s timeout=%s, want 720h", mode, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveJWTTimeoutHonorsExplicitConfiguration(t *testing.T) {
|
||||
if got := resolveJWTTimeout("prod", 3600); got != time.Hour {
|
||||
t.Fatalf("timeout=%s, want 1h", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveJWTTimeoutKeepsUpstreamDevelopmentBehavior(t *testing.T) {
|
||||
if got := resolveJWTTimeout("dev", 1); got != 876010*time.Hour {
|
||||
t.Fatalf("development timeout=%s", got)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user