Spaces:
Sleeping
Sleeping
Nagesh Muralidhar
commited on
Commit
·
b7038e5
1
Parent(s):
d931ef2
Simplify login, signup, and token handlers by directly accessing database and handling authentication
Browse files
app.py
CHANGED
|
@@ -240,49 +240,65 @@ async def login_proxy(request: Request):
|
|
| 240 |
logger.info("Received login request at /login endpoint")
|
| 241 |
if backend_available:
|
| 242 |
try:
|
| 243 |
-
#
|
| 244 |
-
|
|
|
|
| 245 |
|
| 246 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 247 |
try:
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 251 |
|
| 252 |
-
#
|
| 253 |
-
|
| 254 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 255 |
|
| 256 |
-
#
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
logger.error(f"Error parsing JSON in login request: {str(json_error)}")
|
| 261 |
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
"headers": [[k.lower().encode(), v.encode()] for k, v in request.headers.items()],
|
| 273 |
-
}
|
| 274 |
-
|
| 275 |
-
# Create a new request with the same body
|
| 276 |
-
from starlette.datastructures import Headers
|
| 277 |
-
new_req = StarletteRequest(scope=scope, receive=lambda: {"type": "http.request", "body": body_bytes})
|
| 278 |
-
|
| 279 |
-
# Call backend login function
|
| 280 |
-
from backend.app.main import login
|
| 281 |
-
result = await login(new_req)
|
| 282 |
-
return result
|
| 283 |
-
except Exception as raw_error:
|
| 284 |
-
logger.error(f"Error forwarding raw request to login: {str(raw_error)}")
|
| 285 |
-
raise HTTPException(status_code=500, detail=f"Login processing error: {str(raw_error)}")
|
| 286 |
except Exception as e:
|
| 287 |
logger.error(f"General error in login_proxy: {str(e)}")
|
| 288 |
return JSONResponse(
|
|
@@ -301,48 +317,62 @@ async def signup_proxy(request: Request):
|
|
| 301 |
logger.info("Received signup request at /signup endpoint")
|
| 302 |
if backend_available:
|
| 303 |
try:
|
| 304 |
-
#
|
| 305 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 306 |
|
| 307 |
-
#
|
| 308 |
try:
|
| 309 |
-
from
|
| 310 |
-
|
| 311 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 312 |
|
| 313 |
-
#
|
| 314 |
-
|
| 315 |
-
logger.info(f"Signup request data: {user_data}")
|
| 316 |
|
| 317 |
-
#
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
# Create a new request with the same body
|
| 337 |
-
new_req = StarletteRequest(scope=scope, receive=lambda: {"type": "http.request", "body": body_bytes})
|
| 338 |
-
|
| 339 |
-
# Call backend signup function
|
| 340 |
-
from backend.app.main import signup
|
| 341 |
-
result = await signup(new_req)
|
| 342 |
-
return result
|
| 343 |
-
except Exception as raw_error:
|
| 344 |
-
logger.error(f"Error forwarding raw request to signup: {str(raw_error)}")
|
| 345 |
-
raise HTTPException(status_code=500, detail=f"Signup processing error: {str(raw_error)}")
|
| 346 |
except Exception as e:
|
| 347 |
logger.error(f"General error in signup_proxy: {str(e)}")
|
| 348 |
return JSONResponse(
|
|
@@ -361,45 +391,69 @@ async def token_proxy(request: Request):
|
|
| 361 |
logger.info("Received token request at /token endpoint")
|
| 362 |
if backend_available:
|
| 363 |
try:
|
| 364 |
-
#
|
| 365 |
-
body_bytes = await request.body()
|
| 366 |
-
|
| 367 |
-
# Try to get form data
|
| 368 |
try:
|
| 369 |
form_data = await request.form()
|
|
|
|
|
|
|
| 370 |
|
| 371 |
-
|
| 372 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 373 |
|
| 374 |
-
#
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 379 |
|
| 380 |
-
#
|
| 381 |
try:
|
| 382 |
-
|
| 383 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 384 |
|
| 385 |
-
#
|
| 386 |
-
|
| 387 |
-
"
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
|
| 393 |
-
# Create
|
| 394 |
-
|
|
|
|
|
|
|
| 395 |
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
except Exception as
|
| 401 |
-
logger.error(f"
|
| 402 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 403 |
except Exception as e:
|
| 404 |
logger.error(f"General error in token_proxy: {str(e)}")
|
| 405 |
return JSONResponse(
|
|
|
|
| 240 |
logger.info("Received login request at /login endpoint")
|
| 241 |
if backend_available:
|
| 242 |
try:
|
| 243 |
+
# Get the JSON data from the request
|
| 244 |
+
user_data = await request.json()
|
| 245 |
+
logger.info(f"Login request data: {user_data}")
|
| 246 |
|
| 247 |
+
# Extract username and password
|
| 248 |
+
username = user_data.get("username")
|
| 249 |
+
password = user_data.get("password")
|
| 250 |
+
|
| 251 |
+
if not username or not password:
|
| 252 |
+
logger.error("Missing username or password in login request")
|
| 253 |
+
return JSONResponse(
|
| 254 |
+
content={"error": "Missing username or password"},
|
| 255 |
+
status_code=400
|
| 256 |
+
)
|
| 257 |
+
|
| 258 |
+
# Import database connection and utility functions
|
| 259 |
+
try:
|
| 260 |
+
from backend.app.main import users, verify_password, create_access_token
|
| 261 |
+
from datetime import timedelta
|
| 262 |
+
except ImportError as e:
|
| 263 |
+
logger.error(f"Error importing backend modules: {str(e)}")
|
| 264 |
+
return JSONResponse(
|
| 265 |
+
content={"error": "Backend configuration error"},
|
| 266 |
+
status_code=500
|
| 267 |
+
)
|
| 268 |
+
|
| 269 |
+
# Try to find the user in the database
|
| 270 |
try:
|
| 271 |
+
user = await users.find_one({"username": username})
|
| 272 |
+
if not user:
|
| 273 |
+
logger.warning(f"User not found: {username}")
|
| 274 |
+
return JSONResponse(
|
| 275 |
+
content={"error": "Invalid username or password"},
|
| 276 |
+
status_code=401
|
| 277 |
+
)
|
| 278 |
|
| 279 |
+
# Verify password
|
| 280 |
+
if not verify_password(password, user["password"]):
|
| 281 |
+
logger.warning(f"Invalid password for user: {username}")
|
| 282 |
+
return JSONResponse(
|
| 283 |
+
content={"error": "Invalid username or password"},
|
| 284 |
+
status_code=401
|
| 285 |
+
)
|
| 286 |
|
| 287 |
+
# Create access token
|
| 288 |
+
access_token = create_access_token(
|
| 289 |
+
data={"sub": username}
|
| 290 |
+
)
|
|
|
|
| 291 |
|
| 292 |
+
return JSONResponse(
|
| 293 |
+
content={"access_token": access_token, "token_type": "bearer"},
|
| 294 |
+
status_code=200
|
| 295 |
+
)
|
| 296 |
+
except Exception as db_error:
|
| 297 |
+
logger.error(f"Database error in login: {str(db_error)}")
|
| 298 |
+
return JSONResponse(
|
| 299 |
+
content={"error": "Database error", "message": str(db_error)},
|
| 300 |
+
status_code=500
|
| 301 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 302 |
except Exception as e:
|
| 303 |
logger.error(f"General error in login_proxy: {str(e)}")
|
| 304 |
return JSONResponse(
|
|
|
|
| 317 |
logger.info("Received signup request at /signup endpoint")
|
| 318 |
if backend_available:
|
| 319 |
try:
|
| 320 |
+
# Get the JSON data from the request
|
| 321 |
+
user_data = await request.json()
|
| 322 |
+
logger.info(f"Signup request data: {user_data}")
|
| 323 |
+
|
| 324 |
+
# Extract username and password
|
| 325 |
+
username = user_data.get("username")
|
| 326 |
+
password = user_data.get("password")
|
| 327 |
+
|
| 328 |
+
if not username or not password:
|
| 329 |
+
logger.error("Missing username or password in signup request")
|
| 330 |
+
return JSONResponse(
|
| 331 |
+
content={"error": "Missing username or password"},
|
| 332 |
+
status_code=400
|
| 333 |
+
)
|
| 334 |
|
| 335 |
+
# Import database connection and utility functions
|
| 336 |
try:
|
| 337 |
+
from backend.app.main import users, get_password_hash
|
| 338 |
+
except ImportError as e:
|
| 339 |
+
logger.error(f"Error importing backend modules: {str(e)}")
|
| 340 |
+
return JSONResponse(
|
| 341 |
+
content={"error": "Backend configuration error"},
|
| 342 |
+
status_code=500
|
| 343 |
+
)
|
| 344 |
+
|
| 345 |
+
# Check if username exists
|
| 346 |
+
try:
|
| 347 |
+
existing_user = await users.find_one({"username": username})
|
| 348 |
+
if existing_user:
|
| 349 |
+
logger.warning(f"Username already exists: {username}")
|
| 350 |
+
return JSONResponse(
|
| 351 |
+
content={"error": "Username already exists"},
|
| 352 |
+
status_code=400
|
| 353 |
+
)
|
| 354 |
|
| 355 |
+
# Hash the password and create user
|
| 356 |
+
hashed_password = get_password_hash(password)
|
|
|
|
| 357 |
|
| 358 |
+
# Insert the new user
|
| 359 |
+
new_user = {
|
| 360 |
+
"username": username,
|
| 361 |
+
"password": hashed_password
|
| 362 |
+
}
|
| 363 |
|
| 364 |
+
await users.insert_one(new_user)
|
| 365 |
+
|
| 366 |
+
return JSONResponse(
|
| 367 |
+
content={"message": "User created successfully"},
|
| 368 |
+
status_code=201
|
| 369 |
+
)
|
| 370 |
+
except Exception as db_error:
|
| 371 |
+
logger.error(f"Database error in signup: {str(db_error)}")
|
| 372 |
+
return JSONResponse(
|
| 373 |
+
content={"error": "Database error", "message": str(db_error)},
|
| 374 |
+
status_code=500
|
| 375 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 376 |
except Exception as e:
|
| 377 |
logger.error(f"General error in signup_proxy: {str(e)}")
|
| 378 |
return JSONResponse(
|
|
|
|
| 391 |
logger.info("Received token request at /token endpoint")
|
| 392 |
if backend_available:
|
| 393 |
try:
|
| 394 |
+
# Get form data from the request
|
|
|
|
|
|
|
|
|
|
| 395 |
try:
|
| 396 |
form_data = await request.form()
|
| 397 |
+
username = form_data.get("username")
|
| 398 |
+
password = form_data.get("password")
|
| 399 |
|
| 400 |
+
if not username or not password:
|
| 401 |
+
logger.error("Missing username or password in token request")
|
| 402 |
+
return JSONResponse(
|
| 403 |
+
content={"error": "Missing username or password"},
|
| 404 |
+
status_code=400
|
| 405 |
+
)
|
| 406 |
|
| 407 |
+
# Import database connection and utility functions
|
| 408 |
+
try:
|
| 409 |
+
from backend.app.main import users, verify_password, create_access_token
|
| 410 |
+
from datetime import timedelta
|
| 411 |
+
except ImportError as e:
|
| 412 |
+
logger.error(f"Error importing backend modules: {str(e)}")
|
| 413 |
+
return JSONResponse(
|
| 414 |
+
content={"error": "Backend configuration error"},
|
| 415 |
+
status_code=500
|
| 416 |
+
)
|
| 417 |
|
| 418 |
+
# Try to find the user in the database
|
| 419 |
try:
|
| 420 |
+
user = await users.find_one({"username": username})
|
| 421 |
+
if not user:
|
| 422 |
+
logger.warning(f"User not found: {username}")
|
| 423 |
+
return JSONResponse(
|
| 424 |
+
content={"error": "Invalid username or password"},
|
| 425 |
+
status_code=401
|
| 426 |
+
)
|
| 427 |
|
| 428 |
+
# Verify password
|
| 429 |
+
if not verify_password(password, user["password"]):
|
| 430 |
+
logger.warning(f"Invalid password for user: {username}")
|
| 431 |
+
return JSONResponse(
|
| 432 |
+
content={"error": "Invalid username or password"},
|
| 433 |
+
status_code=401
|
| 434 |
+
)
|
| 435 |
|
| 436 |
+
# Create access token
|
| 437 |
+
access_token = create_access_token(
|
| 438 |
+
data={"sub": username}
|
| 439 |
+
)
|
| 440 |
|
| 441 |
+
return JSONResponse(
|
| 442 |
+
content={"access_token": access_token, "token_type": "bearer"},
|
| 443 |
+
status_code=200
|
| 444 |
+
)
|
| 445 |
+
except Exception as db_error:
|
| 446 |
+
logger.error(f"Database error in token request: {str(db_error)}")
|
| 447 |
+
return JSONResponse(
|
| 448 |
+
content={"error": "Database error", "message": str(db_error)},
|
| 449 |
+
status_code=500
|
| 450 |
+
)
|
| 451 |
+
except Exception as form_error:
|
| 452 |
+
logger.error(f"Error processing form data in token request: {str(form_error)}")
|
| 453 |
+
return JSONResponse(
|
| 454 |
+
content={"error": "Invalid form data", "message": str(form_error)},
|
| 455 |
+
status_code=400
|
| 456 |
+
)
|
| 457 |
except Exception as e:
|
| 458 |
logger.error(f"General error in token_proxy: {str(e)}")
|
| 459 |
return JSONResponse(
|