Skip to content

Commit 00e4659

Browse files
committed
access_token_ttl 默认改为 6 小时过期,并且允许从 .env 定义
1 parent 5c71b02 commit 00e4659

3 files changed

Lines changed: 22 additions & 3 deletions

File tree

.claude/settings.local.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
{
22
"permissions": {
33
"allow": [
4-
"Bash(cargo check:*)"
4+
"Bash(cargo check:*)",
5+
"Bash(grep:*)",
6+
"WebSearch",
7+
"Bash(pnpm add:*)",
8+
"Bash(npm run build:*)"
59
]
610
}
711
}

backend/.env.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ HC_API_BASE=http://localhost:8080
88
HC_JWT_SECRET=please-set-a-strong-random-secret-32chars
99
HC_JWT_ISSUER=hypercraft-api
1010
HC_JWT_AUDIENCE=hypercraft-clients
11+
# JWT Token 有效期(秒)
12+
# HC_ACCESS_TOKEN_TTL=21600 # Access Token 有效期(默认 6 小时)
13+
# HC_REFRESH_TOKEN_TTL=604800 # Refresh Token 有效期(默认 7 天)
1114
# 逗号分隔命令名(仅文件名)
1215
HC_ALLOWED_COMMANDS=java.exe,node.exe
1316
# 分号分隔绝对路径前缀

backend/hypercraft-core/src/user/manager.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::error::{Result, ServiceError};
66
use chrono::Utc;
77
use serde_json;
88
use std::collections::HashMap;
9+
use std::env;
910
use std::fs;
1011
use std::path::{Path, PathBuf};
1112
use tracing::{info, instrument};
@@ -37,13 +38,24 @@ pub struct UserManager {
3738
impl UserManager {
3839
/// 创建新的用户管理器
3940
pub fn new<P: AsRef<Path>>(data_dir: P, jwt_secret: String) -> Self {
41+
// 从环境变量读取 TTL,如果未设置则使用默认值
42+
let access_token_ttl = env::var("HC_ACCESS_TOKEN_TTL")
43+
.ok()
44+
.and_then(|s| s.parse().ok())
45+
.unwrap_or(6 * 60 * 60); // 默认 6 小时
46+
47+
let refresh_token_ttl = env::var("HC_REFRESH_TOKEN_TTL")
48+
.ok()
49+
.and_then(|s| s.parse().ok())
50+
.unwrap_or(7 * 24 * 3600); // 默认 7 天
51+
4052
Self {
4153
data_dir: data_dir.as_ref().to_path_buf(),
4254
jwt_secret,
4355
jwt_issuer: DEFAULT_JWT_ISSUER.to_string(),
4456
jwt_audience: DEFAULT_JWT_AUDIENCE.to_string(),
45-
access_token_ttl: 15 * 60, // 15 分钟
46-
refresh_token_ttl: 7 * 24 * 3600, // 7 天
57+
access_token_ttl,
58+
refresh_token_ttl,
4759
}
4860
}
4961

0 commit comments

Comments
 (0)