diff --git a/README.md b/README.md index cebb385..e9083c5 100644 --- a/README.md +++ b/README.md @@ -177,6 +177,7 @@ app/ | `API_KEYS` | **Required**, list of Gemini API keys | `[]` | | `ALLOWED_TOKENS` | **Required**, list of access tokens | `[]` | | `AUTH_TOKEN` | Super admin token, defaults to the first of `ALLOWED_TOKENS` | `sk-123456` | +| `ADMIN_SESSION_EXPIRE` | Admin session expiration time in seconds (5 minutes to 24 hours) | `3600` | | `TEST_MODEL` | Model for testing key validity | `gemini-1.5-flash` | | `IMAGE_MODELS` | Models supporting image generation | `["gemini-2.0-flash-exp"]` | | `SEARCH_MODELS` | Models supporting web search | `["gemini-2.0-flash-exp"]` | diff --git a/app/config/config.py b/app/config/config.py index 95b03a8..18a8fef 100644 --- a/app/config/config.py +++ b/app/config/config.py @@ -6,7 +6,7 @@ import datetime import json from typing import Any, Dict, List, Type, get_args, get_origin -from pydantic import ValidationError, ValidationInfo, field_validator +from pydantic import ValidationError, ValidationInfo, field_validator, Field from pydantic_settings import BaseSettings from sqlalchemy import insert, select, update @@ -131,6 +131,14 @@ class Settings(BaseSettings): FILES_CLEANUP_INTERVAL_HOURS: int = 1 FILES_USER_ISOLATION_ENABLED: bool = True + # Admin Session Configuration + ADMIN_SESSION_EXPIRE: int = Field( + default=3600, + ge=300, + le=86400, + description="Admin session expiration time in seconds (5 minutes to 24 hours)" + ) + def __init__(self, **kwargs): super().__init__(**kwargs) # 设置默认AUTH_TOKEN(如果未提供) diff --git a/app/router/routes.py b/app/router/routes.py index bc916f6..7ab24e7 100644 --- a/app/router/routes.py +++ b/app/router/routes.py @@ -7,6 +7,7 @@ from fastapi.responses import HTMLResponse, RedirectResponse from fastapi.templating import Jinja2Templates from app.core.security import verify_auth_token +from app.config.config import settings from app.log.logger import get_routes_logger from app.router import error_log_routes, gemini_routes, openai_routes, config_routes, scheduler_routes, stats_routes, version_routes, openai_compatiable_routes, vertex_express_routes, files_routes from app.service.key.key_manager import get_key_manager_instance @@ -69,7 +70,7 @@ def setup_page_routes(app: FastAPI) -> None: logger.info("Successful authentication") response = RedirectResponse(url="/config", status_code=302) response.set_cookie( - key="auth_token", value=auth_token, httponly=True, max_age=3600 + key="auth_token", value=auth_token, httponly=True, max_age=settings.ADMIN_SESSION_EXPIRE ) return response logger.warning("Failed authentication attempt with invalid token")