diff --git a/chatgpt/authorization.py b/chatgpt/authorization.py index 34c811d..dcc0cd7 100644 --- a/chatgpt/authorization.py +++ b/chatgpt/authorization.py @@ -105,6 +105,9 @@ async def verify_token(req_token): return access_token elif len(req_token) == 45: try: + if req_token in globals.error_token_list: + raise HTTPException(status_code=401, detail="Error RefreshToken") + access_token = await rt2ac(req_token, force_refresh=False) return access_token except HTTPException as e: diff --git a/chatgpt/chatLimit.py b/chatgpt/chatLimit.py index ef6453d..08f8bf2 100644 --- a/chatgpt/chatLimit.py +++ b/chatgpt/chatLimit.py @@ -1,4 +1,3 @@ -import threading import time from datetime import datetime diff --git a/gateway/share.py b/gateway/share.py index 860cd80..a54a83a 100644 --- a/gateway/share.py +++ b/gateway/share.py @@ -1,6 +1,8 @@ import json import random +import time +import jwt from fastapi import Request, HTTPException, Security from fastapi.responses import Response from fastapi.security import HTTPAuthorizationCredentials @@ -219,14 +221,17 @@ async def refresh(request: Request): if not refresh_token and not access_token: raise HTTPException(status_code=401, detail="refresh_token or access_token is required") + need_refresh = True if access_token: - account_check_info = await chatgpt_account_check(access_token) - if account_check_info: - auth_info.update(account_check_info) - auth_info.update({"accessToken": access_token}) - return Response(content=json.dumps(auth_info), media_type="application/json") + try: + access_token_info = jwt.decode(access_token, options={"verify_signature": False}) + exp = access_token_info.get("exp", 0) + if exp > int(time.time()) + 60 * 60 * 24 * 5: + need_refresh = False + except Exception as e: + logger.error(f"access_token: {e}") - if refresh_token: + if refresh_token and need_refresh: chatgpt_refresh_info = await chatgpt_refresh(refresh_token) if chatgpt_refresh_info: auth_info.update(chatgpt_refresh_info) @@ -236,4 +241,13 @@ async def refresh(request: Request): auth_info.update(account_check_info) auth_info.update({"accessToken": access_token}) return Response(content=json.dumps(auth_info), media_type="application/json") + elif access_token: + account_check_info = await chatgpt_account_check(access_token) + if account_check_info: + auth_info.update(account_check_info) + auth_info.update({"accessToken": access_token}) + return Response(content=json.dumps(auth_info), media_type="application/json") + raise HTTPException(status_code=401, detail="Unauthorized") + + diff --git a/requirements.txt b/requirements.txt index 61b5637..dba48a1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -10,3 +10,4 @@ pybase64 jinja2 APScheduler ua-generator +pyjwt \ No newline at end of file diff --git a/version.txt b/version.txt index 3321830..743b3d6 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -1.7.0-beta2 \ No newline at end of file +1.7.0-beta3 \ No newline at end of file