Merge remote-tracking branch 'origin/dev' into fix/101-sense-layout-shell
# Conflicts: # docs/02-architecture-and-code-map.md
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,57 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/go-admin-team/go-admin-core/sdk/config"
|
||||
)
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuthInitSignsTokenExpiringAfterThirtyDays(t *testing.T) {
|
||||
oldMode := config.ApplicationConfig.Mode
|
||||
oldTimeout := config.JwtConfig.Timeout
|
||||
oldSecret := config.JwtConfig.Secret
|
||||
t.Cleanup(func() {
|
||||
config.ApplicationConfig.Mode = oldMode
|
||||
config.JwtConfig.Timeout = oldTimeout
|
||||
config.JwtConfig.Secret = oldSecret
|
||||
})
|
||||
|
||||
config.ApplicationConfig.Mode = "prod"
|
||||
config.JwtConfig.Timeout = 0
|
||||
config.JwtConfig.Secret = "task-104-test-secret-not-for-production"
|
||||
|
||||
auth, err := AuthInit()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
now := time.Date(2026, 8, 17, 10, 0, 0, 0, time.UTC)
|
||||
auth.TimeFunc = func() time.Time { return now }
|
||||
_, expiresAt, err := auth.TokenGenerator(map[string]interface{}{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := expiresAt.Sub(now); got != 30*24*time.Hour {
|
||||
t.Fatalf("signed token validity=%s, want 720h", got)
|
||||
}
|
||||
}
|
||||
@@ -20,7 +20,7 @@ settings:
|
||||
# JWT加密字符串
|
||||
secret: ""
|
||||
# 过期时间单位:秒
|
||||
timeout: 3600
|
||||
timeout: 2592000
|
||||
database:
|
||||
# 数据库名称
|
||||
name: dbname
|
||||
|
||||
@@ -16,7 +16,7 @@ settings:
|
||||
frontpath: ../ui/src
|
||||
jwt:
|
||||
secret: ""
|
||||
timeout: 3600
|
||||
timeout: 2592000
|
||||
logger:
|
||||
# 日志存放路径
|
||||
path: temp/logs
|
||||
|
||||
@@ -34,7 +34,7 @@ settings:
|
||||
# token 密钥,生产环境时及的修改
|
||||
secret: ""
|
||||
# token 过期时间 单位:秒
|
||||
timeout: 3600
|
||||
timeout: 2592000
|
||||
database:
|
||||
# Sense 仅支持 PostgreSQL;连接信息由仓库外配置提供。
|
||||
driver: postgres
|
||||
|
||||
@@ -25,7 +25,7 @@ settings:
|
||||
# token 密钥,生产环境时及的修改
|
||||
secret: ""
|
||||
# token 过期时间 单位:秒
|
||||
timeout: 3600
|
||||
timeout: 2592000
|
||||
database:
|
||||
# 文件名为上游兼容名称;Sense 仍只支持 PostgreSQL。
|
||||
driver: postgres
|
||||
|
||||
@@ -25,7 +25,7 @@ settings:
|
||||
# 必填。生产环境至少 32 个字符;不得提交真实值。
|
||||
secret: ""
|
||||
# token 过期时间 单位:秒
|
||||
timeout: 3600
|
||||
timeout: 2592000
|
||||
database:
|
||||
# 数据库类型 mysql, sqlite3, postgres, sqlserver
|
||||
# sqlserver: sqlserver://用户名:密码@地址?database=数据库名
|
||||
|
||||
Reference in New Issue
Block a user