File size: 922 Bytes
19adbbb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# config.py
# 配置模块,管理环境变量和配置项
import os
from typing import Optional
# API认证配置
class AuthConfig:
# 从环境变量获取API令牌,如果未设置则使用默认值
API_TOKEN: str = os.getenv('API_TOKEN', 'sk-114514')
@classmethod
def get_token(cls) -> str:
"""获取API认证令牌
Returns:
str: API认证令牌
"""
return cls.API_TOKEN
@classmethod
def validate_token(cls, token: Optional[str]) -> bool:
"""验证API令牌是否有效
Args:
token: 待验证的令牌
Returns:
bool: 令牌是否有效
"""
if not token:
return False
# 移除Bearer前缀并验证
if token.startswith('Bearer '):
token = token[7:]
return token == cls.API_TOKEN |