feat: 将 Sense 登录有效期调整为30天 (#104)
This commit is contained in:
@@ -228,7 +228,7 @@ function Initialize-SenseRuntime {
|
|||||||
' enableddb: false',
|
' enableddb: false',
|
||||||
' jwt:',
|
' jwt:',
|
||||||
" secret: $(ConvertTo-SenseYamlString $jwtSecret)",
|
" secret: $(ConvertTo-SenseYamlString $jwtSecret)",
|
||||||
' timeout: 3600',
|
' timeout: 2592000',
|
||||||
' database:',
|
' database:',
|
||||||
' driver: postgres',
|
' driver: postgres',
|
||||||
" source: $(ConvertTo-SenseYamlString $databaseURL)",
|
" source: $(ConvertTo-SenseYamlString $databaseURL)",
|
||||||
|
|||||||
@@ -8,16 +8,11 @@ import (
|
|||||||
jwt "github.com/go-admin-team/go-admin-core/sdk/pkg/jwtauth"
|
jwt "github.com/go-admin-team/go-admin-core/sdk/pkg/jwtauth"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const defaultLoginValidity = 30 * 24 * time.Hour
|
||||||
|
|
||||||
// AuthInit jwt验证new
|
// AuthInit jwt验证new
|
||||||
func AuthInit() (*jwt.GinJWTMiddleware, error) {
|
func AuthInit() (*jwt.GinJWTMiddleware, error) {
|
||||||
timeout := time.Hour
|
timeout := resolveJWTTimeout(config.ApplicationConfig.Mode, config.JwtConfig.Timeout)
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return jwt.New(&jwt.GinJWTMiddleware{
|
return jwt.New(&jwt.GinJWTMiddleware{
|
||||||
Realm: "Sense",
|
Realm: "Sense",
|
||||||
Key: []byte(config.JwtConfig.Secret),
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -20,7 +20,7 @@ settings:
|
|||||||
# JWT加密字符串
|
# JWT加密字符串
|
||||||
secret: ""
|
secret: ""
|
||||||
# 过期时间单位:秒
|
# 过期时间单位:秒
|
||||||
timeout: 3600
|
timeout: 2592000
|
||||||
database:
|
database:
|
||||||
# 数据库名称
|
# 数据库名称
|
||||||
name: dbname
|
name: dbname
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ settings:
|
|||||||
frontpath: ../ui/src
|
frontpath: ../ui/src
|
||||||
jwt:
|
jwt:
|
||||||
secret: ""
|
secret: ""
|
||||||
timeout: 3600
|
timeout: 2592000
|
||||||
logger:
|
logger:
|
||||||
# 日志存放路径
|
# 日志存放路径
|
||||||
path: temp/logs
|
path: temp/logs
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ settings:
|
|||||||
# token 密钥,生产环境时及的修改
|
# token 密钥,生产环境时及的修改
|
||||||
secret: ""
|
secret: ""
|
||||||
# token 过期时间 单位:秒
|
# token 过期时间 单位:秒
|
||||||
timeout: 3600
|
timeout: 2592000
|
||||||
database:
|
database:
|
||||||
# Sense 仅支持 PostgreSQL;连接信息由仓库外配置提供。
|
# Sense 仅支持 PostgreSQL;连接信息由仓库外配置提供。
|
||||||
driver: postgres
|
driver: postgres
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ settings:
|
|||||||
# token 密钥,生产环境时及的修改
|
# token 密钥,生产环境时及的修改
|
||||||
secret: ""
|
secret: ""
|
||||||
# token 过期时间 单位:秒
|
# token 过期时间 单位:秒
|
||||||
timeout: 3600
|
timeout: 2592000
|
||||||
database:
|
database:
|
||||||
# 文件名为上游兼容名称;Sense 仍只支持 PostgreSQL。
|
# 文件名为上游兼容名称;Sense 仍只支持 PostgreSQL。
|
||||||
driver: postgres
|
driver: postgres
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ settings:
|
|||||||
# 必填。生产环境至少 32 个字符;不得提交真实值。
|
# 必填。生产环境至少 32 个字符;不得提交真实值。
|
||||||
secret: ""
|
secret: ""
|
||||||
# token 过期时间 单位:秒
|
# token 过期时间 单位:秒
|
||||||
timeout: 3600
|
timeout: 2592000
|
||||||
database:
|
database:
|
||||||
# 数据库类型 mysql, sqlite3, postgres, sqlserver
|
# 数据库类型 mysql, sqlite3, postgres, sqlserver
|
||||||
# sqlserver: sqlserver://用户名:密码@地址?database=数据库名
|
# sqlserver: sqlserver://用户名:密码@地址?database=数据库名
|
||||||
|
|||||||
@@ -65,6 +65,7 @@ try {
|
|||||||
Assert-Equal 18071 $state.Port 'non-empty process environment must override sense.env'
|
Assert-Equal 18071 $state.Port 'non-empty process environment must override sense.env'
|
||||||
Assert-True (-not (Test-Path -LiteralPath $marker)) 'sense.env content must never execute'
|
Assert-True (-not (Test-Path -LiteralPath $marker)) 'sense.env content must never execute'
|
||||||
$generatedSettings = Get-Content -LiteralPath $state.SettingsPath -Raw
|
$generatedSettings = Get-Content -LiteralPath $state.SettingsPath -Raw
|
||||||
|
Assert-True $generatedSettings.Contains('timeout: 2592000') 'generated runtime settings must keep login valid for 30 days'
|
||||||
Assert-True ((Get-SenseEnvironmentValue -Name 'SENSE_DATABASE_URL').Contains('p#&;=x')) 'special characters must survive env parsing'
|
Assert-True ((Get-SenseEnvironmentValue -Name 'SENSE_DATABASE_URL').Contains('p#&;=x')) 'special characters must survive env parsing'
|
||||||
Assert-True ($generatedSettings.Contains('password=p#\u0026;=x')) 'special characters must be safely JSON-escaped in YAML'
|
Assert-True ($generatedSettings.Contains('password=p#\u0026;=x')) 'special characters must be safely JSON-escaped in YAML'
|
||||||
Assert-True (-not ((Get-SenseDatabaseInfo (Get-SenseEnvironmentValue -Name 'SENSE_DATABASE_URL')).Sanitized.Contains('password='))) 'PostgreSQL tool arguments must not contain password'
|
Assert-True (-not ((Get-SenseDatabaseInfo (Get-SenseEnvironmentValue -Name 'SENSE_DATABASE_URL')).Sanitized.Contains('password='))) 'PostgreSQL tool arguments must not contain password'
|
||||||
|
|||||||
@@ -1,13 +1,14 @@
|
|||||||
import Cookies from 'js-cookie'
|
import Cookies from 'js-cookie'
|
||||||
|
|
||||||
const TokenKey = 'Sense-Admin-Token'
|
const TokenKey = 'Sense-Admin-Token'
|
||||||
|
const LoginValidityDays = 30
|
||||||
|
|
||||||
export function getToken() {
|
export function getToken() {
|
||||||
return Cookies.get(TokenKey)
|
return Cookies.get(TokenKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function setToken(token) {
|
export function setToken(token) {
|
||||||
return Cookies.set(TokenKey, token)
|
return Cookies.set(TokenKey, token, { expires: LoginValidityDays })
|
||||||
}
|
}
|
||||||
|
|
||||||
export function removeToken() {
|
export function removeToken() {
|
||||||
|
|||||||
@@ -0,0 +1,32 @@
|
|||||||
|
import Cookies from 'js-cookie'
|
||||||
|
import { getToken, removeToken, setToken } from '@/utils/auth'
|
||||||
|
|
||||||
|
jest.mock('js-cookie', () => ({
|
||||||
|
get: jest.fn(),
|
||||||
|
set: jest.fn(),
|
||||||
|
remove: jest.fn()
|
||||||
|
}))
|
||||||
|
|
||||||
|
describe('Sense login token cookie', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
jest.clearAllMocks()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('persists the token for 30 days', () => {
|
||||||
|
setToken('test-token')
|
||||||
|
|
||||||
|
expect(Cookies.set).toHaveBeenCalledWith(
|
||||||
|
'Sense-Admin-Token',
|
||||||
|
'test-token',
|
||||||
|
{ expires: 30 }
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('reads and removes the same product-specific cookie', () => {
|
||||||
|
getToken()
|
||||||
|
removeToken()
|
||||||
|
|
||||||
|
expect(Cookies.get).toHaveBeenCalledWith('Sense-Admin-Token')
|
||||||
|
expect(Cookies.remove).toHaveBeenCalledWith('Sense-Admin-Token')
|
||||||
|
})
|
||||||
|
})
|
||||||
Reference in New Issue
Block a user