bibibi12345 commited on
Commit
4fe6125
·
1 Parent(s): c86a50e

fixed login link not showing

Browse files
Files changed (2) hide show
  1. src/auth.py +12 -1
  2. src/main.py +48 -11
src/auth.py CHANGED
@@ -135,7 +135,7 @@ def save_credentials(creds, project_id=None):
135
  json.dump(creds_data, f, indent=2)
136
 
137
 
138
- def get_credentials():
139
  """Loads credentials matching gemini-cli OAuth2 flow."""
140
  global credentials, credentials_from_env, user_project_id
141
 
@@ -365,6 +365,11 @@ def get_credentials():
365
  logging.error(f"Failed to read credentials file {CREDENTIAL_FILE}: {e}")
366
  # Fall through to new login only if file is completely unreadable
367
 
 
 
 
 
 
368
  client_config = {
369
  "installed": {
370
  "client_id": CLIENT_ID,
@@ -387,6 +392,12 @@ def get_credentials():
387
  prompt="consent",
388
  include_granted_scopes='true'
389
  )
 
 
 
 
 
 
390
  logging.info(f"Please open this URL in your browser to log in: {auth_url}")
391
 
392
  server = HTTPServer(("", 8080), _OAuthCallbackHandler)
 
135
  json.dump(creds_data, f, indent=2)
136
 
137
 
138
+ def get_credentials(allow_oauth_flow=True):
139
  """Loads credentials matching gemini-cli OAuth2 flow."""
140
  global credentials, credentials_from_env, user_project_id
141
 
 
365
  logging.error(f"Failed to read credentials file {CREDENTIAL_FILE}: {e}")
366
  # Fall through to new login only if file is completely unreadable
367
 
368
+ # Only start OAuth flow if explicitly allowed
369
+ if not allow_oauth_flow:
370
+ logging.info("OAuth flow not allowed - returning None (credentials will be required on first request)")
371
+ return None
372
+
373
  client_config = {
374
  "installed": {
375
  "client_id": CLIENT_ID,
 
392
  prompt="consent",
393
  include_granted_scopes='true'
394
  )
395
+ print(f"\n{'='*80}")
396
+ print(f"AUTHENTICATION REQUIRED")
397
+ print(f"{'='*80}")
398
+ print(f"Please open this URL in your browser to log in:")
399
+ print(f"{auth_url}")
400
+ print(f"{'='*80}\n")
401
  logging.info(f"Please open this URL in your browser to log in: {auth_url}")
402
 
403
  server = HTTPServer(("", 8080), _OAuthCallbackHandler)
src/main.py CHANGED
@@ -37,20 +37,57 @@ app.add_middleware(
37
  async def startup_event():
38
  try:
39
  logging.info("Starting Gemini proxy server...")
40
- creds = get_credentials()
41
- if creds:
 
 
 
 
 
 
 
42
  try:
43
- proj_id = get_user_project_id(creds)
44
- if proj_id:
45
- onboard_user(creds, proj_id)
46
- logging.info(f"Successfully onboarded with project ID: {proj_id}")
47
- logging.info("Gemini proxy server started successfully")
48
- logging.info("Authentication required - Password: see .env file")
 
 
 
 
 
 
 
 
 
49
  except Exception as e:
50
- logging.error(f"Setup failed: {str(e)}")
51
- logging.warning("Server started but may not function properly until setup issues are resolved.")
52
  else:
53
- logging.error("Could not obtain credentials. Please authenticate and restart the server.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  except Exception as e:
55
  logging.error(f"Startup error: {str(e)}")
56
  logging.warning("Server may not function properly.")
 
37
  async def startup_event():
38
  try:
39
  logging.info("Starting Gemini proxy server...")
40
+
41
+ # Check if credentials exist
42
+ import os
43
+ from .config import CREDENTIAL_FILE
44
+
45
+ env_creds_json = os.getenv("GEMINI_CREDENTIALS")
46
+ creds_file_exists = os.path.exists(CREDENTIAL_FILE)
47
+
48
+ if env_creds_json or creds_file_exists:
49
  try:
50
+ # Try to load existing credentials without OAuth flow first
51
+ creds = get_credentials(allow_oauth_flow=False)
52
+ if creds:
53
+ try:
54
+ proj_id = get_user_project_id(creds)
55
+ if proj_id:
56
+ onboard_user(creds, proj_id)
57
+ logging.info(f"Successfully onboarded with project ID: {proj_id}")
58
+ logging.info("Gemini proxy server started successfully")
59
+ logging.info("Authentication required - Password: see .env file")
60
+ except Exception as e:
61
+ logging.error(f"Setup failed: {str(e)}")
62
+ logging.warning("Server started but may not function properly until setup issues are resolved.")
63
+ else:
64
+ logging.warning("Credentials file exists but could not be loaded. Server started - authentication will be required on first request.")
65
  except Exception as e:
66
+ logging.error(f"Credential loading error: {str(e)}")
67
+ logging.warning("Server started but credentials need to be set up.")
68
  else:
69
+ # No credentials found - prompt user to authenticate
70
+ logging.info("No credentials found. Starting OAuth authentication flow...")
71
+ try:
72
+ creds = get_credentials(allow_oauth_flow=True)
73
+ if creds:
74
+ try:
75
+ proj_id = get_user_project_id(creds)
76
+ if proj_id:
77
+ onboard_user(creds, proj_id)
78
+ logging.info(f"Successfully onboarded with project ID: {proj_id}")
79
+ logging.info("Gemini proxy server started successfully")
80
+ except Exception as e:
81
+ logging.error(f"Setup failed: {str(e)}")
82
+ logging.warning("Server started but may not function properly until setup issues are resolved.")
83
+ else:
84
+ logging.error("Authentication failed. Server started but will not function until credentials are provided.")
85
+ except Exception as e:
86
+ logging.error(f"Authentication error: {str(e)}")
87
+ logging.warning("Server started but authentication failed.")
88
+
89
+ logging.info("Authentication required - Password: see .env file")
90
+
91
  except Exception as e:
92
  logging.error(f"Startup error: {str(e)}")
93
  logging.warning("Server may not function properly.")