返回提交历史
Modified
g4f/api/__init__.py
+10
-11
XFEstudio/gpt4free
Refactor API key validation to improve error handling and user retrieval logic
e2f8437c
代码差异
1 个文件
+10
-11
@@ -249,18 +249,17 @@ class Api:
249
249
if AppConfig.g4f_api_key is None or not user_g4f_api_key or not secrets.compare_digest(AppConfig.g4f_api_key, user_g4f_api_key):
250
250
if has_crypto and user_g4f_api_key:
251
251
try:
252
expires, user = decrypt_data(private_key, user_g4f_api_key).split(":", 1)
253
except:
254
252
try:
255
data = json.loads(decrypt_data(session_key, user_g4f_api_key))
256
expires = int(decrypt_data(private_key, data["data"])) + 86400
257
user = data.get("user", user)
258
if not user:
259
raise ValueError("User not found")
260
except Exception as e:
261
debug.log(f"Invalid G4F API key '{user_g4f_api_key[:6]}...' for user: '{user}'")
262
debug.error(e)
263
return ErrorResponse.from_message(f"Invalid G4F API key", HTTP_401_UNAUTHORIZED)
253
expires, user = decrypt_data(private_key, user_g4f_api_key).split(":", 1)
254
except ValueError as e:
255
pass
256
data = json.loads(decrypt_data(session_key, user_g4f_api_key))
257
expires = int(decrypt_data(private_key, data["data"])) + 86400
258
user = data.get("user", user)
259
except Exception as e:
260
debug.log(f"Invalid G4F API key '{user_g4f_api_key[:6]}...' for user: '{user}'")
261
debug.error(e)
262
return ErrorResponse.from_message(f"Invalid G4F API key", HTTP_401_UNAUTHORIZED)
264
263
expires = int(expires) - int(time.time())
265
264
hours, remainder = divmod(expires, 3600)
266
265
minutes, seconds = divmod(remainder, 60)