Spaces:
Running
Running
Download app/core/auth.py from moole/grok2api_fastapi: direct link, hf CLI and curl.
- Browser
- Download file 1.61 kB
-
https://huggingface.co/spaces/moole/grok2api_fastapi/resolve/main/app/core/auth.py
- Command line
-
hf download hf://spaces/moole/grok2api_fastapi/app/core/auth.py
-
curl -L -o auth.py https://huggingface.co/spaces/moole/grok2api_fastapi/resolve/main/app/core/auth.py
1.61 kB
| """认证模块""" | |
| from typing import Optional | |
| from fastapi import Depends, HTTPException | |
| from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials | |
| from app.core.config import setting | |
| from app.core.logger import logger | |
| security = HTTPBearer(auto_error=False) | |
| class AuthManager: | |
| """认证管理器""" | |
| def verify(credentials: Optional[HTTPAuthorizationCredentials] = Depends(security)) -> Optional[str]: | |
| """验证认证令牌""" | |
| api_key = setting.grok_config.get("api_key") | |
| if not api_key: | |
| logger.debug("[Auth] 未设置API_KEY,跳过验证。") | |
| return credentials.credentials if credentials else None | |
| if not credentials: | |
| raise HTTPException( | |
| status_code=401, | |
| detail={ | |
| "error": { | |
| "message": "缺少认证令牌", | |
| "type": "authentication_error", | |
| "code": "missing_token" | |
| } | |
| } | |
| ) | |
| if credentials.credentials != api_key: | |
| raise HTTPException( | |
| status_code=401, | |
| detail={ | |
| "error": { | |
| "message": f"令牌无效,长度: {len(credentials.credentials)}", | |
| "type": "authentication_error", | |
| "code": "invalid_token" | |
| } | |
| } | |
| ) | |
| logger.debug("[Auth] 令牌认证成功。") | |
| return credentials.credentials | |
| auth_manager = AuthManager() |