XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet
公开
关注 0 Fork 0 Star 1
返回提交历史

XFEstudio/gpt4free

Fix check api key

d78c2c91
hlohaus <983577+hlohaus@users.noreply.github.com>
提交于

代码差异

1 个文件 +0 -59
Modified g4f/api/__init__.py +0 -59
@@ -558,15 +558,6 @@ class Api:
558 558 private_key, _ = create_or_read_keys()
559 559 session_key = get_session_key()
560 560
561 def _requires_api_key(path: str, demo: bool) -> bool:
562 """Return ``True`` when *path* must present a G4F API key."""
563 return (
564 path.startswith("/v1/")
565 or (path.startswith("/api/"))
566 or (path.startswith("/pa/") and not demo)
567 or (demo and path in ["/backend-api/v2/upload_cookies"])
568 )
569
570 561 @self.app.middleware("http")
571 562 async def authorization(request: Request, call_next):
572 563 user = None
@@ -588,56 +579,6 @@ class Api:
588 579 AppConfig.g4f_api_key, user_g4f_api_key[0]
589 580 )
590 581 ):
591 if has_crypto and user_g4f_api_key:
592 try:
593 expires, user = decrypt_data(
594 private_key, user_g4f_api_key[0]
595 ).split(":", 1)
596 except Exception:
597 try:
598 data = json.loads(
599 decrypt_data(session_key, user_g4f_api_key[0])
600 )
601 debug.log(f"Decrypted G4F API key data: {data}")
602 expires = (
603 int(decrypt_data(private_key, data.pop("data")))
604 + 86400
605 )
606 user = data.get("user", user)
607 if not user or "referrer" not in data:
608 raise ValueError("User not found")
609 except Exception:
610 return ErrorResponse.from_message(
611 f"Invalid G4F API key", HTTP_401_UNAUTHORIZED
612 )
613 user = f"{country}:{user}" if country else user
614 expires = int(expires) - int(time.time())
615 hours, remainder = divmod(expires, 3600)
616 minutes, seconds = divmod(remainder, 60)
617 if expires < 0:
618 debug.log(f"G4F API key expired for user '{user}'")
619 return ErrorResponse.from_message(
620 "G4F API key expired", HTTP_401_UNAUTHORIZED
621 )
622 count = 0
623 for char in user:
624 if char.isupper():
625 count += 1
626 if count >= 6:
627 debug.log(f"Invalid user name (screaming): '{user}'")
628 return ErrorResponse.from_message(
629 "Invalid user name (screaming)", HTTP_401_UNAUTHORIZED
630 )
631 debug.log(
632 f"User: '{user}' G4F API key expires in {hours}h {minutes}m {seconds}s"
633 )
634 elif user is None:
635 user = "admin"
636 if user_g4f_api_key:
637 if user is None:
638 return ErrorResponse.from_message(
639 "Invalid G4F API key", HTTP_403_FORBIDDEN
640 )
641 582 try:
642 583 new_user = await self.get_username(request)
643 584 if user is None: