g4f 6.9.10__py3-none-any.whl → 7.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,9 +1,17 @@
1
1
  import os
2
+ import sys
2
3
  import json
3
4
  import base64
4
5
  import time
6
+ import secrets
7
+ import hashlib
8
+ import asyncio
9
+ import webbrowser
10
+ import threading
5
11
  from pathlib import Path
6
- from typing import Any, AsyncGenerator, Dict, List, Optional, Union
12
+ from typing import Any, AsyncGenerator, Dict, List, Optional, Union, Tuple
13
+ from urllib.parse import urlencode, parse_qs, urlparse
14
+ from http.server import HTTPServer, BaseHTTPRequestHandler
7
15
 
8
16
  import aiohttp
9
17
  from aiohttp import ClientSession, ClientTimeout
@@ -17,9 +25,213 @@ from ..base_provider import AsyncGeneratorProvider, ProviderModelMixin, AuthFile
17
25
  from ..helper import get_connector, get_system_prompt, format_media_prompt
18
26
  from ... import debug
19
27
 
28
+
20
29
  def get_oauth_creds_path():
21
30
  return Path.home() / ".gemini" / "oauth_creds.json"
22
31
 
32
+
33
+ # OAuth configuration for GeminiCLI
34
+ GEMINICLI_REDIRECT_URI = "http://localhost:51122/oauthcallback"
35
+ GEMINICLI_SCOPES = [
36
+ "https://www.googleapis.com/auth/cloud-platform",
37
+ "https://www.googleapis.com/auth/userinfo.email",
38
+ "https://www.googleapis.com/auth/userinfo.profile",
39
+ ]
40
+ GEMINICLI_OAUTH_CALLBACK_PORT = 51122
41
+ GEMINICLI_OAUTH_CALLBACK_PATH = "/oauthcallback"
42
+
43
+
44
+ def generate_pkce_pair() -> Tuple[str, str]:
45
+ """Generate a PKCE verifier and challenge pair."""
46
+ verifier = secrets.token_urlsafe(32)
47
+ digest = hashlib.sha256(verifier.encode('ascii')).digest()
48
+ challenge = base64.urlsafe_b64encode(digest).rstrip(b'=').decode('ascii')
49
+ return verifier, challenge
50
+
51
+
52
+ def encode_oauth_state(verifier: str) -> str:
53
+ """Encode OAuth state parameter with PKCE verifier."""
54
+ payload = {"verifier": verifier}
55
+ return base64.urlsafe_b64encode(json.dumps(payload).encode()).decode().rstrip('=')
56
+
57
+
58
+ def decode_oauth_state(state: str) -> Dict[str, str]:
59
+ """Decode OAuth state parameter back to verifier."""
60
+ padded = state + '=' * (4 - len(state) % 4) if len(state) % 4 else state
61
+ normalized = padded.replace('-', '+').replace('_', '/')
62
+ try:
63
+ decoded = base64.b64decode(normalized).decode('utf-8')
64
+ parsed = json.loads(decoded)
65
+ return {"verifier": parsed.get("verifier", "")}
66
+ except Exception:
67
+ return {"verifier": ""}
68
+
69
+
70
+ class GeminiCLIOAuthCallbackHandler(BaseHTTPRequestHandler):
71
+ """HTTP request handler for OAuth callback."""
72
+
73
+ callback_result: Optional[Dict[str, str]] = None
74
+ callback_error: Optional[str] = None
75
+
76
+ def log_message(self, format, *args):
77
+ """Suppress default logging."""
78
+ pass
79
+
80
+ def do_GET(self):
81
+ """Handle GET request for OAuth callback."""
82
+ parsed = urlparse(self.path)
83
+
84
+ if parsed.path != GEMINICLI_OAUTH_CALLBACK_PATH:
85
+ self.send_error(404, "Not Found")
86
+ return
87
+
88
+ params = parse_qs(parsed.query)
89
+ code = params.get("code", [None])[0]
90
+ state = params.get("state", [None])[0]
91
+ error = params.get("error", [None])[0]
92
+
93
+ if error:
94
+ GeminiCLIOAuthCallbackHandler.callback_error = error
95
+ self._send_error_response(error)
96
+ elif code and state:
97
+ GeminiCLIOAuthCallbackHandler.callback_result = {"code": code, "state": state}
98
+ self._send_success_response()
99
+ else:
100
+ GeminiCLIOAuthCallbackHandler.callback_error = "Missing code or state parameter"
101
+ self._send_error_response("Missing parameters")
102
+
103
+ def _send_success_response(self):
104
+ """Send success HTML response."""
105
+ html = """<!DOCTYPE html>
106
+ <html lang="en">
107
+ <head>
108
+ <meta charset="utf-8">
109
+ <title>Authentication Successful</title>
110
+ <style>
111
+ body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
112
+ display: flex; justify-content: center; align-items: center; height: 100vh;
113
+ margin: 0; background: linear-gradient(135deg, #4285f4 0%, #34a853 100%); }
114
+ .container { background: white; padding: 3rem; border-radius: 1rem;
115
+ box-shadow: 0 20px 60px rgba(0,0,0,0.3); text-align: center; max-width: 400px; }
116
+ h1 { color: #10B981; margin-bottom: 1rem; }
117
+ p { color: #6B7280; line-height: 1.6; }
118
+ </style>
119
+ </head>
120
+ <body>
121
+ <div class="container">
122
+ <div style="font-size: 4rem; margin-bottom: 1rem;">✅</div>
123
+ <h1>Authentication Successful!</h1>
124
+ <p>You have successfully authenticated with Google GeminiCLI.<br>You can close this window and return to your terminal.</p>
125
+ </div>
126
+ </body>
127
+ </html>"""
128
+ self.send_response(200)
129
+ self.send_header("Content-Type", "text/html; charset=utf-8")
130
+ self.send_header("Content-Length", len(html.encode()))
131
+ self.end_headers()
132
+ self.wfile.write(html.encode())
133
+
134
+ def _send_error_response(self, error: str):
135
+ """Send error HTML response."""
136
+ html = f"""<!DOCTYPE html>
137
+ <html lang="en">
138
+ <head>
139
+ <meta charset="utf-8">
140
+ <title>Authentication Failed</title>
141
+ <style>
142
+ body {{ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
143
+ display: flex; justify-content: center; align-items: center; height: 100vh;
144
+ margin: 0; background: #FEE2E2; }}
145
+ .container {{ background: white; padding: 3rem; border-radius: 1rem;
146
+ box-shadow: 0 10px 40px rgba(0,0,0,0.1); text-align: center; }}
147
+ h1 {{ color: #EF4444; }}
148
+ p {{ color: #6B7280; }}
149
+ </style>
150
+ </head>
151
+ <body>
152
+ <div class="container">
153
+ <h1>❌ Authentication Failed</h1>
154
+ <p>Error: {error}</p>
155
+ <p>Please try again.</p>
156
+ </div>
157
+ </body>
158
+ </html>"""
159
+ self.send_response(400)
160
+ self.send_header("Content-Type", "text/html; charset=utf-8")
161
+ self.send_header("Content-Length", len(html.encode()))
162
+ self.end_headers()
163
+ self.wfile.write(html.encode())
164
+
165
+
166
+ class GeminiCLIOAuthCallbackServer:
167
+ """Local HTTP server to capture OAuth callback."""
168
+
169
+ def __init__(self, port: int = GEMINICLI_OAUTH_CALLBACK_PORT, timeout: float = 300.0):
170
+ self.port = port
171
+ self.timeout = timeout
172
+ self.server: Optional[HTTPServer] = None
173
+ self._thread: Optional[threading.Thread] = None
174
+ self._stop_flag = False
175
+
176
+ def start(self) -> bool:
177
+ """Start the callback server. Returns True if successful."""
178
+ try:
179
+ GeminiCLIOAuthCallbackHandler.callback_result = None
180
+ GeminiCLIOAuthCallbackHandler.callback_error = None
181
+ self._stop_flag = False
182
+
183
+ self.server = HTTPServer(("localhost", self.port), GeminiCLIOAuthCallbackHandler)
184
+ self.server.timeout = 0.5
185
+
186
+ self._thread = threading.Thread(target=self._serve, daemon=True)
187
+ self._thread.start()
188
+ return True
189
+ except OSError as e:
190
+ debug.log(f"Failed to start OAuth callback server: {e}")
191
+ return False
192
+
193
+ def _serve(self):
194
+ """Serve requests until shutdown or result received."""
195
+ start_time = time.time()
196
+ while not self._stop_flag and self.server:
197
+ if time.time() - start_time > self.timeout:
198
+ break
199
+ if GeminiCLIOAuthCallbackHandler.callback_result or GeminiCLIOAuthCallbackHandler.callback_error:
200
+ time.sleep(0.3)
201
+ break
202
+ try:
203
+ self.server.handle_request()
204
+ except Exception:
205
+ break
206
+
207
+ def wait_for_callback(self) -> Optional[Dict[str, str]]:
208
+ """Wait for OAuth callback and return result."""
209
+ start_time = time.time()
210
+ while time.time() - start_time < self.timeout:
211
+ if GeminiCLIOAuthCallbackHandler.callback_result or GeminiCLIOAuthCallbackHandler.callback_error:
212
+ break
213
+ time.sleep(0.1)
214
+
215
+ self._stop_flag = True
216
+
217
+ if self._thread:
218
+ self._thread.join(timeout=2.0)
219
+
220
+ if GeminiCLIOAuthCallbackHandler.callback_error:
221
+ raise RuntimeError(f"OAuth error: {GeminiCLIOAuthCallbackHandler.callback_error}")
222
+
223
+ return GeminiCLIOAuthCallbackHandler.callback_result
224
+
225
+ def stop(self):
226
+ """Stop the callback server."""
227
+ self._stop_flag = True
228
+ if self.server:
229
+ try:
230
+ self.server.server_close()
231
+ except Exception:
232
+ pass
233
+ self.server = None
234
+
23
235
  class AuthManager(AuthFileMixin):
