jebin2 commited on
Commit
178694a
·
1 Parent(s): 05b100e
Files changed (3) hide show
  1. app.py +5 -1
  2. core/database.py +1 -1
  3. routers/auth.py +18 -7
app.py CHANGED
@@ -146,9 +146,13 @@ app = FastAPI(
146
  )
147
 
148
  # Configure CORS
 
 
 
 
149
  app.add_middleware(
150
  CORSMiddleware,
151
- allow_origins=["*"], # Configure appropriately for production
152
  allow_credentials=True,
153
  allow_methods=["*"],
154
  allow_headers=["*"],
 
146
  )
147
 
148
  # Configure CORS
149
+ # For cookies to work with credentials, we need specific origins (not "*")
150
+ allowed_origins = os.getenv("CORS_ORIGINS", "http://localhost:3000,http://localhost:5173").split(",")
151
+ logger.info(f"CORS allowed origins: {allowed_origins}")
152
+
153
  app.add_middleware(
154
  CORSMiddleware,
155
+ allow_origins=allowed_origins, # Specific origins for production
156
  allow_credentials=True,
157
  allow_methods=["*"],
158
  allow_headers=["*"],
core/database.py CHANGED
@@ -7,7 +7,7 @@ import os
7
 
8
  # Database configuration
9
  DB_NAME = os.getenv("DB_NAME", "apigateway")
10
- ENV = os.getenv("ENV", "prod")
11
  DB_FILENAME = f"{DB_NAME}_{ENV}.db"
12
 
13
  # Database URL - SQLite file in the same directory
 
7
 
8
  # Database configuration
9
  DB_NAME = os.getenv("DB_NAME", "apigateway")
10
+ ENV = os.getenv("ENVIRONMENT", "production")
11
  DB_FILENAME = f"{DB_NAME}_{ENV}.db"
12
 
13
  # Database URL - SQLite file in the same directory
routers/auth.py CHANGED
@@ -239,21 +239,27 @@ async def google_auth(
239
  }
240
 
241
  # Handle token delivery based on client type
242
- if request.client_type == "web":
243
  # Web: Set HttpOnly cookie for refresh token
244
  response = JSONResponse(content=response_data)
 
 
 
245
  response.set_cookie(
246
  key="refresh_token",
247
  value=refresh_token,
248
  httponly=True,
249
- secure=True, # Should be True in production
250
- samesite="lax",
251
- max_age=7 * 24 * 60 * 60 # 7 days
 
252
  )
 
253
  else:
254
  # Mobile: Return refresh token in body
255
  response_data["refresh_token"] = refresh_token
256
  response = JSONResponse(content=response_data)
 
257
 
258
  return response
259
 
@@ -372,14 +378,19 @@ async def refresh_token(
372
  if using_cookie:
373
  # If came from cookie, rotate cookie
374
  response = JSONResponse(content=response_data)
 
 
 
375
  response.set_cookie(
376
  key="refresh_token",
377
  value=new_refresh_token,
378
  httponly=True,
379
- secure=True,
380
- samesite="lax",
381
- max_age=7 * 24 * 60 * 60
 
382
  )
 
383
  return response
384
  else:
385
  # If came from body, return in body
 
239
  }
240
 
241
  # Handle token delivery based on client type
242
+ if client_type == "web":
243
  # Web: Set HttpOnly cookie for refresh token
244
  response = JSONResponse(content=response_data)
245
+ # Cookie settings for production
246
+ import os
247
+ is_production = os.getenv("ENVIRONMENT", "production") == "production"
248
  response.set_cookie(
249
  key="refresh_token",
250
  value=refresh_token,
251
  httponly=True,
252
+ secure=is_production, # True in production (HTTPS), False locally (HTTP)
253
+ samesite="none" if is_production else "lax", # 'none' for cross-origin in production
254
+ max_age=7 * 24 * 60 * 60, # 7 days
255
+ domain=None # Let browser set domain automatically
256
  )
257
+ logger.info(f"Set refresh_token cookie for web client (production={is_production})")
258
  else:
259
  # Mobile: Return refresh token in body
260
  response_data["refresh_token"] = refresh_token
261
  response = JSONResponse(content=response_data)
262
+ logger.info(f"Returned refresh_token in body for mobile client")
263
 
264
  return response
265
 
 
378
  if using_cookie:
379
  # If came from cookie, rotate cookie
380
  response = JSONResponse(content=response_data)
381
+ # Cookie settings for production
382
+ import os
383
+ is_production = os.getenv("ENVIRONMENT", "production") == "production"
384
  response.set_cookie(
385
  key="refresh_token",
386
  value=new_refresh_token,
387
  httponly=True,
388
+ secure=is_production, # True in production (HTTPS), False locally (HTTP)
389
+ samesite="none" if is_production else "lax", # 'none' for cross-origin in production
390
+ max_age=7 * 24 * 60 * 60,
391
+ domain=None # Let browser set domain automatically
392
  )
393
+ logger.info(f"Rotated refresh_token cookie (production={is_production})")
394
  return response
395
  else:
396
  # If came from body, return in body