-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.py
More file actions
298 lines (241 loc) · 8.43 KB
/
Copy pathauth.py
File metadata and controls
298 lines (241 loc) · 8.43 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
"""
认证和授权模块
"""
from datetime import datetime, timedelta
from typing import Optional
from jose import JWTError, jwt
from passlib.context import CryptContext
from fastapi import Depends, HTTPException, status, Header, Request
from fastapi.security import OAuth2PasswordBearer
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select, and_, func
from sqlalchemy.orm import selectinload
from database import get_db
from models import User, APIKey, DailyUsage
from config import get_settings
settings = get_settings()
# 密码加密上下文
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
# OAuth2 scheme
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/api/auth/login", auto_error=False)
def verify_password(plain_password: str, hashed_password: str) -> bool:
"""验证密码"""
return pwd_context.verify(plain_password, hashed_password)
def get_password_hash(password: str) -> str:
"""获取密码哈希"""
return pwd_context.hash(password)
def create_access_token(data: dict, expires_delta: Optional[timedelta] = None) -> str:
"""创建访问令牌"""
to_encode = data.copy()
if expires_delta:
expire = datetime.utcnow() + expires_delta
else:
expire = datetime.utcnow() + timedelta(minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES)
to_encode.update({"exp": expire})
encoded_jwt = jwt.encode(to_encode, settings.SECRET_KEY, algorithm=settings.ALGORITHM)
return encoded_jwt
async def get_user_by_username(db: AsyncSession, username: str) -> Optional[User]:
"""通过用户名获取用户"""
result = await db.execute(select(User).where(User.username == username))
return result.scalar_one_or_none()
async def get_user_by_email(db: AsyncSession, email: str) -> Optional[User]:
"""通过邮箱获取用户"""
result = await db.execute(select(User).where(User.email == email))
return result.scalar_one_or_none()
async def authenticate_user(db: AsyncSession, username: str, password: str) -> Optional[User]:
"""认证用户"""
user = await get_user_by_username(db, username)
if not user:
return None
if not verify_password(password, user.hashed_password):
return None
return user
async def get_current_user(
token: Optional[str] = Depends(oauth2_scheme),
db: AsyncSession = Depends(get_db)
) -> Optional[User]:
"""获取当前用户(从 JWT Token)"""
if not token:
return None
try:
payload = jwt.decode(token, settings.SECRET_KEY, algorithms=[settings.ALGORITHM])
username: str = payload.get("sub")
if username is None:
return None
except JWTError:
return None
user = await get_user_by_username(db, username)
if user is None or not user.is_active:
return None
return user
async def get_current_active_user(
current_user: Optional[User] = Depends(get_current_user)
) -> User:
"""获取当前活跃用户(必须登录)"""
if not current_user:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="未登录或登录已过期",
headers={"WWW-Authenticate": "Bearer"},
)
if not current_user.is_active:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="用户已被禁用"
)
return current_user
async def get_current_admin_user(
current_user: User = Depends(get_current_active_user)
) -> User:
"""获取当前管理员用户"""
if not current_user.is_admin:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="需要管理员权限"
)
return current_user
async def get_api_key(
x_api_key: Optional[str] = Header(None, alias="X-API-Key"),
api_key: Optional[str] = Header(None),
db: AsyncSession = Depends(get_db)
) -> Optional[APIKey]:
"""从请求头获取 API Key"""
key = x_api_key or api_key
if not key:
return None
result = await db.execute(
select(APIKey)
.options(selectinload(APIKey.user))
.where(APIKey.key == key)
)
api_key_obj = result.scalar_one_or_none()
if not api_key_obj:
return None
# 检查是否有效
if not api_key_obj.is_active:
return None
# 检查是否过期
if api_key_obj.expires_at and api_key_obj.expires_at < datetime.utcnow():
return None
# 检查用户是否有效
if not api_key_obj.user.is_active:
return None
return api_key_obj
async def check_rate_limit(
api_key: APIKey,
db: AsyncSession
) -> tuple[bool, str]:
"""
检查 API Key 的速率限制
返回: (是否允许, 错误信息)
"""
now = datetime.utcnow()
today = now.replace(hour=0, minute=0, second=0, microsecond=0)
# 检查每日限制
result = await db.execute(
select(DailyUsage).where(
and_(
DailyUsage.api_key_id == api_key.id,
DailyUsage.date == today
)
)
)
daily_usage = result.scalar_one_or_none()
if daily_usage and daily_usage.request_count >= api_key.daily_limit:
return False, f"已达到每日请求限制 ({api_key.daily_limit} 次/天)"
return True, ""
async def record_usage(
api_key: APIKey,
db: AsyncSession
):
"""记录 API 使用"""
now = datetime.utcnow()
today = now.replace(hour=0, minute=0, second=0, microsecond=0)
# 更新每日统计
result = await db.execute(
select(DailyUsage).where(
and_(
DailyUsage.api_key_id == api_key.id,
DailyUsage.date == today
)
)
)
daily_usage = result.scalar_one_or_none()
if daily_usage:
daily_usage.request_count += 1
else:
daily_usage = DailyUsage(
api_key_id=api_key.id,
date=today,
request_count=1
)
db.add(daily_usage)
# 更新 API Key 统计
api_key.total_requests += 1
api_key.last_used_at = now
await db.commit()
async def require_api_key(
request: Request,
api_key: Optional[APIKey] = Depends(get_api_key),
db: AsyncSession = Depends(get_db)
) -> APIKey:
"""要求有效的 API Key(用于 API 端点)"""
if not api_key:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="缺少有效的 API Key,请在请求头中添加 X-API-Key"
)
# 检查速率限制
allowed, error_msg = await check_rate_limit(api_key, db)
if not allowed:
raise HTTPException(
status_code=status.HTTP_429_TOO_MANY_REQUESTS,
detail=error_msg
)
return api_key
async def optional_api_key(
api_key: Optional[APIKey] = Depends(get_api_key),
) -> Optional[APIKey]:
"""可选的 API Key(用于公开端点,有 Key 则记录使用)"""
return api_key
async def web_or_api_key(
request: Request,
api_key: Optional[APIKey] = Depends(get_api_key),
db: AsyncSession = Depends(get_db)
) -> Optional[APIKey]:
"""
网页查询用的依赖:
- 如果 WEB_QUERY_REQUIRE_API_KEY=True,必须提供 API Key
- 如果 WEB_QUERY_REQUIRE_API_KEY=False,API Key 可选
- 如果提供了 API Key,仍然校验并记录用量
"""
if api_key:
# 有 key 就校验限流
allowed, error_msg = await check_rate_limit(api_key, db)
if not allowed:
raise HTTPException(
status_code=status.HTTP_429_TOO_MANY_REQUESTS,
detail=error_msg
)
return api_key
if settings.WEB_QUERY_REQUIRE_API_KEY:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="缺少有效的 API Key,请在请求头中添加 X-API-Key"
)
return None
async def create_admin_user(db: AsyncSession):
"""创建默认管理员用户(如果不存在)"""
admin = await get_user_by_username(db, settings.ADMIN_USERNAME)
if not admin:
admin = User(
username=settings.ADMIN_USERNAME,
email=settings.ADMIN_EMAIL,
hashed_password=get_password_hash(settings.ADMIN_PASSWORD),
is_active=True,
is_admin=True
)
db.add(admin)
await db.commit()
print(f"✅ 已创建管理员账户: {settings.ADMIN_USERNAME}")
return admin