24
236
  """
25
237
  Handles OAuth2 authentication and Google Code Assist API communication.
@@ -567,4 +779,363 @@ class GeminiCLI(AsyncGeneratorProvider, ProviderModelMixin):
567
779
  tools=tools,
568
780
  **kwargs
569
781
  ):
570
- yield chunk
782
+ yield chunk
783
+
784
+ @classmethod
785
+ def build_authorization_url(cls) -> Tuple[str, str, str]:
786
+ """Build OAuth authorization URL with PKCE."""
787
+ verifier, challenge = generate_pkce_pair()
788
+ state = encode_oauth_state(verifier)
789
+
790
+ params = {
791
+ "client_id": AuthManager.OAUTH_CLIENT_ID,
792
+ "response_type": "code",
793
+ "redirect_uri": GEMINICLI_REDIRECT_URI,
794
+ "scope": " ".join(GEMINICLI_SCOPES),
795
+ "code_challenge": challenge,
796
+ "code_challenge_method": "S256",
797
+ "state": state,
798
+ "access_type": "offline",
799
+ "prompt": "consent",
800
+ }
801
+
802
+ url = f"https://accounts.google.com/o/oauth2/v2/auth?{urlencode(params)}"
803
+ return url, verifier, state
804
+
805
+ @classmethod
806
+ async def exchange_code_for_tokens(cls, code: str, state: str) -> Dict[str, Any]:
807
+ """Exchange authorization code for access and refresh tokens."""
808
+ decoded_state = decode_oauth_state(state)
809
+ verifier = decoded_state.get("verifier", "")
810
+
811
+ if not verifier:
812
+ raise RuntimeError("Missing PKCE verifier in state parameter")
813
+
814
+ start_time = time.time()
815
+
816
+ async with aiohttp.ClientSession() as session:
817
+ token_data = {
818
+ "client_id": AuthManager.OAUTH_CLIENT_ID,
819
+ "client_secret": AuthManager.OAUTH_CLIENT_SECRET,
820
+ "code": code,
821
+ "grant_type": "authorization_code",
822
+ "redirect_uri": GEMINICLI_REDIRECT_URI,
823
+ "code_verifier": verifier,
824
+ }
825
+
826
+ async with session.post(
827
+ "https://oauth2.googleapis.com/token",
828
+ data=token_data,
829
+ headers={"Content-Type": "application/x-www-form-urlencoded"}
830
+ ) as resp:
831
+ if not resp.ok:
832
+ error_text = await resp.text()
833
+ raise RuntimeError(f"Token exchange failed: {error_text}")
834
+
835
+ token_response = await resp.json()
836
+
837
+ access_token = token_response.get("access_token")
838
+ refresh_token = token_response.get("refresh_token")
839
+ expires_in = token_response.get("expires_in", 3600)
840
+
841
+ if not access_token or not refresh_token:
842
+ raise RuntimeError("Missing tokens in response")
843
+
844
+ # Get user info
845
+ email = None
846
+ async with session.get(
847
+ "https://www.googleapis.com/oauth2/v1/userinfo?alt=json",
848
+ headers={"Authorization": f"Bearer {access_token}"}
849
+ ) as resp:
850
+ if resp.ok:
851
+ user_info = await resp.json()
852
+ email = user_info.get("email")
853
+
854
+ expires_at = int((start_time + expires_in) * 1000) # milliseconds
855
+
856
+ return {
857
+ "access_token": access_token,
858
+ "refresh_token": refresh_token,
859
+ "expiry_date": expires_at,
860
+ "email": email,
861
+ }
862
+
863
+ @classmethod
864
+ async def interactive_login(
865
+ cls,
866
+ no_browser: bool = False,
867
+ timeout: float = 300.0,
868
+ ) -> Dict[str, Any]:
869
+ """Perform interactive OAuth login flow."""
870
+ auth_url, verifier, state = cls.build_authorization_url()
871
+
872
+ print("\n" + "=" * 60)
873
+ print("GeminiCLI OAuth Login")
874
+ print("=" * 60)
875
+
876
+ callback_server = GeminiCLIOAuthCallbackServer(timeout=timeout)
877
+ server_started = callback_server.start()
878
+
879
+ if server_started and not no_browser:
880
+ print(f"\nOpening browser for authentication...")
881
+ print(f"If browser doesn't open, visit this URL:\n")
882
+ print(f"{auth_url}\n")
883
+
884
+ try:
885
+ webbrowser.open(auth_url)
886
+ except Exception as e:
887
+ print(f"Could not open browser automatically: {e}")
888
+ print("Please open the URL above manually.\n")
889
+ else:
890
+ if not server_started:
891
+ print(f"\nCould not start local callback server on port {GEMINICLI_OAUTH_CALLBACK_PORT}.")
892
+ print("You may need to close any application using that port.\n")
893
+
894
+ print(f"\nPlease open this URL in your browser:\n")
895
+ print(f"{auth_url}\n")
896
+
897
+ if server_started:
898
+ print("Waiting for authentication callback...")
899
+
900
+ try:
901
+ callback_result = callback_server.wait_for_callback()
902
+
903
+ if not callback_result:
904
+ raise RuntimeError("OAuth callback timed out")
905
+
906
+ code = callback_result.get("code")
907
+ callback_state = callback_result.get("state")
908
+
909
+ if not code:
910
+ raise RuntimeError("No authorization code received")
911
+
912
+ print("\n✓ Authorization code received. Exchanging for tokens...")
913
+
914
+ tokens = await cls.exchange_code_for_tokens(code, callback_state or state)
915
+
916
+ print(f"✓ Authentication successful!")
917
+ if tokens.get("email"):
918
+ print(f" Logged in as: {tokens['email']}")
919
+
920
+ return tokens
921
+
922
+ finally:
923
+ callback_server.stop()
924
+ else:
925
+ print("\nAfter completing authentication, you'll be redirected to a localhost URL.")
926
+ print("Copy and paste the full redirect URL or just the authorization code below:\n")
927
+
928
+ user_input = input("Paste redirect URL or code: ").strip()
929
+
930
+ if not user_input:
931
+ raise RuntimeError("No input provided")
932
+
933
+ if user_input.startswith("http"):
934
+ parsed = urlparse(user_input)
935
+ params = parse_qs(parsed.query)
936
+ code = params.get("code", [None])[0]
937
+ callback_state = params.get("state", [state])[0]
938
+ else:
939
+ code = user_input
940
+ callback_state = state
941
+
942
+ if not code:
943
+ raise RuntimeError("Could not extract authorization code")
944
+
945
+ print("\nExchanging code for tokens...")
946
+ tokens = await cls.exchange_code_for_tokens(code, callback_state)
947
+
948
+ print(f"✓ Authentication successful!")
949
+ if tokens.get("email"):
950
+ print(f" Logged in as: {tokens['email']}")
951
+
952
+ return tokens
953
+
954
+ @classmethod
955
+ async def login(
956
+ cls,
957
+ no_browser: bool = False,
958
+ credentials_path: Optional[Path] = None,
959
+ ) -> "AuthManager":
960
+ """
961
+ Perform interactive OAuth login and save credentials.
962
+
963
+ Args:
964
+ no_browser: If True, don't auto-open browser
965
+ credentials_path: Path to save credentials
966
+
967
+ Returns:
968
+ AuthManager with active credentials
969
+
970
+ Example:
971
+ >>> import asyncio
972
+ >>> from g4f.Provider.needs_auth import GeminiCLI
973
+ >>> asyncio.run(GeminiCLI.login())
974
+ """
975
+ tokens = await cls.interactive_login(no_browser=no_browser)
976
+
977
+ creds = {
978
+ "access_token": tokens["access_token"],
979
+ "refresh_token": tokens["refresh_token"],
980
+ "expiry_date": tokens["expiry_date"],
981
+ "email": tokens.get("email"),
982
+ "client_id": AuthManager.OAUTH_CLIENT_ID,
983
+ "client_secret": AuthManager.OAUTH_CLIENT_SECRET,
984
+ }
985
+
986
+ if credentials_path:
987
+ path = credentials_path
988
+ else:
989
+ path = AuthManager.get_cache_file()
990
+
991
+ path.parent.mkdir(parents=True, exist_ok=True)
992
+
993
+ with path.open("w") as f:
994
+ json.dump(creds, f, indent=2)
995
+
996
+ try:
997
+ path.chmod(0o600)
998
+ except Exception:
999
+ pass
1000
+
1001
+ print(f"\n✓ Credentials saved to: {path}")
1002
+ print("=" * 60 + "\n")
1003
+
1004
+ auth_manager = AuthManager(env=os.environ)
1005
+ auth_manager._access_token = tokens["access_token"]
1006
+ auth_manager._expiry = tokens["expiry_date"] / 1000
1007
+ cls.auth_manager = auth_manager
1008
+
1009
+ return auth_manager
1010
+
1011
+ @classmethod
1012
+ def has_credentials(cls) -> bool:
1013
+ """Check if valid credentials exist."""
1014
+ cache_path = AuthManager.get_cache_file()
1015
+ if cache_path.exists():
1016
+ return True
1017
+ default_path = get_oauth_creds_path()
1018
+ return default_path.exists()
1019
+
1020
+ @classmethod
1021
+ def get_credentials_path(cls) -> Optional[Path]:
1022
+ """Get path to credentials file if it exists."""
1023
+ cache_path = AuthManager.get_cache_file()
1024
+ if cache_path.exists():
1025
+ return cache_path
1026
+ default_path = get_oauth_creds_path()
1027
+ if default_path.exists():
1028
+ return default_path
1029
+ return None
1030
+
1031
+
1032
+ async def main():
1033
+ """CLI entry point for GeminiCLI authentication."""
1034
+ import argparse
1035
+
1036
+ parser = argparse.ArgumentParser(
1037
+ description="GeminiCLI OAuth Authentication for gpt4free",
1038
+ formatter_class=argparse.RawDescriptionHelpFormatter,
1039
+ epilog="""
1040
+ Examples:
1041
+ %(prog)s login # Interactive login with browser
1042
+ %(prog)s login --no-browser # Manual login (paste URL)
1043
+ %(prog)s status # Check authentication status
1044
+ %(prog)s logout # Remove saved credentials
1045
+ """
1046
+ )
1047
+
1048
+ subparsers = parser.add_subparsers(dest="command", help="Commands")
1049
+
1050
+ # Login command
1051
+ login_parser = subparsers.add_parser("login", help="Authenticate with Google")
1052
+ login_parser.add_argument(
1053
+ "--no-browser", "-n",
1054
+ action="store_true",
1055
+ help="Don't auto-open browser, print URL instead"
1056
+ )
1057
+
1058
+ # Status command
1059
+ subparsers.add_parser("status", help="Check authentication status")
1060
+
1061
+ # Logout command
1062
+ subparsers.add_parser("logout", help="Remove saved credentials")
1063
+
1064
+ args = parser.parse_args()
1065
+
1066
+ if args.command == "login":
1067
+ try:
1068
+ await GeminiCLI.login(no_browser=args.no_browser)
1069
+ except KeyboardInterrupt:
1070
+ print("\n\nLogin cancelled.")
1071
+ sys.exit(1)
1072
+ except Exception as e:
1073
+ print(f"\n❌ Login failed: {e}")
1074
+ sys.exit(1)
1075
+
1076
+ elif args.command == "status":
1077
+ print("\nGeminiCLI Authentication Status")
1078
+ print("=" * 40)
1079
+
1080
+ if GeminiCLI.has_credentials():
1081
+ creds_path = GeminiCLI.get_credentials_path()
1082
+ print(f"✓ Credentials found at: {creds_path}")
1083
+
1084
+ try:
1085
+ with creds_path.open() as f:
1086
+ creds = json.load(f)
1087
+
1088
+ if creds.get("email"):
1089
+ print(f" Email: {creds['email']}")
1090
+
1091
+ expiry = creds.get("expiry_date")
1092
+ if expiry:
1093
+ expiry_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(expiry / 1000))
1094
+ if expiry / 1000 > time.time():
1095
+ print(f" Token expires: {expiry_time}")
1096
+ else:
1097
+ print(f" Token expired: {expiry_time} (will auto-refresh)")
1098
+ except Exception as e:
1099
+ print(f" (Could not read credential details: {e})")
1100
+ else:
1101
+ print("✗ No credentials found")
1102
+ print(f"\nRun 'g4f-geminicli login' to authenticate.")
1103
+
1104
+ print()
1105
+
1106
+ elif args.command == "logout":
1107
+ print("\nGeminiCLI Logout")
1108
+ print("=" * 40)
1109
+
1110
+ removed = False
1111
+
1112
+ cache_path = AuthManager.get_cache_file()
1113
+ if cache_path.exists():
1114
+ cache_path.unlink()
1115
+ print(f"✓ Removed: {cache_path}")
1116
+ removed = True
1117
+
1118
+ default_path = get_oauth_creds_path()
1119
+ if default_path.exists():
1120
+ default_path.unlink()
1121
+ print(f"✓ Removed: {default_path}")
1122
+ removed = True
1123
+
1124
+ if removed:
1125
+ print("\n✓ Credentials removed successfully.")
1126
+ else:
1127
+ print("No credentials found to remove.")
1128
+
1129
+ print()
1130
+
1131
+ else:
1132
+ parser.print_help()
1133
+
1134
+
1135
+ def cli_main():
1136
+ """Synchronous CLI entry point for setup.py console_scripts."""
1137
+ asyncio.run(main())
1138
+
1139
+
1140
+ if __name__ == "__main__":
1141
+ cli_main()
@@ -464,6 +464,8 @@ class OpenaiChat(AsyncAuthedProvider, ProviderModelMixin):
464
464
  }
465
465
  if temporary:
466
466
  data["history_and_training_disabled"] = True
467
+ if conversation.conversation_id is not None and not temporary:
468
+ data["conversation_id"] = conversation.conversation_id
467
469
  async with session.post(
468
470
  prepare_url,
469
471
  json=data,
@@ -1,5 +1,6 @@
1
1
  from .AIBadgr import AIBadgr
2
2
  from .Anthropic import Anthropic
3
+ from .Antigravity import Antigravity
3
4
  from .Azure import Azure
4
5
  from .BingCreateImages import BingCreateImages
5
6
  from .BlackboxPro import BlackboxPro